0aa6876fd52625ade2df94b7d006b64d1b9ad2ab
[samplevnf.git] / VNFs / DPPD-PROX / handle_cgnat.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_mbuf.h>
18 #include <rte_hash.h>
19 #include <rte_hash_crc.h>
20 #include <rte_ether.h>
21 #include <rte_ip.h>
22 #include <rte_version.h>
23 #include <rte_byteorder.h>
24 #include <rte_lpm.h>
25
26 #include "prox_lua_types.h"
27 #include "prox_lua.h"
28 #include "prox_malloc.h"
29 #include "prox_cksum.h"
30 #include "prefetch.h"
31 #include "etypes.h"
32 #include "log.h"
33 #include "quit.h"
34 #include "task_init.h"
35 #include "task_base.h"
36 #include "lconf.h"
37 #include "log.h"
38 #include "prox_port_cfg.h"
39 #include "hash_entry_types.h"
40 #include "prox_shared.h"
41 #include "handle_cgnat.h"
42
43 #define ALL_32_BITS 0xffffffff
44 #define BIT_16_TO_31 0xffff0000
45 #define BIT_8_TO_15 0x0000ff00
46 #define BIT_0_TO_15 0x0000ffff
47
48 struct private_key {
49                 uint32_t ip_addr;
50                 uint16_t l4_port;
51 } __attribute__((packed));
52
53 struct private_flow_entry {
54         uint64_t flow_time;
55         uint32_t ip_addr;
56         uint32_t private_ip_idx;
57         uint16_t l4_port;
58 };
59
60 struct public_key {
61         uint32_t ip_addr;
62         uint16_t l4_port;
63 } __attribute__((packed));
64
65 struct public_entry {
66         uint32_t ip_addr;
67         uint16_t l4_port;
68         uint32_t private_ip_idx;
69         uint8_t dpdk_port;
70 };
71
72 struct public_ip_config_info {
73         uint32_t public_ip;
74         uint32_t max_port_count;
75         uint32_t port_free_count;
76         uint16_t *port_list;
77 };
78
79 struct private_ip_info {
80         uint64_t mac_aging_time;
81         uint32_t public_ip;
82         uint32_t public_ip_idx;
83         struct rte_ether *private_mac;
84         uint8_t static_entry;
85 };
86
87 struct task_nat {
88         struct task_base base;
89         struct rte_hash  *private_ip_hash;
90         struct rte_hash  *private_ip_port_hash;
91         struct rte_hash  *public_ip_port_hash;
92         struct private_flow_entry *private_flow_entries;
93         struct public_entry *public_entries;
94         struct next_hop *next_hops;
95         struct lcore_cfg *lconf;
96         struct rte_lpm *ipv4_lpm;
97         uint32_t total_free_port_count;
98         uint32_t number_free_rules;
99         int    private;
100         uint32_t public_ip_count;
101         uint32_t last_ip;
102         struct public_ip_config_info *public_ip_config_info;
103         struct private_ip_info *private_ip_info;
104         uint8_t runtime_flags;
105         int offload_crc;
106         uint64_t src_mac[PROX_MAX_PORTS];
107         uint64_t src_mac_from_dpdk_port[PROX_MAX_PORTS];
108         volatile int dump_public_hash;
109         volatile int dump_private_hash;
110 };
111 static __m128i proto_ipsrc_portsrc_mask;
112 static __m128i proto_ipdst_portdst_mask;
113 struct pkt_eth_ipv4 {
114         struct ether_hdr ether_hdr;
115         struct ipv4_hdr  ipv4_hdr;
116         struct udp_hdr  udp_hdr;
117 } __attribute__((packed));
118
119 void task_cgnat_dump_public_hash(struct task_nat *task)
120 {
121         task->dump_public_hash = 1;
122 }
123
124 void task_cgnat_dump_private_hash(struct task_nat *task)
125 {
126         task->dump_private_hash = 1;
127 }
128
129 static void set_l2(struct task_nat *task, struct rte_mbuf *mbuf, uint8_t nh_idx)
130 {
131         struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
132         *((uint64_t *)(&peth->d_addr)) = task->next_hops[nh_idx].mac_port_8bytes;
133         *((uint64_t *)(&peth->s_addr)) = task->src_mac[task->next_hops[nh_idx].mac_port.out_idx];
134 }
135
136 static uint8_t route_ipv4(struct task_nat *task, struct rte_mbuf *mbuf)
137 {
138         struct pkt_eth_ipv4 *pkt = rte_pktmbuf_mtod(mbuf, struct pkt_eth_ipv4 *);
139         struct ipv4_hdr *ip = &pkt->ipv4_hdr;
140         struct ether_hdr *peth_out;
141         uint8_t tx_port;
142         uint32_t dst_ip;
143
144         switch(ip->next_proto_id) {
145         case IPPROTO_TCP:
146         case IPPROTO_UDP:
147                 dst_ip = ip->dst_addr;
148                 break;
149         default:
150                 /* Routing for other protocols is not implemented */
151                 plogx_info("Routing nit implemented for this protocol\n");
152                 return OUT_DISCARD;
153         }
154
155 #if RTE_VERSION >= RTE_VERSION_NUM(16,4,0,1)
156         uint32_t next_hop_index;
157 #else
158         uint8_t next_hop_index;
159 #endif
160         if (unlikely(rte_lpm_lookup(task->ipv4_lpm, rte_bswap32(dst_ip), &next_hop_index) != 0)) {
161                 uint8_t* dst_ipp = (uint8_t*)&dst_ip;
162                 plog_warn("lpm_lookup failed for ip %d.%d.%d.%d: rc = %d\n",
163                         dst_ipp[0], dst_ipp[1], dst_ipp[2], dst_ipp[3], -ENOENT);
164                 return OUT_DISCARD;
165         }
166
167         tx_port = task->next_hops[next_hop_index].mac_port.out_idx;
168         set_l2(task, mbuf, next_hop_index);
169         return tx_port;
170 }
171
172 static int release_ip(struct task_nat *task, uint32_t *ip_addr, int public_ip_idx)
173 {
174         return 0;
175 }
176
177 static int release_port(struct task_nat *task, uint32_t public_ip_idx, uint16_t udp_src_port)
178 {
179         struct public_ip_config_info *public_ip_config_info = &task->public_ip_config_info[public_ip_idx];
180         if (public_ip_config_info->max_port_count > public_ip_config_info->port_free_count) {
181                 public_ip_config_info->port_list[public_ip_config_info->port_free_count] = udp_src_port;
182                 public_ip_config_info->port_free_count++;
183                 task->total_free_port_count ++;
184                 plogx_dbg("Now %d free ports for IP %d.%d.%d.%d\n", public_ip_config_info->port_free_count, IP4(public_ip_config_info->public_ip));
185         } else {
186                 plogx_err("Unable to release port for ip index %d: max_port_count = %d, port_free_count = %d", public_ip_idx, public_ip_config_info->max_port_count, public_ip_config_info->port_free_count);
187                 return -1;
188         }
189         return 0;
190 }
191
192 static int get_new_ip(struct task_nat *task, uint32_t *ip_addr)
193 {
194         struct public_ip_config_info *ip_info;
195         if (++task->last_ip >= task->public_ip_count)
196                 task->last_ip = 0;
197         for (uint32_t ip_idx = task->last_ip; ip_idx < task->public_ip_count; ip_idx++) {
198                 ip_info = &task->public_ip_config_info[ip_idx];
199                 plogx_dbg("Checking public IP index %d\n", ip_idx);
200                 if ((ip_info->port_free_count) > 0) {
201                         plogx_dbg("Public IP index %d (IP %d.%d.%d.%d) has %d free ports\n", ip_idx, IP4(ip_info->public_ip), ip_info->port_free_count);
202                         *ip_addr = ip_info->public_ip;
203                         task->last_ip = ip_idx;
204                         return ip_idx;
205                 }
206         }
207         for (uint32_t ip_idx = 0; ip_idx < task->last_ip; ip_idx++) {
208                 ip_info = &task->public_ip_config_info[ip_idx];
209                 if ((ip_info->port_free_count) > 0) {
210                         plogx_dbg("Public IP index %d (IP %d.%d.%d.%d) has %d free ports\n", ip_idx, IP4(ip_info->public_ip), ip_info->port_free_count);
211                         *ip_addr = ip_info->public_ip;
212                         task->last_ip = ip_idx;
213                         return ip_idx;
214                 }
215         }
216         return -1;
217 }
218
219 static int get_new_port(struct task_nat *task, uint32_t ip_idx, uint16_t *udp_src_port)
220 {
221         int ret;
222         struct public_ip_config_info *public_ip_config_info = &task->public_ip_config_info[ip_idx];
223         if (public_ip_config_info->port_free_count > 0) {
224                 public_ip_config_info->port_free_count--;
225                 *udp_src_port = public_ip_config_info->port_list[public_ip_config_info->port_free_count];
226                 task->total_free_port_count --;
227                 plogx_info("Now %d free ports for IP %d.%d.%d.%d\n", public_ip_config_info->port_free_count, IP4(public_ip_config_info->public_ip));
228         } else
229                 return -1;
230         return 0;
231 }
232
233 static int delete_port_entry(struct task_nat *task, uint8_t proto, uint32_t private_ip, uint16_t private_port,  uint32_t public_ip, uint16_t public_port, int public_ip_idx)
234 {
235         int ret;
236         struct private_key private_key;
237         struct public_key public_key;
238 //      private_key.proto = proto;
239         private_key.ip_addr = private_ip;
240         private_key.l4_port = private_port;
241         ret = rte_hash_del_key(task->private_ip_port_hash, (const void *)&private_key);
242         if (ret < 0) {
243                 plogx_info("Unable delete key ip %d.%d.%d.%d / port %x in private ip_port hash\n", IP4(private_ip), private_port);
244                 return -1;
245         } else {
246                 plogx_dbg("Deleted ip %d.%d.%d.%d / port %x from private ip_port hash\n", IP4(private_ip), private_port);
247         }
248         public_key.ip_addr = public_ip;
249         public_key.l4_port = public_port;
250         ret = rte_hash_del_key(task->public_ip_port_hash, (const void *)&public_key);
251         if (ret < 0) {
252                 plogx_info("Unable delete key ip %d.%d.%d.%d / port %x in public ip_port hash\n", IP4(public_ip), public_port);
253                 return -1;
254         } else {
255                 plogx_dbg("Deleted ip %d.%d.%d.%d / port %x (hash index %d) from public ip_port hash\n", IP4(public_ip), public_port, ret);
256                 release_port(task, public_ip_idx, public_port);
257         }
258         return 0;
259 }
260
261 static int add_new_port_entry(struct task_nat *task, uint8_t proto, int public_ip_idx, int private_ip_idx, uint32_t private_src_ip, uint16_t private_udp_port, struct rte_mbuf *mbuf, uint64_t tsc, uint16_t *port)
262 {
263         struct private_key private_key;
264         struct public_key public_key;
265         uint32_t ip = task->public_ip_config_info[public_ip_idx].public_ip;
266         int ret;
267         if (get_new_port(task, public_ip_idx, port) < 0) {
268                 plogx_info("Unable to find new port for IP %x\n", private_src_ip);
269                 return -1;
270         }
271 //      private_key.proto = proto;
272         private_key.ip_addr = private_src_ip;
273         private_key.l4_port = private_udp_port;
274         ret = rte_hash_add_key(task->private_ip_port_hash, (const void *)&private_key);
275         if (ret < 0) {
276                 plogx_info("Unable add ip %d.%d.%d.%d / port %x in private ip_port hash\n", IP4(private_src_ip), private_udp_port);
277                 release_port(task, public_ip_idx, *port);
278                 return -1;
279         } else if (task->private_flow_entries[ret].ip_addr) {
280                 plogx_dbg("Race condition properly handled: port alrerady added\n");
281                 release_port(task, public_ip_idx, *port);
282                 return ret;
283         } else {
284                 plogx_dbg("Added ip %d.%d.%d.%d / port %x in private ip_port hash => %d.%d.%d.%d / %d - index = %d\n", IP4(private_src_ip), private_udp_port, IP4(ip), *port, ret);
285         }
286         task->private_flow_entries[ret].ip_addr = ip;
287         task->private_flow_entries[ret].l4_port = *port;
288         task->private_flow_entries[ret].flow_time = tsc;
289         task->private_flow_entries[ret].private_ip_idx = private_ip_idx;
290
291         public_key.ip_addr = ip;
292         public_key.l4_port = *port;
293         plogx_dbg("Adding key ip %d.%d.%d.%d / port %x in public ip_port hash\n", IP4(ip), *port);
294         ret = rte_hash_add_key(task->public_ip_port_hash, (const void *)&public_key);
295         if (ret < 0) {
296                 plogx_info("Unable add ip %x / port %x in public ip_port hash\n", ip, *port);
297                 // TODO: remove from private_ip_port_hash
298                 release_port(task, public_ip_idx, *port);
299                 return -1;
300         } else {
301                 plogx_dbg("Added ip %d.%d.%d.%d / port %x in public ip_port hash\n", IP4(ip), *port);
302         }
303         task->public_entries[ret].ip_addr = private_src_ip;
304         task->public_entries[ret].l4_port = private_udp_port;
305         task->public_entries[ret].dpdk_port = mbuf->port;
306         task->public_entries[ret].private_ip_idx = private_ip_idx;
307         return ret;
308 }
309
310 static int handle_nat_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
311 {
312         struct task_nat *task = (struct task_nat *)tbase;
313         uint8_t out[MAX_PKT_BURST];
314         uint16_t j;
315         uint32_t *ip_addr, public_ip, private_ip;
316         uint16_t *udp_src_port, port, private_port, public_port;
317         struct pkt_eth_ipv4 *pkt[MAX_PKT_BURST];
318         int ret, private_ip_idx, public_ip_idx = -1, port_idx;
319         int new_entry = 0;
320         uint8_t proto;
321         uint64_t tsc = rte_rdtsc();
322         void *keys[MAX_PKT_BURST];
323         int32_t positions[MAX_PKT_BURST];
324         int map[MAX_PKT_BURST] = {0};
325
326         if (unlikely(task->dump_public_hash)) {
327                 const struct public_key *next_key;
328                 void *next_data;
329                 uint32_t iter = 0;
330                 int i = 0;
331                 int ret;
332
333                 while ((ret = rte_hash_iterate(task->public_ip_port_hash, (const void **)&next_key, &next_data, &iter)) >= 0) {
334                         plogx_info("Public entry %d (index %d): ip = %d.%d.%d.%d, port = %d ===> private entry: ip = %d.%d.%d.%d, port = %d\n", i++, ret, IP4(next_key->ip_addr), next_key->l4_port, IP4(task->public_entries[ret].ip_addr),task->public_entries[ret].l4_port);
335                 }
336                 task->dump_public_hash = 0;
337         }
338         if (unlikely(task->dump_private_hash)) {
339                 const struct private_key *next_key;
340                 void *next_data;
341                 uint32_t iter = 0;
342                 int i = 0;
343                 int ret;
344
345                 while ((ret = rte_hash_iterate(task->private_ip_port_hash, (const void **)&next_key, &next_data, &iter)) >= 0) {
346                         plogx_info("Private entry %d (index %d): ip = %d.%d.%d.%d, port = %d ===> public entry: ip = %d.%d.%d.%d, port = %d\n", i++, ret, IP4(next_key->ip_addr), next_key->l4_port, IP4(task->private_flow_entries[ret].ip_addr),task->private_flow_entries[ret].l4_port);
347                 }
348                 task->dump_private_hash = 0;
349         }
350
351         for (j = 0; j < n_pkts; ++j) {
352                 PREFETCH0(mbufs[j]);
353         }
354         for (j = 0; j < n_pkts; ++j) {
355                 pkt[j] = rte_pktmbuf_mtod(mbufs[j], struct pkt_eth_ipv4 *);
356                 PREFETCH0(pkt[j]);
357         }
358         if (task->private) {
359                 struct private_key key[MAX_PKT_BURST];
360                 for (j = 0; j < n_pkts; ++j) {
361                         /* Currently, only support eth/ipv4 packets */
362                         if (pkt[j]->ether_hdr.ether_type != ETYPE_IPv4) {
363                                 plogx_info("Currently, only support eth/ipv4 packets\n");
364                                 out[j] = OUT_DISCARD;
365                                 keys[j] = (void *)NULL;
366                                 continue;
367                         }
368                         key[j].ip_addr = pkt[j]->ipv4_hdr.src_addr;
369                         key[j].l4_port = pkt[j]->udp_hdr.src_port;
370                         keys[j] = &key[j];
371                 }
372                 ret = rte_hash_lookup_bulk(task->private_ip_port_hash, (const void **)&keys, n_pkts, positions);
373                 if (unlikely(ret < 0)) {
374                         plogx_info("lookup_bulk failed in private_ip_port_hash\n");
375                         return -1;
376                 }
377                 int n_new_mapping = 0;
378                 for (j = 0; j < n_pkts; ++j) {
379                         port_idx = positions[j];
380                         if (unlikely(port_idx < 0)) {
381                                 plogx_dbg("ip %d.%d.%d.%d / port %x not found in private ip/port hash\n", IP4(pkt[j]->ipv4_hdr.src_addr), pkt[j]->udp_hdr.src_port);
382                                 map[n_new_mapping] = j;
383                                 keys[n_new_mapping++] = (void *)&(pkt[j]->ipv4_hdr.src_addr);
384                         } else {
385                                 ip_addr = &(pkt[j]->ipv4_hdr.src_addr);
386                                 udp_src_port = &(pkt[j]->udp_hdr.src_port);
387                                 plogx_dbg("ip/port %d.%d.%d.%d / %x found in private ip/port hash\n", IP4(pkt[j]->ipv4_hdr.src_addr), pkt[j]->udp_hdr.src_port);
388                                 *ip_addr = task->private_flow_entries[port_idx].ip_addr;
389                                 *udp_src_port = task->private_flow_entries[port_idx].l4_port;
390                                 uint64_t flow_time = task->private_flow_entries[port_idx].flow_time;
391                                 if (flow_time + tsc_hz < tsc) {
392                                         task->private_flow_entries[port_idx].flow_time = tsc;
393                                 }
394                                 private_ip_idx = task->private_flow_entries[port_idx].private_ip_idx;
395                                 if (task->private_ip_info[private_ip_idx].mac_aging_time + tsc_hz < tsc)
396                                         task->private_ip_info[private_ip_idx].mac_aging_time = tsc;
397                                 prox_ip_udp_cksum(mbufs[j], &pkt[j]->ipv4_hdr, sizeof(struct ether_hdr), sizeof(struct ipv4_hdr), task->offload_crc);
398                                 out[j] =  route_ipv4(task, mbufs[j]);
399                         }
400                 }
401
402                 if (n_new_mapping) {
403                         // Find whether at least IP is already known...
404                         ret = rte_hash_lookup_bulk(task->private_ip_hash, (const void **)&keys, n_new_mapping, positions);
405                         if (unlikely(ret < 0)) {
406                                 plogx_info("lookup_bulk failed for private_ip_hash\n");
407                                 for (int k = 0; k < n_new_mapping; ++k) {
408                                         j = map[k];
409                                         out[j] = OUT_DISCARD;
410                                 }
411                                 n_new_mapping = 0;
412                         }
413                         for (int k = 0; k < n_new_mapping; ++k) {
414                                 private_ip_idx = positions[k];
415                                 j = map[k];
416                                 ip_addr = &(pkt[j]->ipv4_hdr.src_addr);
417                                 proto = pkt[j]->ipv4_hdr.next_proto_id;
418                                 udp_src_port = &(pkt[j]->udp_hdr.src_port);
419                                 int new_ip_entry = 0;
420
421                                 if (unlikely(private_ip_idx < 0)) {
422                                         private_ip = *ip_addr;
423                                         private_port = *udp_src_port;
424                                         plogx_dbg("Did not find private ip %d.%d.%d.%d in ip hash table, looking for new public ip\n", IP4(*ip_addr));
425                                         // IP not found, need to get a new IP/port mapping
426                                         public_ip_idx = get_new_ip(task, &public_ip);
427                                         if (public_ip_idx < 0) {
428                                                 plogx_info("Unable to find new ip/port\n");
429                                                 out[j] = OUT_DISCARD;
430                                                 continue;
431                                         } else {
432                                                 plogx_dbg("found new public ip %d.%d.%d.%d at public IP index %d\n", IP4(public_ip), public_ip_idx);
433                                         }
434                                         private_ip_idx = rte_hash_add_key(task->private_ip_hash, (const void *)ip_addr);
435                                         // The key might be added multiple time - in case the same key was present in the bulk_lookup multiple times
436                                         // As such this is not an issue - the add_key will returns the index as for a new key
437                                         // This scenario should not happen often in real time use case
438                                         // as a for a new flow (flow renewal), probably only one packet will be sent (e.g. TCP SYN)
439                                         if (private_ip_idx < 0) {
440                                                 release_ip(task, &public_ip, public_ip_idx);
441                                                 plogx_info("Unable add ip %d.%d.%d.%d in private ip hash\n", IP4(*ip_addr));
442                                                 out[j] = OUT_DISCARD;
443                                                 continue;
444                                         } else if (task->private_ip_info[private_ip_idx].public_ip) {
445                                                 plogx_info("race condition properly handled : ip %d.%d.%d.%d already in private ip hash\n", IP4(*ip_addr));
446                                                 release_ip(task, &public_ip, public_ip_idx);
447                                                 public_ip = task->private_ip_info[private_ip_idx].public_ip;
448                                                 public_ip_idx = task->private_ip_info[private_ip_idx].public_ip_idx;
449                                         } else {
450                                                 plogx_dbg("Added ip %d.%d.%d.%d in private ip hash\n", IP4(*ip_addr));
451                                                 rte_memcpy(&task->private_ip_info[private_ip_idx].private_mac, ((uint8_t *)pkt) + 6, 6);
452                                                 task->private_ip_info[private_ip_idx].public_ip = public_ip;
453                                                 task->private_ip_info[private_ip_idx].static_entry = 0;
454                                                 task->private_ip_info[private_ip_idx].public_ip_idx = public_ip_idx;
455                                                 new_ip_entry = 1;
456                                         }
457                                 } else {
458                                         public_ip = task->private_ip_info[private_ip_idx].public_ip;
459                                         public_ip_idx = task->private_ip_info[private_ip_idx].public_ip_idx;
460                                 }
461                                 port_idx = add_new_port_entry(task, proto, public_ip_idx, private_ip_idx, *ip_addr, *udp_src_port, mbufs[j], tsc, &public_port);
462                                 if (port_idx < 0) {
463                                         // TODO: delete IP in ip_hash
464                                         if ((new_ip_entry) && (task->last_ip != 0)) {
465                                                 release_ip(task, &public_ip, public_ip_idx);
466                                                 task->last_ip--;
467                                         } else if (new_ip_entry) {
468                                                 release_ip(task, &public_ip, public_ip_idx);
469                                                 task->last_ip = task->public_ip_count-1;
470                                         }
471                                         plogx_info("Failed to add new port entry\n");
472                                         out[j] = OUT_DISCARD;
473                                         continue;
474                                 } else {
475                                         private_ip = *ip_addr;
476                                         private_port = *udp_src_port;
477                                         plogx_info("Added new ip/port: private ip/port = %d.%d.%d.%d/%x public ip/port = %d.%d.%d.%d/%x, index = %d\n", IP4(private_ip), private_port, IP4(public_ip), public_port, port_idx);
478                                 }
479                                 // task->private_flow_entries[port_idx].ip_addr = task->private_ip_info[private_ip_idx].public_ip;
480                                 plogx_info("Added new port: private ip/port = %d.%d.%d.%d/%x, public ip/port = %d.%d.%d.%d/%x\n", IP4(private_ip), private_port, IP4(task->private_ip_info[private_ip_idx].public_ip), public_port);
481                                 *ip_addr = public_ip ;
482                                 *udp_src_port = public_port;
483                                 uint64_t flow_time = task->private_flow_entries[port_idx].flow_time;
484                                 if (flow_time + tsc_hz < tsc) {
485                                         task->private_flow_entries[port_idx].flow_time = tsc;
486                                 }
487                                 if (task->private_ip_info[private_ip_idx].mac_aging_time + tsc_hz < tsc)
488                                         task->private_ip_info[private_ip_idx].mac_aging_time = tsc;
489                                 prox_ip_udp_cksum(mbufs[j], &pkt[j]->ipv4_hdr, sizeof(struct ether_hdr), sizeof(struct ipv4_hdr), task->offload_crc);
490                                 // TODO: if route fails while just added new key in table, should we delete the key from the table?
491                                 out[j] =  route_ipv4(task, mbufs[j]);
492                                 if (out[j] && new_entry) {
493                                         delete_port_entry(task, proto, private_ip, private_port, *ip_addr, *udp_src_port, public_ip_idx);
494                                         plogx_info("Deleted port: private ip/port = %d.%d.%d.%d/%x, public ip/port = %d.%d.%d.%d/%x\n", IP4(private_ip), private_port, IP4(*ip_addr), *udp_src_port);
495                                 }
496                         }
497                 }
498                 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
499         } else {
500                 struct public_key public_key[MAX_PKT_BURST];
501                 for (j = 0; j < n_pkts; ++j) {
502                         /* Currently, only support eth/ipv4 packets */
503                         if (pkt[j]->ether_hdr.ether_type != ETYPE_IPv4) {
504                                 plogx_info("Currently, only support eth/ipv4 packets\n");
505                                 out[j] = OUT_DISCARD;
506                                 keys[j] = (void *)NULL;
507                                 continue;
508                         }
509                         public_key[j].ip_addr = pkt[j]->ipv4_hdr.dst_addr;
510                         public_key[j].l4_port = pkt[j]->udp_hdr.dst_port;
511                         keys[j] = &public_key[j];
512                 }
513                 ret = rte_hash_lookup_bulk(task->public_ip_port_hash, (const void **)&keys, n_pkts, positions);
514                 if (ret < 0) {
515                         plogx_err("Failed lookup bulk public_ip_port_hash\n");
516                         return -1;
517                 }
518                 for (j = 0; j < n_pkts; ++j) {
519                         port_idx = positions[j];
520                         ip_addr = &(pkt[j]->ipv4_hdr.dst_addr);
521                         udp_src_port = &(pkt[j]->udp_hdr.dst_port);
522                         if (port_idx < 0) {
523                                 plogx_err("Failed to find ip/port %d.%d.%d.%d/%x in public_ip_port_hash\n", IP4(*ip_addr), *udp_src_port);
524                                 out[j] = OUT_DISCARD;
525                         } else {
526                                 plogx_dbg("Found ip/port %d.%d.%d.%d/%x in public_ip_port_hash\n", IP4(*ip_addr), *udp_src_port);
527                                 *ip_addr = task->public_entries[port_idx].ip_addr;
528                                 *udp_src_port = task->public_entries[port_idx].l4_port;
529                                 private_ip_idx = task->public_entries[port_idx].private_ip_idx;
530                                 plogx_dbg("Found private IP info for ip %d.%d.%d.%d\n", IP4(*ip_addr));
531                                 rte_memcpy(((uint8_t *)(pkt[j])) + 0, &task->private_ip_info[private_ip_idx].private_mac, 6);
532                                 rte_memcpy(((uint8_t *)(pkt[j])) + 6, &task->src_mac_from_dpdk_port[task->public_entries[port_idx].dpdk_port], 6);
533                                 out[j] = task->public_entries[port_idx].dpdk_port;
534                         }
535                         prox_ip_udp_cksum(mbufs[j], &pkt[j]->ipv4_hdr, sizeof(struct ether_hdr), sizeof(struct ipv4_hdr), task->offload_crc);
536                 }
537                 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
538         }
539
540 }
541
542 static int lua_to_hash_nat(struct task_args *targ, struct lua_State *L, enum lua_place from, const char *name, uint8_t socket)
543 {
544         struct rte_hash *tmp_priv_ip_hash, *tmp_priv_hash, *tmp_pub_hash;
545         struct private_flow_entry *tmp_priv_flow_entries;
546         struct public_entry *tmp_pub_entries;
547         uint32_t n_entries = 0;;
548         uint32_t ip_from, ip_to;
549         uint16_t port_from, port_to;
550         int ret, idx, pop, pop2, pop3, n_static_entries = 0;
551         uint32_t dst_ip1, dst_ip2;
552         struct val_range dst_port;
553         struct public_ip_config_info *ip_info;
554         struct public_ip_config_info *tmp_public_ip_config_info;
555
556         if ((pop = lua_getfrom(L, from, name)) < 0)
557                 return -1;
558
559         if (!lua_istable(L, -1)) {
560                 plogx_err("Can't read cgnat since data is not a table\n");
561                 return -1;
562         }
563
564         struct tmp_public_ip {
565                 uint32_t ip_beg;
566                 uint32_t ip_end;
567                 uint16_t port_beg;
568                 uint16_t port_end;
569         };
570         struct tmp_static_ip {
571                 uint32_t private_ip;
572                 uint32_t public_ip;
573         };
574         struct tmp_static_ip_port {
575                 uint32_t private_ip;
576                 uint32_t public_ip;
577                 uint32_t n_ports;
578                 uint16_t private_port;
579                 uint16_t public_port;
580                 int ip_found;
581                 uint8_t port_found;
582         };
583         uint32_t n_public_groups = 0;
584         uint32_t n_public_ip = 0;
585         uint32_t n_static_ip = 0;
586         uint32_t n_static_ip_port = 0;
587         unsigned int i = 0;
588         struct tmp_public_ip *tmp_public_ip = NULL;
589         struct tmp_static_ip *tmp_static_ip = NULL;
590         struct tmp_static_ip_port *tmp_static_ip_port = NULL;
591
592         // Look for Dynamic entries configuration
593         plogx_info("Reading dynamic NAT table\n");
594         if ((pop2 = lua_getfrom(L, TABLE, "dynamic")) < 0) {
595                 plogx_info("No dynamic table found\n");
596         } else {
597                 uint64_t n_ip, n_port;
598                 if (!lua_istable(L, -1)) {
599                         plogx_err("Can't read cgnat since data is not a table\n");
600                         return -1;
601                 }
602                 lua_len(L, -1);
603                 n_public_groups = lua_tointeger(L, -1);
604                 plogx_info("%d groups of public IP\n", n_public_groups);
605                 tmp_public_ip = (struct tmp_public_ip *)malloc(n_public_groups * sizeof(struct tmp_public_ip));
606                 PROX_PANIC(tmp_public_ip == NULL, "Failed to allocated tmp_public_ip\n");
607                 lua_pop(L, 1);
608                 lua_pushnil(L);
609
610                 while (lua_next(L, -2)) {
611                         if (lua_to_ip(L, TABLE, "public_ip_range_start", &dst_ip1) ||
612                                 lua_to_ip(L, TABLE, "public_ip_range_stop", &dst_ip2) ||
613                                 lua_to_val_range(L, TABLE, "public_port", &dst_port))
614                                         return -1;
615                         PROX_PANIC(dst_ip2 < dst_ip1, "public_ip_range error: %d.%d.%d.%d < %d.%d.%d.%d\n", (dst_ip2 >> 24), (dst_ip2 >> 16) & 0xFF, (dst_ip2 >> 8) & 0xFF, dst_ip2 & 0xFF, dst_ip1 >> 24, (dst_ip1 >> 16) & 0xFF, (dst_ip1 >> 8) & 0xFF, dst_ip1 & 0xFF);
616                         PROX_PANIC(dst_port.end < dst_port.beg, "public_port error: %d < %d\n", dst_port.end, dst_port.beg);
617                         n_ip = dst_ip2 - dst_ip1 + 1;
618                         n_port =  dst_port.end - dst_port.beg + 1;
619                         n_public_ip += n_ip;
620                         plogx_info("Found IP from %d.%d.%d.%d to %d.%d.%d.%d and port from %d to %d\n", dst_ip1 >> 24, (dst_ip1 >> 16) & 0xFF, (dst_ip1 >> 8) & 0xFF, dst_ip1 & 0xFF, (dst_ip2 >> 24), (dst_ip2 >> 16) & 0xFF, (dst_ip2 >> 8) & 0xFF, dst_ip2 & 0xFF, dst_port.beg, dst_port.end);
621                         tmp_public_ip[i].ip_beg = dst_ip1;
622                         tmp_public_ip[i].ip_end = dst_ip2;
623                         tmp_public_ip[i].port_beg = dst_port.beg;
624                         tmp_public_ip[i++].port_end = dst_port.end;
625                         n_entries += n_ip * n_port;
626                         lua_pop(L, 1);
627                 }
628                 lua_pop(L, pop2);
629
630         }
631         i = 0;
632         if ((pop2 = lua_getfrom(L, TABLE, "static_ip")) < 0) {
633                 plogx_info("No static ip table found\n");
634         } else {
635                 if (!lua_istable(L, -1)) {
636                         plogx_err("Can't read cgnat since data is not a table\n");
637                         return -1;
638                 }
639
640                 lua_len(L, -1);
641                 n_static_ip = lua_tointeger(L, -1);
642                 plogx_info("%d entries in static ip table\n", n_static_ip);
643                 lua_pop(L, 1);
644                 tmp_static_ip = (struct tmp_static_ip *)malloc(n_static_ip * sizeof(struct tmp_static_ip));
645                 PROX_PANIC(tmp_static_ip == NULL, "Failed to allocated tmp_static_ip\n");
646                 lua_pushnil(L);
647                 while (lua_next(L, -2)) {
648                         if (lua_to_ip(L, TABLE, "src_ip", &ip_from) ||
649                                 lua_to_ip(L, TABLE, "dst_ip", &ip_to))
650                                         return -1;
651                         ip_from = rte_bswap32(ip_from);
652                         ip_to = rte_bswap32(ip_to);
653                         tmp_static_ip[i].private_ip = ip_from;
654                         tmp_static_ip[i++].public_ip = ip_to;
655                         for (unsigned int j = 0; j < n_public_groups; j++) {
656                                 if ((tmp_public_ip[j].ip_beg <= ip_to) && (ip_to <= tmp_public_ip[j].ip_end)) {
657                                         PROX_PANIC(1, "list of static ip mapping overlap with list of dynamic IP => not supported yet\n");
658                                 }
659                         }
660                         n_public_ip++;
661                         lua_pop(L, 1);
662                 }
663                 lua_pop(L, pop2);
664         }
665
666         i = 0;
667         if ((pop2 = lua_getfrom(L, TABLE, "static_ip_port")) < 0) {
668                 plogx_info("No static table found\n");
669         } else {
670                 if (!lua_istable(L, -1)) {
671                         plogx_err("Can't read cgnat since data is not a table\n");
672                         return -1;
673                 }
674
675                 lua_len(L, -1);
676                 n_static_ip_port = lua_tointeger(L, -1);
677                 plogx_info("%d entries in static table\n", n_static_ip_port);
678                 lua_pop(L, 1);
679                 tmp_static_ip_port = (struct tmp_static_ip_port *)malloc(n_static_ip_port * sizeof(struct tmp_static_ip_port));
680                 PROX_PANIC(tmp_static_ip_port == NULL, "Failed to allocated tmp_static_ip_port\n");
681                 lua_pushnil(L);
682
683                 while (lua_next(L, -2)) {
684                         if (lua_to_ip(L, TABLE, "src_ip", &ip_from) ||
685                                 lua_to_ip(L, TABLE, "dst_ip", &ip_to) ||
686                                 lua_to_port(L, TABLE, "src_port", &port_from) ||
687                                 lua_to_port(L, TABLE, "dst_port", &port_to))
688                                         return -1;
689
690                         ip_from = rte_bswap32(ip_from);
691                         ip_to = rte_bswap32(ip_to);
692                         port_from = rte_bswap16(port_from);
693                         port_to = rte_bswap16(port_to);
694                         tmp_static_ip_port[i].private_ip = ip_from;
695                         tmp_static_ip_port[i].public_ip = ip_to;
696                         tmp_static_ip_port[i].private_port = port_from;
697                         tmp_static_ip_port[i].public_port = port_to;
698                         tmp_static_ip_port[i].n_ports = 1;
699                         for (unsigned int j = 0; j < n_public_groups; j++) {
700                                 if ((tmp_public_ip[j].ip_beg <= rte_bswap32(ip_to)) && (rte_bswap32(ip_to) <= tmp_public_ip[j].ip_end)) {
701                                         tmp_static_ip_port[i].ip_found = j + 11;
702                                         PROX_PANIC(1, "list of static ip/port mapping overlap with list of dynamic IP => not supported yet\n");
703                                 }
704                         }
705                         for (unsigned int j = 0; j < n_static_ip; j++) {
706                                 if ((tmp_static_ip[j].public_ip == ip_to) ) {
707                                         tmp_static_ip_port[i].ip_found = j + 1;
708                                         PROX_PANIC(1, "list of static ip/port mapping overlap with list of static ip => not supported yet\n");
709                                 }
710                         }
711                         for (unsigned int j = 0; j <= i; j++) {
712                                 if (ip_to == tmp_static_ip_port[j].public_ip) {
713                                         tmp_static_ip_port[i].ip_found = j + 1;
714                                         tmp_static_ip_port[j].n_ports++;
715                                         tmp_static_ip_port[i].n_ports = 0;
716                                 }
717                         }
718                         i++;
719                         if (!tmp_static_ip_port[i].ip_found) {
720                                 n_public_ip++;
721                                 n_entries++;
722                         }
723                         lua_pop(L, 1);
724                 }
725                 lua_pop(L, pop2);
726         }
727         lua_pop(L, pop);
728
729         tmp_public_ip_config_info = (struct public_ip_config_info *)prox_zmalloc(n_public_ip * sizeof(struct public_ip_config_info), socket);
730         PROX_PANIC(tmp_public_ip_config_info == NULL, "Failed to allocate PUBLIC IP INFO\n");
731         plogx_info("%d PUBLIC IP INFO allocated\n", n_public_ip);
732
733         struct private_ip_info *tmp_priv_ip_info = (struct private_ip_info *)prox_zmalloc(4 * n_public_ip * sizeof(struct public_ip_config_info), socket);
734         PROX_PANIC(tmp_priv_ip_info == NULL, "Failed to allocate PRIVATE IP INFO\n");
735         plogx_info("%d PRIVATE IP INFO allocated\n", 4 * n_public_ip);
736
737         uint32_t ip_free_count = 0;
738         for (i = 0; i < n_public_groups; i++) {
739                 for (uint32_t ip = tmp_public_ip[i].ip_beg; ip <= tmp_public_ip[i].ip_end; ip++) {
740                         ip_info = &tmp_public_ip_config_info[ip_free_count];
741                         ip_info->public_ip = rte_bswap32(ip);
742                         ip_info->port_list = (uint16_t *)prox_zmalloc((dst_port.end - dst_port.beg) * sizeof(uint16_t), socket);
743                         PROX_PANIC(ip_info->port_list == NULL, "Failed to allocate list of ports for ip %x\n", ip);
744                         for (uint32_t port = tmp_public_ip[i].port_beg; port <= tmp_public_ip[i].port_end; port++) {
745                                 ip_info->port_list[ip_info->port_free_count] = rte_bswap16(port);
746                                 ip_info->port_free_count++;
747                         }
748                         ip_info->max_port_count = ip_info->port_free_count;
749                         plogx_dbg("Added IP %d.%d.%d.%d with ports from %x to %x at index %x\n", IP4(ip_info->public_ip), tmp_public_ip[i].port_beg, tmp_public_ip[i].port_end, ip_free_count);
750                         ip_free_count++;
751                 }
752         }
753         uint32_t public_ip_count = ip_free_count;
754         for (i = 0; i < n_static_ip; i++) {
755                 ip_info = &tmp_public_ip_config_info[ip_free_count];
756                 ip_info->public_ip = tmp_static_ip[i].public_ip;
757                 ip_info->port_list = NULL;
758                 ip_info->max_port_count = 0;
759                 ip_free_count++;
760         }
761         for (i = 0; i < n_static_ip_port; i++) {
762                 if (!tmp_static_ip_port[i].ip_found) {
763                         ip_info = &tmp_public_ip_config_info[ip_free_count];
764                         ip_info->public_ip = tmp_static_ip_port[i].public_ip;
765                         ip_info->port_list = (uint16_t *)prox_zmalloc(tmp_static_ip_port[i].n_ports * sizeof(uint16_t), socket);
766                         PROX_PANIC(ip_info->port_list == NULL, "Failed to allocate list of ports for ip %x\n", tmp_static_ip_port[i].public_ip);
767                         ip_info->port_list[ip_info->port_free_count] = tmp_static_ip_port[i].public_port;
768                         ip_info->port_free_count++;
769                         ip_info->max_port_count = ip_info->port_free_count;
770                         ip_free_count++;
771                 } else {
772                         for (unsigned j = 0; j < ip_free_count; j++) {
773                                 ip_info = &tmp_public_ip_config_info[j];
774                                 if (ip_info->public_ip == tmp_static_ip_port[i].public_ip) {
775                                         ip_info = &tmp_public_ip_config_info[j];
776                                         ip_info->port_list[ip_info->port_free_count] = tmp_static_ip_port[i].public_port;
777                                         ip_info->port_free_count++;
778                                         ip_info->max_port_count = ip_info->port_free_count;
779                                         break;
780                                 }
781                         }
782                 }
783         }
784         plogx_info("%d entries in dynamic table\n", n_entries);
785
786         n_entries = n_entries * 4;
787         static char hash_name[30];
788         sprintf(hash_name, "A%03d_hash_nat_table", targ->lconf->id);
789         struct rte_hash_parameters hash_params = {
790                 .name = hash_name,
791                 .entries = n_entries,
792                 .key_len = sizeof(struct private_key),
793                 .hash_func = rte_hash_crc,
794                 .hash_func_init_val = 0,
795         };
796         plogx_info("hash table name = %s\n", hash_params.name);
797         struct private_key private_key;
798         struct public_key public_key;
799         tmp_priv_hash = rte_hash_create(&hash_params);
800         PROX_PANIC(tmp_priv_hash == NULL, "Failed to set up private hash table for NAT\n");
801         plogx_info("private hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
802
803         tmp_priv_flow_entries = (struct private_flow_entry *)prox_zmalloc(n_entries * sizeof(struct private_flow_entry), socket);
804         PROX_PANIC(tmp_priv_flow_entries == NULL, "Failed to allocate memory for private NAT %u entries\n", n_entries);
805         plogx_info("private data allocated, with %d entries of size %ld\n", n_entries, sizeof(struct private_flow_entry));
806
807         hash_name[0]++;
808         //hash_params.name[0]++;
809         plogx_info("hash table name = %s\n", hash_params.name);
810         hash_params.key_len = sizeof(uint32_t);
811         hash_params.entries = 4 * ip_free_count;
812         tmp_priv_ip_hash = rte_hash_create(&hash_params);
813         PROX_PANIC(tmp_priv_ip_hash == NULL, "Failed to set up private ip hash table for NAT\n");
814         plogx_info("private ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
815
816         hash_name[0]++;
817         //hash_params.name[0]++;
818         plogx_info("hash table name = %s\n", hash_params.name);
819         hash_params.entries = n_entries;
820         hash_params.key_len = sizeof(struct public_key),
821         tmp_pub_hash = rte_hash_create(&hash_params);
822         PROX_PANIC(tmp_pub_hash == NULL, "Failed to set up public hash table for NAT\n");
823         plogx_info("public hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
824
825         hash_name[0]++;
826         //hash_params.name[0]++;
827         tmp_pub_entries = (struct public_entry *)prox_zmalloc(n_entries * sizeof(struct public_entry), socket);
828         PROX_PANIC(tmp_pub_entries == NULL, "Failed to allocate memory for public NAT %u entries\n", n_entries);
829         plogx_info("public data allocated, with %d entries of size %ld\n", n_entries, sizeof(struct private_flow_entry));
830
831         for (i = 0; i < n_static_ip_port; i++) {
832                 ip_to = tmp_static_ip_port[i].public_ip;
833                 ip_from = tmp_static_ip_port[i].private_ip;
834                 port_to = tmp_static_ip_port[i].public_port;
835                 port_from = tmp_static_ip_port[i].private_port;
836                 private_key.ip_addr = ip_from;
837                 private_key.l4_port = port_from;
838                 ret = rte_hash_lookup(tmp_priv_hash, (const void *)&private_key);
839                 PROX_PANIC(ret >= 0, "Key %x %x already exists in NAT private hash table\n", ip_from, port_from);
840
841                 idx = rte_hash_add_key(tmp_priv_ip_hash, (const void *)&ip_from);
842                 PROX_PANIC(idx < 0, "Failed to add ip %x to NAT private hash table\n", ip_from);
843                 ret = rte_hash_add_key(tmp_priv_hash, (const void *)&private_key);
844                 PROX_PANIC(ret < 0, "Failed to add Key %x %x to NAT private hash table\n", ip_from, port_from);
845                 tmp_priv_flow_entries[ret].ip_addr = ip_to;
846                 tmp_priv_flow_entries[ret].flow_time = -1;
847                 tmp_priv_flow_entries[ret].private_ip_idx = idx;
848                 tmp_priv_flow_entries[ret].l4_port = port_to;
849
850                 public_key.ip_addr = ip_to;
851                 public_key.l4_port = port_to;
852                 ret = rte_hash_lookup(tmp_pub_hash, (const void *)&public_key);
853                 PROX_PANIC(ret >= 0, "Key %d.%d.%d.%d port %x (for private IP %d.%d.%d.%d port %x) already exists in NAT public hash table fir IP %d.%d.%d.%d port %x\n", IP4(ip_to), port_to, IP4(ip_from), port_from, IP4(tmp_pub_entries[ret].ip_addr), tmp_pub_entries[ret].l4_port);
854
855                 ret = rte_hash_add_key(tmp_pub_hash, (const void *)&public_key);
856                 PROX_PANIC(ret < 0, "Failed to add Key %x %x to NAT public hash table\n", ip_to, port_to);
857                 tmp_pub_entries[ret].ip_addr = ip_from;
858                 tmp_pub_entries[ret].l4_port = port_from;
859                 tmp_pub_entries[ret].private_ip_idx = idx;
860         }
861
862         for (uint8_t task_id = 0; task_id < targ->lconf->n_tasks_all; ++task_id) {
863                 struct task_args *target_targ = (struct task_args *)&(targ->lconf->targs[task_id]);
864                 enum task_mode smode = target_targ->mode;
865                 if (CGNAT == smode) {
866                         target_targ->public_ip_count = public_ip_count;
867                         target_targ->private_ip_hash = tmp_priv_ip_hash;
868                         target_targ->private_ip_port_hash = tmp_priv_hash;
869                         target_targ->private_ip_info = tmp_priv_ip_info;
870                         target_targ->private_flow_entries = tmp_priv_flow_entries;
871                         target_targ->public_ip_port_hash = tmp_pub_hash;
872                         target_targ->public_entries = tmp_pub_entries;
873                         target_targ->public_ip_config_info = tmp_public_ip_config_info;
874                 }
875         }
876         return 0;
877 }
878
879 static void early_init_task_nat(struct task_args *targ)
880 {
881         int ret;
882         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
883         if (!targ->private_ip_hash) {
884                 ret = lua_to_hash_nat(targ, prox_lua(), GLOBAL, targ->nat_table, socket_id);
885                 PROX_PANIC(ret != 0, "Failed to load NAT table from lua:\n%s\n", get_lua_to_errors());
886         }
887 }
888
889 static void init_task_nat(struct task_base *tbase, struct task_args *targ)
890 {
891         struct task_nat *task = (struct task_nat *)tbase;
892         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
893
894         /* Use destination IP by default. */
895         task->private = targ->use_src;
896
897         PROX_PANIC(!strcmp(targ->nat_table, ""), "No nat table specified\n");
898         task->lconf = targ->lconf;
899         task->runtime_flags = targ->runtime_flags;
900
901         task->public_ip_count = targ->public_ip_count;
902         task->last_ip = targ->public_ip_count;
903         task->private_ip_hash = targ->private_ip_hash;
904         task->private_ip_port_hash = targ->private_ip_port_hash;
905         task->private_ip_info = targ->private_ip_info;
906         task->private_flow_entries = targ->private_flow_entries;
907         task->public_ip_port_hash = targ->public_ip_port_hash;
908         task->public_entries = targ->public_entries;
909         task->public_ip_config_info = targ->public_ip_config_info;
910
911         proto_ipsrc_portsrc_mask = _mm_set_epi32(BIT_0_TO_15, 0, ALL_32_BITS, BIT_8_TO_15);
912         proto_ipdst_portdst_mask = _mm_set_epi32(BIT_16_TO_31, ALL_32_BITS, 0, BIT_8_TO_15);
913
914         struct lpm4 *lpm;
915
916         PROX_PANIC(!strcmp(targ->route_table, ""), "route table not specified\n");
917         if (targ->flags & TASK_ARG_LOCAL_LPM) {
918                 int ret = lua_to_lpm4(prox_lua(), GLOBAL, targ->route_table, socket_id, &lpm);
919                 PROX_PANIC(ret, "Failed to load IPv4 LPM:\n%s\n", get_lua_to_errors());
920                 prox_sh_add_socket(socket_id, targ->route_table, lpm);
921                 task->number_free_rules = lpm->n_free_rules;
922         } else {
923                 lpm = prox_sh_find_socket(socket_id, targ->route_table);
924                 if (!lpm) {
925                         int ret = lua_to_lpm4(prox_lua(), GLOBAL, targ->route_table, socket_id, &lpm);
926                         PROX_PANIC(ret, "Failed to load IPv4 LPM:\n%s\n", get_lua_to_errors());
927                         prox_sh_add_socket(socket_id, targ->route_table, lpm);
928                 }
929         }
930         task->ipv4_lpm = lpm->rte_lpm;
931         task->next_hops = lpm->next_hops;
932         task->number_free_rules = lpm->n_free_rules;
933
934         for (uint32_t i = 0; i < MAX_HOP_INDEX; i++) {
935                 int tx_port = task->next_hops[i].mac_port.out_idx;
936                 if ((tx_port > targ->nb_txports - 1) && (tx_port > targ->nb_txrings - 1)) {
937                         PROX_PANIC(1, "Routing Table contains port %d but only %d tx port/ %d ring:\n", tx_port, targ->nb_txports, targ->nb_txrings);
938                 }
939         }
940
941         if (targ->nb_txrings) {
942                 struct task_args *dtarg;
943                 struct core_task ct;
944                 for (uint32_t i = 0; i < targ->nb_txrings; ++i) {
945                         ct = targ->core_task_set[0].core_task[i];
946                         dtarg = core_targ_get(ct.core, ct.task);
947                         dtarg = find_reachable_task_sending_to_port(dtarg);
948                         task->src_mac[i] = (0x0000ffffffffffff & ((*(uint64_t*)&prox_port_cfg[dtarg->tx_port_queue[0].port].eth_addr))) | ((uint64_t)ETYPE_IPv4 << (64 - 16));
949                         task->src_mac_from_dpdk_port[dtarg->tx_port_queue[0].port] = task->src_mac[i];
950                         plogx_dbg("src_mac = %lx for port %d %d\n", task->src_mac[i], i, dtarg->tx_port_queue[0].port);
951                 }
952         } else {
953                 for (uint32_t i = 0; i < targ->nb_txports; ++i) {
954                         task->src_mac[i] = (0x0000ffffffffffff & ((*(uint64_t*)&prox_port_cfg[targ->tx_port_queue[i].port].eth_addr))) | ((uint64_t)ETYPE_IPv4 << (64 - 16));
955                         task->src_mac_from_dpdk_port[targ->tx_port_queue[0].port] = task->src_mac[i];
956                         plogx_dbg("src_mac = %lx for port %d %d\n", task->src_mac[i], i, targ->tx_port_queue[i].port);
957                 }
958         }
959
960         struct prox_port_cfg *port = find_reachable_port(targ);
961         if (port) {
962                 task->offload_crc = port->requested_tx_offload & (DEV_TX_OFFLOAD_IPV4_CKSUM | DEV_TX_OFFLOAD_UDP_CKSUM);
963         }
964 }
965
966 /* Basic static nat. */
967 static struct task_init task_init_nat = {
968         .mode = CGNAT,
969         .mode_str = "cgnat",
970         .early_init = early_init_task_nat,
971         .init = init_task_nat,
972         .handle = handle_nat_bulk,
973 #ifdef SOFT_CRC
974         .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS|TASK_FEATURE_ROUTING|TASK_FEATURE_ZERO_RX,
975 #else
976         .flag_features = TASK_FEATURE_ROUTING|TASK_FEATURE_ZERO_RX,
977 #endif
978         .size = sizeof(struct task_nat),
979 };
980
981 __attribute__((constructor)) static void reg_task_nat(void)
982 {
983         reg_task(&task_init_nat);
984 }