Support packets in flight
[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_common.h>
18 #ifndef __rte_cache_aligned
19 #include <rte_memory.h>
20 #endif
21
22 #include <rte_hash.h>
23 #include <rte_ether.h>
24 #include <rte_memcpy.h>
25 #include <rte_mbuf.h>
26 #include <rte_ip.h>
27 #include <rte_tcp.h>
28 #include <rte_udp.h>
29 #include <rte_version.h>
30 #include <rte_byteorder.h>
31
32 #include "handle_lb_5tuple.h"
33 #include "prox_malloc.h"
34 #include "prox_lua.h"
35 #include "prox_lua_types.h"
36 #include "etypes.h"
37 #include "task_init.h"
38 #include "task_base.h"
39 #include "lconf.h"
40 #include "log.h"
41 #include "prefetch.h"
42 #include "prox_globals.h"
43 #include "defines.h"
44 #include "quit.h"
45
46 #define BYTE_VALUE_MAX 256
47 #define BIT_8_TO_10    0x0000e000         // for protocol field
48 #define BIT_27_TO_31   0x1f000000         // for IP addresses
49 #define BIT_12_TO_16_27_TO_31 0x1f001f00  // for ports
50
51 #define HASH_MAX_SIZE 4*8*1024*1024
52
53 struct task_lb_5tuple {
54         struct task_base base;
55         uint32_t runtime_flags;
56         struct rte_hash *lookup_hash;
57         uint8_t out_if[HASH_MAX_SIZE] __rte_cache_aligned;
58 };
59
60 static __m128i mask0;
61 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)
62 {
63         int ret = 0;
64         union ipv4_5tuple_host key;
65
66         ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(prox_rte_ipv4_hdr, time_to_live);
67         __m128i data = _mm_loadu_si128((__m128i*)(ipv4_hdr));
68         /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
69         key.xmm = _mm_and_si128(data, mask0);
70
71         /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
72         /*
73         rte_mov16(&key.pad0, ipv4_hdr);
74         key.pad0 = 0;
75         key.pad1 = 0;
76         */
77         /* Find destination port */
78         ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
79         return (uint8_t)((ret < 0)? portid : task->out_if[ret]);
80 }
81
82 static inline uint8_t handle_lb_5tuple(struct task_lb_5tuple *task, struct rte_mbuf *mbuf)
83 {
84         prox_rte_ether_hdr *eth_hdr;
85         prox_rte_ipv4_hdr *ipv4_hdr;
86
87         eth_hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
88
89         switch (eth_hdr->ether_type) {
90         case ETYPE_IPv4:
91                 /* Handle IPv4 headers.*/
92                 ipv4_hdr = (prox_rte_ipv4_hdr *) (eth_hdr + 1);
93                 return get_ipv4_dst_port(task, ipv4_hdr, OUT_DISCARD, task->lookup_hash);
94         default:
95                 return OUT_DISCARD;
96         }
97 }
98
99 static int handle_lb_5tuple_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
100 {
101         struct task_lb_5tuple *task = (struct task_lb_5tuple *)tbase;
102         uint8_t out[MAX_PKT_BURST];
103         uint16_t j;
104
105         prefetch_first(mbufs, n_pkts);
106
107         for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
108 #ifdef PROX_PREFETCH_OFFSET
109                 PREFETCH0(mbufs[j + PREFETCH_OFFSET]);
110                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
111 #endif
112                 out[j] = handle_lb_5tuple(task, mbufs[j]);
113         }
114 #ifdef PROX_PREFETCH_OFFSET
115         PREFETCH0(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
116         for (; j < n_pkts; ++j) {
117                 out[j] = handle_lb_5tuple(task, mbufs[j]);
118         }
119 #endif
120
121         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
122 }
123
124 static void init_task_lb_5tuple(struct task_base *tbase, struct task_args *targ)
125 {
126         struct task_lb_5tuple *task = (struct task_lb_5tuple *)tbase;
127         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
128
129         mask0 = _mm_set_epi32(BIT_12_TO_16_27_TO_31, BIT_27_TO_31, BIT_27_TO_31, BIT_8_TO_10);
130         uint8_t *out_table = task->out_if;
131         int ret = lua_to_tuples(prox_lua(), GLOBAL, "tuples", socket_id, &task->lookup_hash, &out_table);
132         PROX_PANIC(ret, "Failed to read tuples from config\n");
133
134         task->runtime_flags = targ->flags;
135 }
136
137 static struct task_init task_init_lb_5tuple = {
138         .mode_str = "lb5tuple",
139         .init = init_task_lb_5tuple,
140         .handle = handle_lb_5tuple_bulk,
141         .flag_features = TASK_FEATURE_NEVER_DISCARDS | TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS,
142         .size = sizeof(struct task_lb_5tuple),
143 };
144
145 __attribute__((constructor)) static void reg_task_lb_5tuple(void)
146 {
147         reg_task(&task_init_lb_5tuple);
148 }