2 // Copyright (c) 2010-2017 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <rte_common.h>
18 #ifndef __rte_cache_aligned
19 #include <rte_memory.h>
23 #include <rte_ether.h>
24 #include <rte_memcpy.h>
29 #include <rte_version.h>
30 #include <rte_byteorder.h>
32 #include "handle_lb_5tuple.h"
33 #include "prox_malloc.h"
35 #include "prox_lua_types.h"
37 #include "task_init.h"
38 #include "task_base.h"
42 #include "prox_globals.h"
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
51 #define HASH_MAX_SIZE 4*8*1024*1024
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;
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)
64 union ipv4_5tuple_host key;
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);
71 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
73 rte_mov16(&key.pad0, ipv4_hdr);
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]);
82 static inline uint8_t handle_lb_5tuple(struct task_lb_5tuple *task, struct rte_mbuf *mbuf)
84 prox_rte_ether_hdr *eth_hdr;
85 prox_rte_ipv4_hdr *ipv4_hdr;
87 eth_hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
89 switch (eth_hdr->ether_type) {
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);
99 static int handle_lb_5tuple_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
101 struct task_lb_5tuple *task = (struct task_lb_5tuple *)tbase;
102 uint8_t out[MAX_PKT_BURST];
105 prefetch_first(mbufs, n_pkts);
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 *));
112 out[j] = handle_lb_5tuple(task, mbufs[j]);
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]);
121 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
124 static void init_task_lb_5tuple(struct task_base *tbase, struct task_args *targ)
126 struct task_lb_5tuple *task = (struct task_lb_5tuple *)tbase;
127 const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
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");
134 task->runtime_flags = targ->flags;
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),
145 __attribute__((constructor)) static void reg_task_lb_5tuple(void)
147 reg_task(&task_init_lb_5tuple);