Create combined image and add helm chart
[samplevnf.git] / VNFs / DPPD-PROX / handle_lb_5tuple.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <rte_hash.h>
18 #include <rte_ether.h>
19 #include <rte_memcpy.h>
20 #include <rte_mbuf.h>
21 #include <rte_ip.h>
22 #include <rte_tcp.h>
23 #include <rte_udp.h>
24 #include <rte_version.h>
25 #include <rte_byteorder.h>
26
27 #include "handle_lb_5tuple.h"
28 #include "prox_malloc.h"
29 #include "prox_lua.h"
30 #include "prox_lua_types.h"
31 #include "etypes.h"
32 #include "task_init.h"
33 #include "task_base.h"
34 #include "lconf.h"
35 #include "log.h"
36 #include "prefetch.h"
37 #include "prox_globals.h"
38 #include "defines.h"
39 #include "quit.h"
40
41 #define BYTE_VALUE_MAX 256
42 #define BIT_8_TO_10    0x0000e000         // for protocol field
43 #define BIT_27_TO_31   0x1f000000         // for IP addresses
44 #define BIT_12_TO_16_27_TO_31 0x1f001f00  // for ports
45
46 #define HASH_MAX_SIZE 4*8*1024*1024
47
48 struct task_lb_5tuple {
49         struct task_base base;
50         uint32_t runtime_flags;
51         struct rte_hash *lookup_hash;
52         uint8_t out_if[HASH_MAX_SIZE] __rte_cache_aligned;
53 };
54
55 static __m128i mask0;
56 static inline uint8_t get_ipv4_dst_port(struct task_lb_5tuple *task, void *ipv4_hdr, uint8_t portid, struct rte_hash * ipv4_l3fwd_lookup_struct)
57 {
58         int ret = 0;
59         union ipv4_5tuple_host key;
60
61         ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(prox_rte_ipv4_hdr, time_to_live);
62         __m128i data = _mm_loadu_si128((__m128i*)(ipv4_hdr));
63         /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
64         key.xmm = _mm_and_si128(data, mask0);
65
66         /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
67         /*
68         rte_mov16(&key.pad0, ipv4_hdr);
69         key.pad0 = 0;
70         key.pad1 = 0;
71         */
72         /* Find destination port */
73         ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
74         return (uint8_t)((ret < 0)? portid : task->out_if[ret]);
75 }
76
77 static inline uint8_t handle_lb_5tuple(struct task_lb_5tuple *task, struct rte_mbuf *mbuf)
78 {
79         prox_rte_ether_hdr *eth_hdr;
80         prox_rte_ipv4_hdr *ipv4_hdr;
81
82         eth_hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
83
84         switch (eth_hdr->ether_type) {
85         case ETYPE_IPv4:
86                 /* Handle IPv4 headers.*/
87                 ipv4_hdr = (prox_rte_ipv4_hdr *) (eth_hdr + 1);
88                 return get_ipv4_dst_port(task, ipv4_hdr, OUT_DISCARD, task->lookup_hash);
89         default:
90                 return OUT_DISCARD;
91         }
92 }
93
94 static int handle_lb_5tuple_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
95 {
96         struct task_lb_5tuple *task = (struct task_lb_5tuple *)tbase;
97         uint8_t out[MAX_PKT_BURST];
98         uint16_t j;
99
100         prefetch_first(mbufs, n_pkts);
101
102         for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
103 #ifdef PROX_PREFETCH_OFFSET
104                 PREFETCH0(mbufs[j + PREFETCH_OFFSET]);
105                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
106 #endif
107                 out[j] = handle_lb_5tuple(task, mbufs[j]);
108         }
109 #ifdef PROX_PREFETCH_OFFSET
110         PREFETCH0(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
111         for (; j < n_pkts; ++j) {
112                 out[j] = handle_lb_5tuple(task, mbufs[j]);
113         }
114 #endif
115
116         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
117 }
118
119 static void init_task_lb_5tuple(struct task_base *tbase, struct task_args *targ)
120 {
121         struct task_lb_5tuple *task = (struct task_lb_5tuple *)tbase;
122         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
123
124         mask0 = _mm_set_epi32(BIT_12_TO_16_27_TO_31, BIT_27_TO_31, BIT_27_TO_31, BIT_8_TO_10);
125         uint8_t *out_table = task->out_if;
126         int ret = lua_to_tuples(prox_lua(), GLOBAL, "tuples", socket_id, &task->lookup_hash, &out_table);
127         PROX_PANIC(ret, "Failed to read tuples from config\n");
128
129         task->runtime_flags = targ->flags;
130 }
131
132 static struct task_init task_init_lb_5tuple = {
133         .mode_str = "lb5tuple",
134         .init = init_task_lb_5tuple,
135         .handle = handle_lb_5tuple_bulk,
136         .flag_features = TASK_FEATURE_NEVER_DISCARDS | TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS,
137         .size = sizeof(struct task_lb_5tuple),
138 };
139
140 __attribute__((constructor)) static void reg_task_lb_5tuple(void)
141 {
142         reg_task(&task_init_lb_5tuple);
143 }