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.
18 #include <rte_ether.h>
19 #include <rte_memcpy.h>
24 #include <rte_version.h>
25 #include <rte_byteorder.h>
27 #include "handle_lb_5tuple.h"
28 #include "prox_malloc.h"
30 #include "prox_lua_types.h"
32 #include "task_init.h"
33 #include "task_base.h"
37 #include "prox_globals.h"
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
46 #define HASH_MAX_SIZE 4*8*1024*1024
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;
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)
59 union ipv4_5tuple_host key;
61 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct 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);
66 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
68 rte_mov16(&key.pad0, ipv4_hdr);
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]);
77 static inline uint8_t handle_lb_5tuple(struct task_lb_5tuple *task, struct rte_mbuf *mbuf)
79 struct ether_hdr *eth_hdr;
80 struct ipv4_hdr *ipv4_hdr;
82 eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
84 switch (eth_hdr->ether_type) {
86 /* Handle IPv4 headers.*/
87 ipv4_hdr = (struct ipv4_hdr *) (eth_hdr + 1);
88 return get_ipv4_dst_port(task, ipv4_hdr, OUT_DISCARD, task->lookup_hash);
94 static int handle_lb_5tuple_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
96 struct task_lb_5tuple *task = (struct task_lb_5tuple *)tbase;
97 uint8_t out[MAX_PKT_BURST];
100 prefetch_first(mbufs, n_pkts);
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 *));
107 out[j] = handle_lb_5tuple(task, mbufs[j]);
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]);
116 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
119 static void init_task_lb_5tuple(struct task_base *tbase, struct task_args *targ)
121 struct task_lb_5tuple *task = (struct task_lb_5tuple *)tbase;
122 const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
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");
129 task->runtime_flags = targ->flags;
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),
140 __attribute__((constructor)) static void reg_task_lb_5tuple(void)
142 reg_task(&task_init_lb_5tuple);