Added support for reporting packet (mis)order.
[samplevnf.git] / VNFs / DPPD-PROX / handle_master.c
1 /*
2 // Copyright (c) 2010-2020 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 <fcntl.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <linux/netlink.h>
21 #include <linux/rtnetlink.h>
22 #include <net/if.h>
23
24 #include <rte_hash.h>
25 #include <rte_hash_crc.h>
26 #include <rte_ether.h>
27 #include <rte_icmp.h>
28
29 #include "prox_cfg.h"
30 #include "prox_globals.h"
31 #include "rx_pkt.h"
32 #include "arp.h"
33 #include "handle_master.h"
34 #include "log.h"
35 #include "mbuf_utils.h"
36 #include "etypes.h"
37 #include "defaults.h"
38 #include "prox_malloc.h"
39 #include "quit.h"
40 #include "task_init.h"
41 #include "prox_port_cfg.h"
42 #include "main.h"
43 #include "lconf.h"
44 #include "input.h"
45 #include "tx_pkt.h"
46 #include "defines.h"
47 #include "prox_ipv6.h"
48 #include "packet_utils.h"
49
50 #define PROX_MAX_ARP_REQUESTS   32      // Maximum number of tasks requesting the same MAC address
51 #define NETLINK_BUF_SIZE        16384
52
53 static char netlink_buf[NETLINK_BUF_SIZE];
54
55 const char *actions_string[] = {
56         "MAC_INFO_FROM_MASTER",         // Controlplane sending a MAC update to dataplane
57         "MAC_INFO_FROM_MASTER_FOR_IPV6",// Controlplane sending a MAC update to dataplane
58         "IPV6_INFO_FROM_MASTER",        // Controlplane IPv6 Global IP info to dataplane
59         "ROUTE_ADD_FROM_MASTER",        // Controlplane sending a new route to dataplane
60         "ROUTE_DEL_FROM_MASTER",        // Controlplane deleting a new route from dataplane
61         "SEND_ARP_REQUEST_FROM_MASTER", // Controlplane requesting dataplane to send ARP request
62         "SEND_ARP_REPLY_FROM_MASTER",   // Controlplane requesting dataplane to send ARP reply
63         "SEND_NDP_FROM_MASTER",         // Controlplane requesting dataplane to send NDP
64         "SEND_ICMP_FROM_MASTER",        // Controlplane requesting dataplane to send ICMP message
65         "SEND_BGP_FROM_MASTER",         // Controlplane requesting dataplane to send BGP message
66         "ARP_PKT_FROM_NET_TO_MASTER",   // ARP sent by datplane to Controlpane for handling
67         "NDP_PKT_FROM_NET_TO_MASTER,"   // NDP sent by datplane to Controlpane for handling
68         "ICMP_TO_MASTER",               // ICMP sent by datplane to Controlpane for handling
69         "BGP_TO_MASTER"                 // BGP sent by datplane to Controlpane for handling
70         "IP4_REQ_MAC_TO_MASTER",        // Dataplane requesting MAC resolution to Controlplane
71         "IP6_REQ_MAC_TO_MASTER",        // Dataplane requesting MAC resolution to Controlplane
72         "PKT_FROM_TAP"                  // Packet received by Controlplane from kernel and forwarded to dataplane for sending
73
74 };
75
76 static struct my_arp_t arp_reply = {
77         .htype = 0x100,
78         .ptype = 8,
79         .hlen = 6,
80         .plen = 4,
81         .oper = 0x200
82 };
83 static struct my_arp_t arp_request = {
84         .htype = 0x100,
85         .ptype = 8,
86         .hlen = 6,
87         .plen = 4,
88         .oper = 0x100
89 };
90
91 struct ip_port {
92         uint32_t ip;
93         uint8_t port;
94 } __attribute__((packed));
95
96 struct ip6_port {
97         struct ipv6_addr ip6;
98         uint8_t port;
99 } __attribute__((packed));
100
101 void register_router_to_ctrl_plane(struct task_base *tbase, uint8_t port_id, uint8_t core_id, uint8_t task_id, struct ipv6_addr *local_ipv6_addr, struct ipv6_addr *global_ipv6_addr, struct ipv6_addr *router_prefix)
102 {
103         struct task_master *task = (struct task_master *)tbase;
104         task->internal_port_table[port_id].flags |= IPV6_ROUTER;
105         memcpy(&task->internal_port_table[port_id].router_prefix, router_prefix, sizeof(struct ipv6_addr));
106         register_node_to_ctrl_plane(tbase, local_ipv6_addr, global_ipv6_addr, port_id, core_id, task_id);
107 }
108
109 void register_node_to_ctrl_plane(struct task_base *tbase, struct ipv6_addr *local_ipv6_addr, struct ipv6_addr *global_ipv6_addr, uint8_t port_id, uint8_t core_id, uint8_t task_id)
110 {
111         struct task_master *task = (struct task_master *)tbase;
112         if (task->internal_port_table[port_id].flags & IPV6_ROUTER)
113                 plogx_dbg("\tregistering router with port %d core %d and task %d\n", port_id, core_id, task_id);
114         else
115                 plogx_dbg("\tregistering node with port %d core %d and task %d\n", port_id, core_id, task_id);
116
117         if (port_id >= PROX_MAX_PORTS) {
118                 plog_err("Unable to register router, port %d\n", port_id);
119                 return;
120         }
121         task->internal_port_table[port_id].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
122         memcpy(&task->internal_port_table[port_id].mac, &prox_port_cfg[port_id].eth_addr, sizeof(prox_rte_ether_addr));
123         memcpy(&task->internal_port_table[port_id].local_ipv6_addr, local_ipv6_addr, sizeof(struct ipv6_addr));
124         if (memcmp(local_ipv6_addr, &prox_cfg.random_ip, sizeof(struct ipv6_addr)) == 0) {
125                 task->internal_port_table[port_id].flags |= HANDLE_RANDOM_LOCAL_IP_FLAG;
126                 return;
127         }
128         memcpy(&task->internal_port_table[port_id].global_ipv6_addr, global_ipv6_addr, sizeof(struct ipv6_addr));
129         if (memcmp(global_ipv6_addr, &prox_cfg.random_ip, sizeof(struct ipv6_addr)) == 0) {
130                 task->internal_port_table[port_id].flags |= HANDLE_RANDOM_GLOBAL_IP_FLAG;
131                 return;
132         }
133         struct ip6_port key;
134         memcpy(&key.ip6, local_ipv6_addr, sizeof(struct ipv6_addr));
135         key.port = port_id;
136         int ret = rte_hash_add_key(task->internal_ip6_hash, (const void *)&key);
137         if (unlikely(ret < 0)) {
138                 plog_err("Unable to register ip "IPv6_BYTES_FMT"\n", IPv6_BYTES(local_ipv6_addr->bytes));
139                 return;
140         }
141         memcpy(&key.ip6, global_ipv6_addr, sizeof(struct ipv6_addr));
142         ret = rte_hash_add_key(task->internal_ip6_hash, (const void *)&key);
143         if (unlikely(ret < 0)) {
144                 plog_err("Unable to register ip "IPv6_BYTES_FMT"\n", IPv6_BYTES(global_ipv6_addr->bytes));
145                 return;
146         }
147         memcpy(&task->internal_ip6_table[ret].mac, &prox_port_cfg[port_id].eth_addr, sizeof(prox_rte_ether_addr));
148         task->internal_ip6_table[ret].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
149 }
150
151 void master_init_vdev(struct task_base *tbase, uint8_t port_id, uint8_t core_id, uint8_t task_id)
152 {
153         struct task_master *task = (struct task_master *)tbase;
154         uint8_t vdev_port = prox_port_cfg[port_id].dpdk_mapping;
155         int rc, i;
156         if (vdev_port != NO_VDEV_PORT) {
157                 for (i = 0; i < task->max_vdev_id; i++) {
158                         if (task->all_vdev[i].port_id == vdev_port)
159                                 break;
160                 }
161                 if (i <  task->max_vdev_id) {
162                         // Already initialized (e.g. by another core handling the same port).
163                         return;
164                 }
165                 task->all_vdev[task->max_vdev_id].port_id = vdev_port;
166                 task->all_vdev[task->max_vdev_id].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
167
168                 struct sockaddr_in dst, src;
169                 src.sin_family = AF_INET;
170                 src.sin_addr.s_addr = prox_port_cfg[vdev_port].ip;
171                 src.sin_port = rte_cpu_to_be_16(PROX_PSEUDO_PKT_PORT);
172
173                 int fd = socket(AF_INET,  SOCK_DGRAM, 0);
174                 PROX_PANIC(fd < 0, "Failed to open socket(AF_INET,  SOCK_DGRAM, 0)\n");
175                 prox_port_cfg[vdev_port].fd = fd;
176                 rc = bind(fd,(struct sockaddr *)&src, sizeof(struct sockaddr_in));
177                 PROX_PANIC(rc, "Failed to bind("IPv4_BYTES_FMT":%d): errno = %d (%s)\n", IPv4_BYTES(((uint8_t*)&src.sin_addr.s_addr)), src.sin_port, errno, strerror(errno));
178                 plog_info("DPDK port %d bound("IPv4_BYTES_FMT":%d) to fd %d\n", port_id, IPv4_BYTES(((uint8_t*)&src.sin_addr.s_addr)), src.sin_port, fd);
179                 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
180                 task->max_vdev_id++;
181         }
182 }
183
184 void register_ip_to_ctrl_plane(struct task_base *tbase, uint32_t ip, uint8_t port_id, uint8_t core_id, uint8_t task_id)
185 {
186         struct task_master *task = (struct task_master *)tbase;
187         struct ip_port key;
188         plogx_info("\tregistering IP "IPv4_BYTES_FMT" with port %d core %d and task %d\n", IP4(ip), port_id, core_id, task_id);
189
190         if (port_id >= PROX_MAX_PORTS) {
191                 plog_err("Unable to register ip "IPv4_BYTES_FMT", port %d\n", IP4(ip), port_id);
192                 return;
193         }
194
195         /* TODO - store multiple rings if multiple cores able to handle IP
196            Remove them when such cores are stopped and de-register IP
197         */
198         task->internal_port_table[port_id].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
199         memcpy(&task->internal_port_table[port_id].mac, &prox_port_cfg[port_id].eth_addr, sizeof(prox_rte_ether_addr));
200         task->internal_port_table[port_id].ip = ip;
201
202         if (ip == RANDOM_IP) {
203                 task->internal_port_table[port_id].flags |= HANDLE_RANDOM_IP_FLAG;
204                 return;
205         }
206
207         key.ip = ip;
208         key.port = port_id;
209         int ret = rte_hash_add_key(task->internal_ip_hash, (const void *)&key);
210         if (unlikely(ret < 0)) {
211                 plog_err("Unable to register ip "IPv4_BYTES_FMT"\n", IP4(ip));
212                 return;
213         }
214         memcpy(&task->internal_ip_table[ret].mac, &prox_port_cfg[port_id].eth_addr, sizeof(prox_rte_ether_addr));
215         task->internal_ip_table[ret].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
216 }
217
218 static inline void handle_arp_reply(struct task_base *tbase, struct rte_mbuf *mbuf, struct my_arp_t *arp)
219 {
220         struct task_master *task = (struct task_master *)tbase;
221         int i, ret;
222         uint32_t key = arp->data.spa;
223         plogx_dbg("\tMaster handling ARP reply for ip "IPv4_BYTES_FMT"\n", IP4(key));
224
225         ret = rte_hash_lookup(task->external_ip_hash, (const void *)&key);
226         if (unlikely(ret < 0)) {
227                 // entry not found for this IP: we did not ask a request, delete the reply
228                 tx_drop(mbuf);
229         } else {
230                 // entry found for this IP
231                 uint16_t nb_requests = task->external_ip_table[ret].nb_requests;
232                 // If we receive a request from multiple task for the same IP, then we update all tasks
233                 if (task->external_ip_table[ret].nb_requests) {
234                         rte_mbuf_refcnt_set(mbuf, nb_requests);
235                         for (int i = 0; i < nb_requests; i++) {
236                                 struct rte_ring *ring = task->external_ip_table[ret].rings[i];
237                                 tx_ring_ip(tbase, ring, MAC_INFO_FROM_MASTER, mbuf, key);
238                         }
239                         task->external_ip_table[ret].nb_requests = 0;
240                 } else {
241                         tx_drop(mbuf);
242                 }
243         }
244 }
245
246 static inline void handle_arp_request(struct task_base *tbase, struct rte_mbuf *mbuf, struct my_arp_t *arp)
247 {
248         struct task_master *task = (struct task_master *)tbase;
249         prox_rte_ether_hdr *ether_hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
250         int i, ret;
251         uint8_t port = get_port(mbuf);
252
253         struct ip_port key;
254         key.ip = arp->data.tpa;
255         key.port = port;
256         if (task->internal_port_table[port].flags & HANDLE_RANDOM_IP_FLAG) {
257                 prox_rte_ether_addr mac;
258                 plogx_dbg("\tMaster handling ARP request for ip "IPv4_BYTES_FMT" on port %d which supports random ip\n", IP4(key.ip), key.port);
259                 struct rte_ring *ring = task->internal_port_table[port].ring;
260                 create_mac(arp, &mac);
261                 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
262                 build_arp_reply(ether_hdr, &mac, arp);
263                 tx_ring(tbase, ring, SEND_ARP_REPLY_FROM_MASTER, mbuf);
264                 return;
265         }
266
267         plogx_dbg("\tMaster handling ARP request for ip "IPv4_BYTES_FMT"\n", IP4(key.ip));
268
269         ret = rte_hash_lookup(task->internal_ip_hash, (const void *)&key);
270         if (unlikely(ret < 0)) {
271                 // entry not found for this IP.
272                 plogx_dbg("Master ignoring ARP REQUEST received on un-registered IP "IPv4_BYTES_FMT" on port %d\n", IP4(arp->data.tpa), port);
273                 tx_drop(mbuf);
274         } else {
275                 struct rte_ring *ring = task->internal_ip_table[ret].ring;
276                 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
277                 build_arp_reply(ether_hdr, &task->internal_ip_table[ret].mac, arp);
278                 tx_ring(tbase, ring, SEND_ARP_REPLY_FROM_MASTER, mbuf);
279         }
280 }
281
282 static inline int record_request(struct task_base *tbase, uint32_t ip_dst, uint8_t port, struct rte_ring *ring)
283 {
284         struct task_master *task = (struct task_master *)tbase;
285         int ret = rte_hash_add_key(task->external_ip_hash, (const void *)&ip_dst);
286         int i;
287
288         if (unlikely(ret < 0)) {
289                 plogx_dbg("Unable to add IP "IPv4_BYTES_FMT" in external_ip_hash\n", IP4(ip_dst));
290                 return -1;
291         }
292
293         // If multiple tasks requesting the same info, we will need to send a reply to all of them
294         // However if one task sends multiple requests to the same IP (e.g. because it is not answering)
295         // then we should not send multiple replies to the same task
296         if (task->external_ip_table[ret].nb_requests >= PROX_MAX_ARP_REQUESTS) {
297                 // This can only happen if really many tasks requests the same IP
298                 plogx_dbg("Unable to add request for IP "IPv4_BYTES_FMT" in external_ip_table\n", IP4(ip_dst));
299                 return -1;
300         }
301         for (i = 0; i < task->external_ip_table[ret].nb_requests; i++) {
302                 if (task->external_ip_table[ret].rings[i] == ring)
303                         break;
304         }
305         if (i >= task->external_ip_table[ret].nb_requests) {
306                 // If this is a new request i.e. a new task requesting a new IP
307                 task->external_ip_table[ret].rings[task->external_ip_table[ret].nb_requests] = ring;
308                 task->external_ip_table[ret].nb_requests++;
309         }
310         return 0;
311 }
312
313 static inline void handle_unknown_ip(struct task_base *tbase, struct rte_mbuf *mbuf)
314 {
315         struct task_master *task = (struct task_master *)tbase;
316         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
317         uint8_t port = get_port(mbuf);
318         uint32_t ip_dst = get_ip(mbuf);
319         uint16_t vlan = ctrl_ring_get_vlan(mbuf);
320
321         plogx_dbg("\tMaster handling unknown ip "IPv4_BYTES_FMT" for port %d\n", IP4(ip_dst), port);
322         if (unlikely(port >= PROX_MAX_PORTS)) {
323                 plogx_dbg("Port %d not found", port);
324                 tx_drop(mbuf);
325                 return;
326         }
327         uint32_t ip_src = task->internal_port_table[port].ip;
328         struct rte_ring *ring = task->ctrl_tx_rings[get_core(mbuf) * MAX_TASKS_PER_CORE + get_task(mbuf)];
329
330         if (ring == NULL) {
331                 plogx_dbg("Port %d not registered", port);
332                 tx_drop(mbuf);
333                 return;
334         }
335
336         if (record_request(tbase, ip_dst, port, ring) < 0) {
337                 tx_drop(mbuf);
338                 return;
339         }
340         // We send an ARP request even if one was just sent (and not yet answered) by another task
341         mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
342         build_arp_request(mbuf, &task->internal_port_table[port].mac, ip_dst, ip_src, vlan);
343         tx_ring(tbase, ring, SEND_ARP_REQUEST_FROM_MASTER, mbuf);
344 }
345
346 static inline void build_icmp_reply_message(struct task_base *tbase, struct rte_mbuf *mbuf)
347 {
348         struct task_master *task = (struct task_master *)tbase;
349         struct ip_port key;
350         key.port = mbuf->port;
351         prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
352         prox_rte_ether_addr dst_mac;
353         prox_rte_ether_addr_copy(&hdr->s_addr, &dst_mac);
354         prox_rte_ether_addr_copy(&hdr->d_addr, &hdr->s_addr);
355         prox_rte_ether_addr_copy(&dst_mac, &hdr->d_addr);
356         prox_rte_ipv4_hdr *ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
357         key.ip = ip_hdr->dst_addr;
358         ip_hdr->dst_addr = ip_hdr->src_addr;
359         ip_hdr->src_addr = key.ip;
360         prox_rte_icmp_hdr *picmp = (prox_rte_icmp_hdr *)(ip_hdr + 1);
361         picmp->icmp_type = PROX_RTE_IP_ICMP_ECHO_REPLY;
362
363         int ret = rte_hash_lookup(task->internal_ip_hash, (const void *)&key);
364         if (unlikely(ret < 0)) {
365                 // entry not found for this IP.
366                 plogx_dbg("Master ignoring ICMP received on un-registered IP "IPv4_BYTES_FMT" on port %d\n", IP4(key.ip), mbuf->port);
367                 tx_drop(mbuf);
368         } else {
369                 struct rte_ring *ring = task->internal_ip_table[ret].ring;
370                 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
371                 tx_ring(tbase, ring, SEND_ICMP_FROM_MASTER, mbuf);
372         }
373 }
374
375 static inline void handle_icmp(struct task_base *tbase, struct rte_mbuf *mbuf)
376 {
377         struct task_master *task = (struct task_master *)tbase;
378         uint8_t port_id = mbuf->port;
379         struct port_table *port = &task->internal_port_table[port_id];
380         prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
381         if (hdr->ether_type != ETYPE_IPv4) {
382                 tx_drop(mbuf);
383                 return;
384         }
385         prox_rte_ipv4_hdr *ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
386         if (ip_hdr->next_proto_id != IPPROTO_ICMP) {
387                 tx_drop(mbuf);
388                 return;
389         }
390         if (ip_hdr->dst_addr != port->ip) {
391                 tx_drop(mbuf);
392                 return;
393         }
394
395         prox_rte_icmp_hdr *picmp = (prox_rte_icmp_hdr *)(ip_hdr + 1);
396         uint8_t type = picmp->icmp_type;
397         if (type == PROX_RTE_IP_ICMP_ECHO_REQUEST) {
398                 port->n_echo_req++;
399                 if (rte_rdtsc() - port->last_echo_req_rcvd_tsc > rte_get_tsc_hz()) {
400                         plog_dbg("Received %u Echo Request on IP "IPv4_BYTES_FMT" (last received from IP "IPv4_BYTES_FMT")\n", port->n_echo_req, IPv4_BYTES(((uint8_t*)&ip_hdr->dst_addr)), IPv4_BYTES(((uint8_t*)&ip_hdr->src_addr)));
401                         port->n_echo_req = 0;
402                         port->last_echo_req_rcvd_tsc = rte_rdtsc();
403                 }
404                 build_icmp_reply_message(tbase, mbuf);
405         } else if (type == PROX_RTE_IP_ICMP_ECHO_REPLY) {
406                 port->n_echo_rep++;
407                 if (rte_rdtsc() - port->last_echo_rep_rcvd_tsc > rte_get_tsc_hz()) {
408                         plog_info("Received %u Echo Reply on IP "IPv4_BYTES_FMT" (last received from IP "IPv4_BYTES_FMT")\n", port->n_echo_rep, IPv4_BYTES(((uint8_t*)&ip_hdr->dst_addr)), IPv4_BYTES(((uint8_t*)&ip_hdr->src_addr)));
409                         port->n_echo_rep = 0;
410                         port->last_echo_rep_rcvd_tsc = rte_rdtsc();
411                 }
412         }
413         tx_drop(mbuf);
414         return;
415 }
416
417 static inline void handle_unknown_ip6(struct task_base *tbase, struct rte_mbuf *mbuf)
418 {
419         struct task_master *task = (struct task_master *)tbase;
420         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
421         uint8_t port = get_port(mbuf);
422         struct ipv6_addr *ip_dst = ctrl_ring_get_ipv6_addr(mbuf);
423         int ret1, ret2, i;
424
425         plogx_dbg("\tMaster trying to find MAC of external IP "IPv6_BYTES_FMT" for port %d\n", IPv6_BYTES(ip_dst->bytes), port);
426         if (unlikely(port >= PROX_MAX_PORTS)) {
427                 plogx_dbg("Port %d not found", port);
428                 tx_drop(mbuf);
429                 return;
430         }
431         struct ipv6_addr *local_ip_src = &task->internal_port_table[port].local_ipv6_addr;
432         struct ipv6_addr *global_ip_src = &task->internal_port_table[port].global_ipv6_addr;
433         struct ipv6_addr *ip_src;
434         if (memcmp(local_ip_src, ip_dst, 8) == 0)
435                 ip_src = local_ip_src;
436         else if (memcmp(global_ip_src,  &null_addr, 16))
437                 ip_src = global_ip_src;
438         else {
439                 plogx_dbg("Unable to find a src ip for dst ip "IPv6_BYTES_FMT"\n", IPv6_BYTES(ip_dst->bytes));
440                 tx_drop(mbuf);
441                 return;
442         }
443         struct rte_ring *ring = task->ctrl_tx_rings[get_core(mbuf) * MAX_TASKS_PER_CORE + get_task(mbuf)];
444
445         if (ring == NULL) {
446                 plogx_dbg("Port %d not registered", port);
447                 tx_drop(mbuf);
448                 return;
449         }
450
451         ret2 = rte_hash_add_key(task->external_ip6_hash, (const void *)ip_dst);
452         if (unlikely(ret2 < 0)) {
453                 plogx_dbg("Unable to add IP "IPv6_BYTES_FMT" in external_ip6_hash\n", IPv6_BYTES(ip_dst->bytes));
454                 tx_drop(mbuf);
455                 return;
456         }
457
458         // If multiple tasks requesting the same info, we will need to send a reply to all of them
459         // However if one task sends multiple requests to the same IP (e.g. because it is not answering)
460         // then we should not send multiple replies to the same task
461         if (task->external_ip6_table[ret2].nb_requests >= PROX_MAX_ARP_REQUESTS) {
462                 // This can only happen if really many tasks requests the same IP
463                 plogx_dbg("Unable to add request for IP "IPv6_BYTES_FMT" in external_ip6_table\n", IPv6_BYTES(ip_dst->bytes));
464                 tx_drop(mbuf);
465                 return;
466         }
467         for (i = 0; i < task->external_ip6_table[ret2].nb_requests; i++) {
468                 if (task->external_ip6_table[ret2].rings[i] == ring)
469                         break;
470         }
471         if (i >= task->external_ip6_table[ret2].nb_requests) {
472                 // If this is a new request i.e. a new task requesting a new IP
473                 task->external_ip6_table[ret2].rings[task->external_ip6_table[ret2].nb_requests] = ring;
474                 task->external_ip6_table[ret2].nb_requests++;
475                 // Only needed for first request - but avoid test and copy the same 6 bytes
476                 // In most cases we will only have one request per IP.
477                 //memcpy(&task->external_ip6_table[ret2].mac, &task->internal_port_table[port].mac, sizeof(prox_rte_ether_addr));
478         }
479
480         // As timers are not handled by master, we might send an NS request even if one was just sent
481         // (and not yet answered) by another task
482         build_neighbour_sollicitation(mbuf, &task->internal_port_table[port].mac, ip_dst, ip_src);
483         tx_ring(tbase, ring, SEND_NDP_FROM_MASTER, mbuf);
484 }
485
486 static inline void handle_rs(struct task_base *tbase, struct rte_mbuf *mbuf)
487 {
488         struct task_master *task = (struct task_master *)tbase;
489         prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
490         prox_rte_ipv6_hdr *ipv6_hdr = (prox_rte_ipv6_hdr *)(hdr + 1);
491         int i, ret;
492         uint8_t port = get_port(mbuf);
493
494         if (task->internal_port_table[port].flags & IPV6_ROUTER) {
495                 plogx_dbg("\tMaster handling Router Solicitation from ip "IPv6_BYTES_FMT" on port %d\n", IPv6_BYTES(ipv6_hdr->src_addr), port);
496                 struct rte_ring *ring = task->internal_port_table[port].ring;
497                 build_router_advertisement(mbuf, &prox_port_cfg[port].eth_addr, &task->internal_port_table[port].local_ipv6_addr, &task->internal_port_table[port].router_prefix);
498                 tx_ring(tbase, ring, SEND_NDP_FROM_MASTER, mbuf);
499                 return;
500         }
501 }
502
503 static inline void handle_ra(struct task_base *tbase, struct rte_mbuf *mbuf)
504 {
505         struct task_master *task = (struct task_master *)tbase;
506         prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
507         prox_rte_ipv6_hdr *ipv6_hdr = (prox_rte_ipv6_hdr *)(hdr + 1);
508         int i, ret, send = 0;
509         uint8_t port = get_port(mbuf);
510         struct rte_ring *ring = task->internal_port_table[port].ring;
511
512         plog_dbg("Master handling Router Advertisement from ip "IPv6_BYTES_FMT" on port %d - len = %d; payload_len = %d\n", IPv6_BYTES(ipv6_hdr->src_addr), port, rte_pktmbuf_pkt_len(mbuf), rte_be_to_cpu_16(ipv6_hdr->payload_len));
513         if (rte_be_to_cpu_16(ipv6_hdr->payload_len) + sizeof(prox_rte_ipv6_hdr) + sizeof(prox_rte_ether_hdr) > rte_pktmbuf_pkt_len(mbuf)) {
514                 plog_err("Unexpected length received: pkt_len = %d, ipv6 hdr length = %ld, ipv6 payload len = %d\n", rte_pktmbuf_pkt_len(mbuf), sizeof(prox_rte_ipv6_hdr), rte_be_to_cpu_16(ipv6_hdr->payload_len));
515                 tx_drop(mbuf);
516                 return;
517         }
518         if (ring == NULL) {
519                 plog_info("TX side not initialized yet => dropping\n");
520                 tx_drop(mbuf);
521                 return;
522         }
523         int16_t option_len = rte_be_to_cpu_16(ipv6_hdr->payload_len) - sizeof(struct icmpv6_RA) + sizeof(struct icmpv6_option);
524         struct icmpv6_RA *router_advertisement = (struct icmpv6_RA *)(ipv6_hdr + 1);
525         struct icmpv6_option *option = (struct icmpv6_option *)&router_advertisement->options;
526         struct icmpv6_prefix_option *prefix_option;
527         while(option_len > 0) {
528                 uint8_t   type = option->type;
529                 switch(type) {
530                         case ICMPv6_source_link_layer_address:
531                                 plog_dbg("\tOption %d = Source Link Layer Address\n", type);
532                                 break;
533                         case ICMPv6_prefix_information:
534                                 prefix_option = (struct icmpv6_prefix_option *)option;
535                                 plog_dbg("\tOption %d = Prefix Information = %s\n", type, IP6_Canonical(&prefix_option->prefix));
536                                 send = 1;
537                                 break;
538                         case ICMPv6_mtu:
539                                 plog_dbg("\tOption %d = MTU\n", type);
540                                 break;
541                         default:
542                                 plog_dbg("\tOption %d = Unknown Option\n", type);
543                                 break;
544                 }
545                 if ((option->length == 0) || (option->length *8 > option_len)) {
546                         plog_err("Unexpected option length (%d) received in option %d: %d\n", option->length, option->type, option->length);
547                         send = 0;
548                         break;
549                 }
550                 option_len -=option->length * 8;
551                 option = (struct icmpv6_option *)(((uint8_t *)option) + option->length * 8);
552         }
553         if (send) {
554                 struct ipv6_addr global_ipv6;
555                 memcpy(&global_ipv6, &prefix_option->prefix, sizeof(struct ipv6_addr));
556                 set_EUI(&global_ipv6, &task->internal_port_table[port].mac);
557                 tx_ring_ip6(tbase, ring, IPV6_INFO_FROM_MASTER, mbuf, &global_ipv6);
558         } else
559                 tx_drop(mbuf);
560 }
561
562 static inline void handle_ns(struct task_base *tbase, struct rte_mbuf *mbuf)
563 {
564         struct task_master *task = (struct task_master *)tbase;
565         prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
566         prox_rte_ipv6_hdr *ipv6_hdr = (prox_rte_ipv6_hdr *)(hdr + 1);
567         struct icmpv6_NS *neighbour_sollicitation = (struct icmpv6_NS *)(ipv6_hdr + 1);
568         int i, ret;
569         uint8_t port = get_port(mbuf);
570         struct rte_ring *ring = task->internal_port_table[port].ring;
571
572         plog_dbg("Master handling Neighbour Sollicitation for ip "IPv6_BYTES_FMT" on port %d - len = %d; payload_len = %d\n", IPv6_BYTES(neighbour_sollicitation->target_address.bytes), port, rte_pktmbuf_pkt_len(mbuf), rte_be_to_cpu_16(ipv6_hdr->payload_len));
573         if (rte_be_to_cpu_16(ipv6_hdr->payload_len) + sizeof(prox_rte_ipv6_hdr) + sizeof(prox_rte_ether_hdr) > rte_pktmbuf_pkt_len(mbuf)) {
574                 plog_err("Unexpected length received: pkt_len = %d, ipv6 hdr length = %ld, ipv6 payload len = %d\n", rte_pktmbuf_pkt_len(mbuf), sizeof(prox_rte_ipv6_hdr), rte_be_to_cpu_16(ipv6_hdr->payload_len));
575                 tx_drop(mbuf);
576                 return;
577         }
578         int16_t option_len = rte_be_to_cpu_16(ipv6_hdr->payload_len) - sizeof(struct icmpv6_NS) + sizeof(struct icmpv6_option);
579         struct icmpv6_option *option = (struct icmpv6_option *)&neighbour_sollicitation->options;
580         while(option_len > 0) {
581                 uint8_t   type = option->type;
582                 switch(type) {
583                         case ICMPv6_source_link_layer_address:
584                                 plog_dbg("Option %d = Source Link Layer Address\n", type);
585                                 break;
586                         default:
587                                 plog_dbg("Option %d = Unknown Option\n", type);
588                                 break;
589                 }
590                 if ((option->length == 0) || (option->length *8 > option_len)) {
591                         plog_err("Unexpected option length (%d) received in option %d: %d\n", option->length, option->type, option->length);
592                         tx_drop(mbuf);
593                         return;
594                 }
595                 option_len -=option->length * 8;
596                 option = (struct icmpv6_option *)(((uint8_t *)option) + option->length * 8);
597         }
598         struct ip6_port key;
599         memcpy(&key.ip6, &neighbour_sollicitation->target_address, sizeof(struct ipv6_addr));
600         key.port = port;
601
602         if (memcmp(&neighbour_sollicitation->target_address, &task->internal_port_table[port].local_ipv6_addr, 8) == 0) {
603                 // Local IP
604                 if (task->internal_port_table[port].flags & HANDLE_RANDOM_LOCAL_IP_FLAG) {
605                         prox_rte_ether_addr mac;
606                         plogx_dbg("\tMaster handling NS request for ip "IPv6_BYTES_FMT" on port %d which supports random ip\n", IPv6_BYTES(key.ip6.bytes), key.port);
607                         struct rte_ring *ring = task->internal_port_table[port].ring;
608                         create_mac_from_EUI(&key.ip6, &mac);
609                         build_neighbour_advertisement(tbase, mbuf, &mac, &task->internal_port_table[port].local_ipv6_addr, PROX_SOLLICITED);
610                         tx_ring(tbase, ring, SEND_NDP_FROM_MASTER, mbuf);
611                         return;
612                 }
613         } else {
614                 if (task->internal_port_table[port].flags & HANDLE_RANDOM_GLOBAL_IP_FLAG) {
615                         prox_rte_ether_addr mac;
616                         plogx_dbg("\tMaster handling NS request for ip "IPv6_BYTES_FMT" on port %d which supports random ip\n", IPv6_BYTES(key.ip6.bytes), key.port);
617                         struct rte_ring *ring = task->internal_port_table[port].ring;
618                         create_mac_from_EUI(&key.ip6, &mac);
619                         build_neighbour_advertisement(tbase, mbuf, &mac, &task->internal_port_table[port].global_ipv6_addr, PROX_SOLLICITED);
620                         tx_ring(tbase, ring, SEND_NDP_FROM_MASTER, mbuf);
621                         return;
622                 }
623         }
624
625         ret = rte_hash_lookup(task->internal_ip6_hash, (const void *)&key);
626         if (unlikely(ret < 0)) {
627                 // entry not found for this IP.
628                 plogx_dbg("Master ignoring Neighbour Sollicitation received on un-registered IP "IPv6_BYTES_FMT" on port %d\n", IPv6_BYTES(key.ip6.bytes), port);
629                 tx_drop(mbuf);
630         } else {
631                 struct rte_ring *ring = task->internal_ip6_table[ret].ring;
632                 build_neighbour_advertisement(tbase, mbuf, &task->internal_ip6_table[ret].mac, &key.ip6, PROX_SOLLICITED);
633                 tx_ring(tbase, ring, SEND_NDP_FROM_MASTER, mbuf);
634         }
635 }
636
637 static inline void handle_na(struct task_base *tbase, struct rte_mbuf *mbuf)
638 {
639         struct task_master *task = (struct task_master *)tbase;
640         prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
641         prox_rte_ipv6_hdr *ipv6_hdr = (prox_rte_ipv6_hdr *)(hdr + 1);
642         struct icmpv6_NA *neighbour_advertisement = (struct icmpv6_NA *)(ipv6_hdr + 1);
643         int i, ret;
644         uint8_t port = get_port(mbuf);
645         struct rte_ring *ring = task->internal_port_table[port].ring;
646
647         plog_dbg("Master handling Neighbour Advertisement for ip "IPv6_BYTES_FMT" on port %d - len = %d; payload_len = %d\n", IPv6_BYTES(neighbour_advertisement->destination_address.bytes), port, rte_pktmbuf_pkt_len(mbuf), rte_be_to_cpu_16(ipv6_hdr->payload_len));
648         if (rte_be_to_cpu_16(ipv6_hdr->payload_len) + sizeof(prox_rte_ipv6_hdr) + sizeof(prox_rte_ether_hdr) > rte_pktmbuf_pkt_len(mbuf)) {
649                 plog_err("Unexpected length received: pkt_len = %d, ipv6 hdr length = %ld, ipv6 payload len = %d\n", rte_pktmbuf_pkt_len(mbuf), sizeof(prox_rte_ipv6_hdr), rte_be_to_cpu_16(ipv6_hdr->payload_len));
650                 tx_drop(mbuf);
651                 return;
652         }
653         int16_t option_len = rte_be_to_cpu_16(ipv6_hdr->payload_len) - sizeof(struct icmpv6_NA) + sizeof(struct icmpv6_option);
654         struct icmpv6_option *option = (struct icmpv6_option *)&neighbour_advertisement->options;
655         uint8_t *target_address = NULL;
656         while(option_len > 0) {
657                 uint8_t   type = option->type;
658                 switch(type) {
659                         case ICMPv6_source_link_layer_address:
660                                 plog_dbg("Option %d = Source Link Layer Address\n", type);
661                                 break;
662                         case ICMPv6_target_link_layer_address:
663                                 if (option->length != 1) {
664                                         plog_err("Unexpected option length = %u for Target Link Layer Address\n", option->length);
665                                         break;
666                                 }
667                                 target_address = option->data;
668                                 plog_dbg("Option %d = Target Link Layer Address = "MAC_BYTES_FMT"\n", type, MAC_BYTES(target_address));
669                                 break;
670                         default:
671                                 plog_dbg("Option %d = Unknown Option\n", type);
672                                 break;
673                 }
674                 if ((option->length == 0) || (option->length *8 > option_len)) {
675                         plog_err("Unexpected option length (%d) received in option %d: %d\n", option->length, option->type, option->length);
676                         tx_drop(mbuf);
677                         return;
678                 }
679                 option_len -=option->length * 8;
680                 option = (struct icmpv6_option *)(((uint8_t *)option) + option->length * 8);
681         }
682
683         if (target_address == NULL) {
684                 tx_drop(mbuf);
685         }
686         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
687         struct ipv6_addr *key = &neighbour_advertisement->destination_address;
688
689         ret = rte_hash_lookup(task->external_ip6_hash, (const void *)key);
690         if (unlikely(ret < 0)) {
691                 // entry not found for this IP: we did not ask a request, delete the reply
692                 tx_drop(mbuf);
693         } else {
694                 // entry found for this IP
695                 uint16_t nb_requests = task->external_ip6_table[ret].nb_requests;
696                 //memcpy(&hdr->d_addr.addr_bytes, &task->external_ip6_table[ret].mac, sizeof(prox_rte_ether_addr));
697                 // If we receive a request from multiple task for the same IP, then we update all tasks
698                 if (task->external_ip6_table[ret].nb_requests) {
699                         rte_mbuf_refcnt_set(mbuf, nb_requests);
700                         for (int i = 0; i < nb_requests; i++) {
701                                 struct rte_ring *ring = task->external_ip6_table[ret].rings[i];
702                                 tx_ring_ip6_data(tbase, ring, MAC_INFO_FROM_MASTER_FOR_IPV6, mbuf, &neighbour_advertisement->destination_address, *(uint64_t *)target_address);
703                         }
704                         task->external_ip6_table[ret].nb_requests = 0;
705                 } else {
706                         tx_drop(mbuf);
707                 }
708         }
709 }
710
711 static inline void handle_message(struct task_base *tbase, struct rte_mbuf *mbuf, int ring_id)
712 {
713         struct task_master *task = (struct task_master *)tbase;
714         prox_rte_ether_hdr *ether_hdr;
715         struct icmpv6 *icmpv6;
716         int command = get_command(mbuf);
717         uint8_t port = get_port(mbuf);
718         uint32_t ip;
719         uint16_t vlan, ether_type;
720         uint8_t vdev_port = prox_port_cfg[port].dpdk_mapping;
721         plogx_dbg("\tMaster received %s (%x) from mbuf %p\n", actions_string[command], command, mbuf);
722         struct my_arp_t *arp;
723
724         switch(command) {
725         case BGP_TO_MASTER:
726                 if (vdev_port != NO_VDEV_PORT) {
727                         // If a virtual (net_tap) device is attached, send the (BGP) packet to this device
728                         // The kernel will receive and handle it.
729                         plogx_dbg("\tMaster forwarding BGP packet to TAP\n");
730                         int n = rte_eth_tx_burst(prox_port_cfg[port].dpdk_mapping, 0, &mbuf, 1);
731                         return;
732                 }
733                 tx_drop(mbuf);
734                 break;
735         case ICMP_TO_MASTER:
736                 if (vdev_port != NO_VDEV_PORT) {
737                         // If a virtual (net_tap) device is attached, send the (PING) packet to this device
738                         // The kernel will receive and handle it.
739                         plogx_dbg("\tMaster forwarding packet to TAP\n");
740                         int n = rte_eth_tx_burst(prox_port_cfg[port].dpdk_mapping, 0, &mbuf, 1);
741                         return;
742                 }
743                 handle_icmp(tbase, mbuf);
744                 break;
745         case ARP_PKT_FROM_NET_TO_MASTER:
746                 if (vdev_port != NO_VDEV_PORT) {
747                         // If a virtual (net_tap) device is attached, send the (ARP) packet to this device
748                         // The kernel will receive and handle it.
749                         plogx_dbg("\tMaster forwarding packet to TAP\n");
750                         int n = rte_eth_tx_burst(prox_port_cfg[port].dpdk_mapping, 0, &mbuf, 1);
751                         return;
752                 }
753                 ether_hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
754                 ether_type = ether_hdr->ether_type;
755                 if (ether_type == ETYPE_VLAN) {
756                         prox_rte_vlan_hdr *vlan_hdr = (prox_rte_vlan_hdr *)(ether_hdr + 1);
757                         arp = (struct my_arp_t *)(vlan_hdr + 1);
758                         ether_type = vlan_hdr->eth_proto;
759                 }  else {
760                         arp = (struct my_arp_t *)(ether_hdr + 1);
761                 }
762
763                 if (ether_type != ETYPE_ARP) {
764                         plog_err("\tUnexpected message received: ARP_PKT_FROM_NET_TO_MASTER with ether_type %x\n", ether_type);
765                         tx_drop(mbuf);
766                         return;
767                 }
768                 if (arp_is_gratuitous(arp)) {
769                         plog_info("\tReceived gratuitous packet \n");
770                         tx_drop(mbuf);
771                         return;
772                 } else if (memcmp(arp, &arp_reply, 8) == 0) {
773                         // uint32_t ip = arp->data.spa;
774                         handle_arp_reply(tbase, mbuf, arp);
775                 } else if (memcmp(arp, &arp_request, 8) == 0) {
776                         handle_arp_request(tbase, mbuf, arp);
777                 } else {
778                         plog_info("\tReceived unexpected ARP operation %d\n", arp->oper);
779                         tx_drop(mbuf);
780                         return;
781                 }
782                 break;
783         case IP4_REQ_MAC_TO_MASTER:
784                 if (vdev_port != NO_VDEV_PORT) {
785                         // We send a packet to the kernel with the proper destnation IP address and our src IP address
786                         // This means that if a generator sends packets from many sources all ARP will still
787                         // be sent from the same IP src. This might be a limitation.
788                         // This prevent to have to open as many sockets as there are sources MAC addresses
789                         // We also always use the same UDP ports - as the packet will finally not leave the system anyhow
790
791                         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
792                         uint32_t ip = get_ip(mbuf);
793                         struct rte_ring *ring = task->ctrl_tx_rings[get_core(mbuf) * MAX_TASKS_PER_CORE + get_task(mbuf)];
794
795                         // First check whether MAC address is not already in kernel MAC table.
796                         // If present in our hash with a non-null MAC, then present in kernel. A null MAC
797                         // might just mean that we sent a request.
798                         // If MAC present in kernel, do not send a packet towards the kernel to try to generate
799                         // an ARP request, as the kernel would not generate it.
800                         int ret = rte_hash_lookup(task->external_ip_hash, (const void *)&ip);
801                         if ((ret >= 0) && (!prox_rte_is_zero_ether_addr(&task->external_ip_table[ret].mac))) {
802                                 memcpy(&hdr_arp->arp.data.sha, &task->external_ip_table[ret].mac, sizeof(prox_rte_ether_addr));
803                                 plogx_dbg("\tMaster ready to send MAC_INFO_FROM_MASTER ip "IPv4_BYTES_FMT" with mac "MAC_BYTES_FMT"\n",
804                                         IP4(ip), MAC_BYTES(hdr_arp->arp.data.sha.addr_bytes));
805                                 tx_ring_ip(tbase, ring, MAC_INFO_FROM_MASTER, mbuf, ip);
806                                 return;
807                         }
808
809                         struct sockaddr_in dst;
810                         dst.sin_family = AF_INET;
811                         dst.sin_addr.s_addr = ip;
812                         dst.sin_port = rte_cpu_to_be_16(PROX_PSEUDO_PKT_PORT);
813                         // TODO VLAN: find the right fd based on the VLAN
814                         int n = sendto(prox_port_cfg[vdev_port].fd, (char*)(&ip), 0, MSG_DONTROUTE,  (struct sockaddr *)&dst, sizeof(struct sockaddr_in));
815                         if (n < 0) {
816                                 plogx_info("\tFailed to send to TAP IP "IPv4_BYTES_FMT" using fd %d, error = %d (%s)\n", IPv4_BYTES(((uint8_t*)&ip)), prox_port_cfg[vdev_port].fd, errno, strerror(errno));
817                         } else
818                                 plogx_dbg("\tSent %d bytes to TAP IP "IPv4_BYTES_FMT" using fd %d\n", n, IPv4_BYTES(((uint8_t*)&ip)), prox_port_cfg[vdev_port].fd);
819
820                         record_request(tbase, ip, port, ring);
821                         tx_drop(mbuf);
822                         break;
823                 }
824                 handle_unknown_ip(tbase, mbuf);
825                 break;
826         case IP6_REQ_MAC_TO_MASTER:
827                 handle_unknown_ip6(tbase, mbuf);
828                 break;
829         case NDP_PKT_FROM_NET_TO_MASTER:
830                 ether_hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
831                 prox_rte_ipv6_hdr *ipv6_hdr = (prox_rte_ipv6_hdr *)(ether_hdr + 1);
832                 if (unlikely((ether_hdr->ether_type != ETYPE_IPv6) || (ipv6_hdr->proto != ICMPv6))) {
833                         // Should not happen
834                         if (ether_hdr->ether_type != ETYPE_IPv6)
835                                 plog_err("\tUnexpected message received: NDP_PKT_FROM_NET_TO_MASTER with ether_type %x\n", ether_hdr->ether_type);
836                         else
837                                 plog_err("\tUnexpected message received: NDP_PKT_FROM_NET_TO_MASTER with ether_type %x and proto %x\n", ether_hdr->ether_type, ipv6_hdr->proto);
838                         tx_drop(mbuf);
839                         return;
840                 }
841                 icmpv6 = (struct icmpv6 *)(ipv6_hdr + 1);
842                 switch (icmpv6->type) {
843                 case ICMPv6_DU:
844                         plog_err("IPV6 ICMPV6 Destination Unreachable\n");
845                         tx_drop(mbuf);
846                         break;
847                 case ICMPv6_PTB:
848                         plog_err("IPV6 ICMPV6 packet too big\n");
849                         tx_drop(mbuf);
850                         break;
851                 case ICMPv6_TE:
852                         plog_err("IPV6 ICMPV6 Time Exceeded\n");
853                         tx_drop(mbuf);
854                         break;
855                 case ICMPv6_PaPr:
856                         plog_err("IPV6 ICMPV6 Parameter Problem\n");
857                         tx_drop(mbuf);
858                         break;
859                 case ICMPv6_RS:
860                         handle_rs(tbase, mbuf);
861                         break;
862                 case ICMPv6_RA:
863                         handle_ra(tbase, mbuf);
864                         break;
865                 case ICMPv6_NS:
866                         handle_ns(tbase, mbuf);
867                         break;
868                 case ICMPv6_NA:
869                         handle_na(tbase, mbuf);
870                         break;
871                 case ICMPv6_RE:
872                         plog_err("IPV6 ICMPV6 Redirect not handled\n");
873                         tx_drop(mbuf);
874                         break;
875                 default:
876                         plog_err("Unexpected type %d in IPV6 ICMPV6\n", icmpv6->type);
877                         tx_drop(mbuf);
878                         break;
879                 }
880                 break;
881         default:
882                 plogx_dbg("\tMaster received unexpected message\n");
883                 tx_drop(mbuf);
884                 break;
885         }
886 }
887
888 void init_ctrl_plane(struct task_base *tbase)
889 {
890         struct task_master *task = (struct task_master *)tbase;
891         int socket_id = rte_lcore_to_socket_id(prox_cfg.master);
892         uint32_t n_entries = MAX_ARP_ENTRIES * 4;
893         static char hash_name[30];
894
895         sprintf(hash_name, "A%03d_hash_arp_table", prox_cfg.master);
896         struct rte_hash_parameters hash_params = {
897                 .name = hash_name,
898                 .entries = n_entries,
899                 .hash_func = rte_hash_crc,
900                 .hash_func_init_val = 0,
901         };
902         if (prox_cfg.flags & DSF_L3_ENABLED) {
903                 hash_params.key_len = sizeof(uint32_t);
904                 task->external_ip_hash = rte_hash_create(&hash_params);
905                 PROX_PANIC(task->external_ip_hash == NULL, "Failed to set up external ip hash\n");
906                 plog_info("\texternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
907                 hash_name[0]++;
908
909                 task->external_ip_table = (struct external_ip_table *)prox_zmalloc(n_entries * sizeof(struct external_ip_table), socket_id);
910                 PROX_PANIC(task->external_ip_table == NULL, "Failed to allocate memory for %u entries in external ip table\n", n_entries);
911                 plog_info("\texternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct external_ip_table));
912
913                 hash_params.key_len = sizeof(struct ip_port);
914                 task->internal_ip_hash = rte_hash_create(&hash_params);
915                 PROX_PANIC(task->internal_ip_hash == NULL, "Failed to set up internal ip hash\n");
916                 plog_info("\tinternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
917                 hash_name[0]++;
918
919                 task->internal_ip_table = (struct ip_table *)prox_zmalloc(n_entries * sizeof(struct ip_table), socket_id);
920                 PROX_PANIC(task->internal_ip_table == NULL, "Failed to allocate memory for %u entries in internal ip table\n", n_entries);
921                 plog_info("\tinternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct ip_table));
922         }
923
924         if (prox_cfg.flags & DSF_NDP_ENABLED) {
925                 hash_params.key_len = sizeof(struct ipv6_addr);
926                 task->external_ip6_hash = rte_hash_create(&hash_params);
927                 PROX_PANIC(task->external_ip6_hash == NULL, "Failed to set up external ip6 hash\n");
928                 plog_info("\texternal ip6 hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
929                 hash_name[0]++;
930
931                 task->external_ip6_table = (struct external_ip_table *)prox_zmalloc(n_entries * sizeof(struct external_ip_table), socket_id);
932                 PROX_PANIC(task->external_ip6_table == NULL, "Failed to allocate memory for %u entries in external ip6 table\n", n_entries);
933                 plog_info("\texternal ip6_table, with %d entries of size %ld\n", n_entries, sizeof(struct external_ip_table));
934
935                 hash_params.key_len = sizeof(struct ip6_port);
936                 task->internal_ip6_hash = rte_hash_create(&hash_params);
937                 PROX_PANIC(task->internal_ip6_hash == NULL, "Failed to set up internal ip6 hash\n");
938                 plog_info("\tinternal ip6 hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
939                 hash_name[0]++;
940
941                 task->internal_ip6_table = (struct ip_table *)prox_zmalloc(n_entries * sizeof(struct ip_table), socket_id);
942                 PROX_PANIC(task->internal_ip6_table == NULL, "Failed to allocate memory for %u entries in internal ip6 table\n", n_entries);
943                 plog_info("\tinternal ip6 table, with %d entries of size %ld\n", n_entries, sizeof(struct ip_table));
944         }
945
946         int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
947         PROX_PANIC(fd < 0, "Failed to open netlink socket: %d\n", errno);
948         fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
949
950         struct sockaddr_nl sockaddr;
951         memset(&sockaddr, 0, sizeof(struct sockaddr_nl));
952         sockaddr.nl_family = AF_NETLINK;
953         sockaddr.nl_groups = RTMGRP_NEIGH | RTMGRP_NOTIFY;
954         int rc = bind(fd, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr_nl));
955         PROX_PANIC(rc < 0, "Failed to bind to RTMGRP_NEIGH netlink group\n");
956         task->arp_fds.fd = fd;
957         task->arp_fds.events = POLL_IN;
958         plog_info("\tRTMGRP_NEIGH netlink group bound; fd = %d\n", fd);
959
960         fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
961         PROX_PANIC(fd < 0, "Failed to open netlink socket: %d\n", errno);
962         fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
963         struct sockaddr_nl sockaddr2;
964         memset(&sockaddr2, 0, sizeof(struct sockaddr_nl));
965         sockaddr2.nl_family = AF_NETLINK;
966         sockaddr2.nl_groups = RTMGRP_IPV6_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_NOTIFY;
967         rc = bind(fd, (struct sockaddr *)&sockaddr2, sizeof(struct sockaddr_nl));
968         PROX_PANIC(rc < 0, "Failed to bind to RTMGRP_NEIGH netlink group\n");
969         task->route_fds.fd = fd;
970         task->route_fds.events = POLL_IN;
971         plog_info("\tRTMGRP_IPV4_ROUTE netlink group bound; fd = %d\n", fd);
972
973         static char name[] = "master_arp_nd_pool";
974         const int NB_ARP_MBUF = 1024;
975         const int ARP_MBUF_SIZE = 2048;
976         const int NB_CACHE_ARP_MBUF = 256;
977         struct rte_mempool *ret = rte_mempool_create(name, NB_ARP_MBUF, ARP_MBUF_SIZE, NB_CACHE_ARP_MBUF,
978                 sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, 0,
979                 rte_socket_id(), 0);
980         PROX_PANIC(ret == NULL, "Failed to allocate ARP memory pool on socket %u with %u elements\n",
981                 rte_socket_id(), NB_ARP_MBUF);
982         plog_info("\tMempool %p (%s) size = %u * %u cache %u, socket %d\n", ret, name, NB_ARP_MBUF,
983                 ARP_MBUF_SIZE, NB_CACHE_ARP_MBUF, rte_socket_id());
984         tbase->l3.arp_nd_pool = ret;
985 }
986
987 static void handle_route_event(struct task_base *tbase)
988 {
989         struct task_master *task = (struct task_master *)tbase;
990         struct rte_mbuf *mbufs[MAX_RING_BURST];
991         int fd = task->route_fds.fd, interface_index, mask = -1;
992         char interface_name[IF_NAMESIZE] = {0};
993         int len = recv(fd, netlink_buf, sizeof(netlink_buf), 0);
994         uint32_t ip = 0, gw_ip = 0;
995         if (len < 0) {
996                 plog_err("Failed to recv from netlink: %d\n", errno);
997                 return;
998         }
999         struct nlmsghdr * nl_hdr = (struct nlmsghdr *)netlink_buf;
1000         if (nl_hdr->nlmsg_flags & NLM_F_MULTI) {
1001                 plog_err("Unexpected multipart netlink message\n");
1002                 return;
1003         }
1004         if ((nl_hdr->nlmsg_type != RTM_NEWROUTE) && (nl_hdr->nlmsg_type != RTM_DELROUTE))
1005                 return;
1006
1007         struct rtmsg *rtmsg = (struct rtmsg *)NLMSG_DATA(nl_hdr);
1008         int rtm_family = rtmsg->rtm_family;
1009         if ((rtm_family == AF_INET) && (rtmsg->rtm_table != RT_TABLE_MAIN) &&(rtmsg->rtm_table != RT_TABLE_LOCAL))
1010                 return;
1011         int dst_len = rtmsg->rtm_dst_len;
1012
1013         struct rtattr *rta = (struct rtattr *)RTM_RTA(rtmsg);
1014         int rtl = RTM_PAYLOAD(nl_hdr);
1015         for (; RTA_OK(rta, rtl); rta = RTA_NEXT(rta, rtl)) {
1016                 switch (rta->rta_type) {
1017                 case RTA_DST:
1018                         ip = *((uint32_t *)RTA_DATA(rta));
1019                         break;
1020                 case RTA_OIF:
1021                         interface_index = *((int *)RTA_DATA(rta));
1022                         if (if_indextoname(interface_index, interface_name) == NULL) {
1023                                 plog_info("Unknown Interface Index %d\n", interface_index);
1024                         }
1025                         break;
1026                 case RTA_METRICS:
1027                         mask = *((int *)RTA_DATA(rta));
1028                         break;
1029                 case RTA_GATEWAY:
1030                         gw_ip = *((uint32_t *)RTA_DATA(rta));
1031                         break;
1032                 default:
1033                         break;
1034                 }
1035         }
1036         int dpdk_vdev_port = -1;
1037         for (int i = 0; i< prox_rte_eth_dev_count_avail(); i++) {
1038                 if (strcmp(prox_port_cfg[i].name, interface_name) == 0)
1039                         dpdk_vdev_port = i;
1040         }
1041         if (dpdk_vdev_port != -1) {
1042                 plogx_info("Received netlink message on tap interface %s for IP "IPv4_BYTES_FMT"/%d, Gateway  "IPv4_BYTES_FMT"\n", interface_name, IP4(ip), dst_len, IP4(gw_ip));
1043                 int ret1 = rte_mempool_get(tbase->l3.arp_nd_pool, (void **)mbufs);
1044                 if (unlikely(ret1 != 0)) {
1045                         plog_err("Unable to allocate a mbuf for master to core communication\n");
1046                         return;
1047                 }
1048                 int dpdk_port = prox_port_cfg[dpdk_vdev_port].dpdk_mapping;
1049                 tx_ring_route(tbase, task->internal_port_table[dpdk_port].ring, (nl_hdr->nlmsg_type == RTM_NEWROUTE), mbufs[0], ip, gw_ip, dst_len);
1050         } else
1051                 plog_info("Received netlink message on unknown interface %s for IP "IPv4_BYTES_FMT"/%d, Gateway  "IPv4_BYTES_FMT"\n", interface_name[0] ? interface_name:"", IP4(ip), dst_len, IP4(gw_ip));
1052         return;
1053 }
1054
1055 static void handle_arp_event(struct task_base *tbase)
1056 {
1057         struct task_master *task = (struct task_master *)tbase;
1058         struct rte_mbuf *mbufs[MAX_RING_BURST];
1059         struct nlmsghdr * nl_hdr;
1060         int fd = task->arp_fds.fd;
1061         int len, ret;
1062         uint32_t ip = 0;
1063         prox_rte_ether_addr mac;
1064         memset(&mac, 0, sizeof(mac));
1065         len = recv(fd, netlink_buf, sizeof(netlink_buf), 0);
1066         if (len < 0) {
1067                 plog_err("Failed to recv from netlink: %d\n", errno);
1068                 return;
1069         }
1070         nl_hdr = (struct nlmsghdr *)netlink_buf;
1071         if (nl_hdr->nlmsg_flags & NLM_F_MULTI) {
1072                 plog_err("Unexpected multipart netlink message\n");
1073                 return;
1074         }
1075         if ((nl_hdr->nlmsg_type != RTM_NEWNEIGH) && (nl_hdr->nlmsg_type != RTM_DELNEIGH))
1076                 return;
1077
1078         struct ndmsg *ndmsg = (struct ndmsg *)NLMSG_DATA(nl_hdr);
1079         int ndm_family = ndmsg->ndm_family;
1080         struct rtattr *rta = (struct rtattr *)RTM_RTA(ndmsg);
1081         int rtl = RTM_PAYLOAD(nl_hdr);
1082         for (; RTA_OK(rta, rtl); rta = RTA_NEXT(rta, rtl)) {
1083                 switch (rta->rta_type) {
1084                 case NDA_DST:
1085                         ip = *((uint32_t *)RTA_DATA(rta));
1086                         break;
1087                 case NDA_LLADDR:
1088                         mac = *((prox_rte_ether_addr *)(uint64_t *)RTA_DATA(rta));
1089                         break;
1090                 default:
1091                         break;
1092                 }
1093         }
1094         plogx_info("Received netlink ip "IPv4_BYTES_FMT" with mac "MAC_BYTES_FMT"\n", IP4(ip), MAC_BYTES(mac.addr_bytes));
1095         ret = rte_hash_lookup(task->external_ip_hash, (const void *)&ip);
1096         if (unlikely(ret < 0)) {
1097                 // entry not found for this IP: we did not ask a request.
1098                 // This can happen if the kernel updated the ARP table when receiving an ARP_REQUEST
1099                 // We must record this, as the ARP entry is now in the kernel table
1100                 if (prox_rte_is_zero_ether_addr(&mac)) {
1101                         // Timeout or MAC deleted from kernel MAC table
1102                         int ret = rte_hash_del_key(task->external_ip_hash, (const void *)&ip);
1103                         plogx_dbg("ip "IPv4_BYTES_FMT" removed from external_ip_hash\n", IP4(ip));
1104                         return;
1105                 }
1106                 int ret = rte_hash_add_key(task->external_ip_hash, (const void *)&ip);
1107                 if (unlikely(ret < 0)) {
1108                         plogx_dbg("IP "IPv4_BYTES_FMT" not found in external_ip_hash and unable to add it\n", IP4(ip));
1109                         return;
1110                 }
1111                 memcpy(&task->external_ip_table[ret].mac, &mac, sizeof(prox_rte_ether_addr));
1112                 plogx_dbg("ip "IPv4_BYTES_FMT" added in external_ip_hash with mac "MAC_BYTES_FMT"\n", IP4(ip), MAC_BYTES(mac.addr_bytes));
1113                 return;
1114         }
1115
1116         // entry found for this IP
1117         uint16_t nb_requests = task->external_ip_table[ret].nb_requests;
1118         if (nb_requests == 0) {
1119                 return;
1120         }
1121
1122         memcpy(&task->external_ip_table[ret].mac, &mac, sizeof(prox_rte_ether_addr));
1123
1124         // If we receive a request from multiple task for the same IP, then we update all tasks
1125         int ret1 = rte_mempool_get(tbase->l3.arp_nd_pool, (void **)mbufs);
1126         if (unlikely(ret1 != 0)) {
1127                 plog_err("Unable to allocate a mbuf for master to core communication\n");
1128                 return;
1129         }
1130         rte_mbuf_refcnt_set(mbufs[0], nb_requests);
1131         for (int i = 0; i < nb_requests; i++) {
1132                 struct rte_ring *ring = task->external_ip_table[ret].rings[i];
1133                 struct ether_hdr_arp *hdr = rte_pktmbuf_mtod(mbufs[0], struct ether_hdr_arp *);
1134                 memcpy(&hdr->arp.data.sha, &mac, sizeof(prox_rte_ether_addr));
1135                 tx_ring_ip(tbase, ring, MAC_INFO_FROM_MASTER, mbufs[0], ip);
1136                 plog_dbg("MAC_INFO_FROM_MASTER ip "IPv4_BYTES_FMT" with mac "MAC_BYTES_FMT"\n", IP4(ip), MAC_BYTES(mac.addr_bytes));
1137         }
1138         task->external_ip_table[ret].nb_requests = 0;
1139         return;
1140 }
1141
1142 static int handle_ctrl_plane_f(struct task_base *tbase, __attribute__((unused)) struct rte_mbuf **mbuf, uint16_t n_pkts)
1143 {
1144         int ring_id = 0, j, ret = 0, n = 0;
1145         struct rte_mbuf *mbufs[MAX_RING_BURST];
1146         struct task_master *task = (struct task_master *)tbase;
1147
1148         /*      Handle_master works differently than other handle functions
1149                 It is not handled by a DPDK dataplane core
1150                 It is no thread_generic based, hence do not receive packets the same way
1151         */
1152
1153         ret = ring_deq(task->ctrl_rx_ring, mbufs);
1154         for (j = 0; j < ret; j++) {
1155                 handle_message(tbase, mbufs[j], ring_id);
1156         }
1157         for (int vdev_id = 0; vdev_id < task->max_vdev_id; vdev_id++) {
1158                 struct vdev *vdev = &task->all_vdev[vdev_id];
1159                 n = rte_eth_rx_burst(vdev->port_id, 0, mbufs, MAX_PKT_BURST);
1160                 for (j = 0; j < n; j++) {
1161                         tx_ring(tbase, vdev->ring, PKT_FROM_TAP, mbufs[j]);
1162                 }
1163                 ret +=n;
1164         }
1165         if ((task->max_vdev_id) && (poll(&task->arp_fds, 1, prox_cfg.poll_timeout) == POLL_IN)) {
1166                 handle_arp_event(tbase);
1167         }
1168         if (poll(&task->route_fds, 1, prox_cfg.poll_timeout) == POLL_IN) {
1169                 handle_route_event(tbase);
1170         }
1171         return ret;
1172 }
1173
1174 static void init_task_master(struct task_base *tbase, struct task_args *targs)
1175 {
1176         if (prox_cfg.flags & DSF_CTRL_PLANE_ENABLED) {
1177                 struct task_master *task = (struct task_master *)tbase;
1178
1179                 task->ctrl_rx_ring = targs->lconf->ctrl_rings_p[0];
1180                 task->ctrl_tx_rings = ctrl_rings;
1181                 init_ctrl_plane(tbase);
1182                 handle_ctrl_plane = handle_ctrl_plane_f;
1183         }
1184 }
1185
1186 static struct task_init task_init_master = {
1187         .mode_str = "master",
1188         .init = init_task_master,
1189         .handle = NULL,
1190         .flag_features = TASK_FEATURE_NEVER_DISCARDS,
1191         .size = sizeof(struct task_master)
1192 };
1193
1194 __attribute__((constructor)) static void reg_task_gen(void)
1195 {
1196         reg_task(&task_init_master);
1197 }