Merge changes from topic 'fix_multiple_arp_issues'
[samplevnf.git] / VNFs / DPPD-PROX / handle_master.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_hash.h>
18 #include <rte_hash_crc.h>
19 #include "prox_cfg.h"
20
21 #include "prox_globals.h"
22 #include "rx_pkt.h"
23 #include "arp.h"
24 #include "handle_master.h"
25 #include "log.h"
26 #include "mbuf_utils.h"
27 #include "etypes.h"
28 #include "defaults.h"
29 #include "prox_cfg.h"
30 #include "prox_malloc.h"
31 #include "quit.h"
32 #include "task_init.h"
33 #include "prox_port_cfg.h"
34 #include "main.h"
35 #include "lconf.h"
36 #include "input.h"
37 #include "tx_pkt.h"
38
39 #define PROX_MAX_ARP_REQUESTS   32      // Maximum number of tasks requesting the same MAC address
40
41 const char *actions_string[] = {"UPDATE_FROM_CTRL", "SEND_ARP_REQUEST_FROM_CTRL", "SEND_ARP_REPLY_FROM_CTRL", "HANDLE_ARP_TO_CTRL", "REQ_MAC_TO_CTRL"};
42
43 static struct my_arp_t arp_reply = {
44         .htype = 0x100,
45         .ptype = 8,
46         .hlen = 6,
47         .plen = 4,
48         .oper = 0x200
49 };
50 static struct my_arp_t arp_request = {
51         .htype = 0x100,
52         .ptype = 8,
53         .hlen = 6,
54         .plen = 4,
55         .oper = 0x100
56 };
57
58 struct ip_table {
59         struct ether_addr       mac;
60         struct rte_ring         *ring;
61 };
62
63 struct external_ip_table {
64         struct ether_addr       mac;
65         struct rte_ring         *rings[PROX_MAX_ARP_REQUESTS];
66         uint16_t                nb_requests;
67 };
68
69 struct port_table {
70         struct ether_addr       mac;
71         struct rte_ring         *ring;
72         uint32_t                ip;
73         uint8_t                 port;
74         uint8_t                 flags;
75 };
76
77 struct task_master {
78         struct task_base base;
79         struct rte_ring *ctrl_rx_ring;
80         struct rte_ring **ctrl_tx_rings;
81         struct ip_table *internal_ip_table;
82         struct external_ip_table *external_ip_table;
83         struct rte_hash  *external_ip_hash;
84         struct rte_hash  *internal_ip_hash;
85         struct port_table internal_port_table[PROX_MAX_PORTS];
86 };
87
88 struct ip_port {
89         uint32_t ip;
90         uint8_t port;
91 } __attribute__((packed));
92
93 static inline uint8_t get_command(struct rte_mbuf *mbuf)
94 {
95         return mbuf->udata64 & 0xFF;
96 }
97 static inline uint8_t get_task(struct rte_mbuf *mbuf)
98 {
99         return (mbuf->udata64 >> 8) & 0xFF;
100 }
101 static inline uint8_t get_core(struct rte_mbuf *mbuf)
102 {
103         return (mbuf->udata64 >> 16) & 0xFF;
104 }
105 static inline uint8_t get_port(struct rte_mbuf *mbuf)
106 {
107         return mbuf->port;
108 }
109 static inline uint32_t get_ip(struct rte_mbuf *mbuf)
110 {
111         return (mbuf->udata64 >> 32) & 0xFFFFFFFF;
112 }
113
114 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)
115 {
116         struct task_master *task = (struct task_master *)tbase;
117         struct ip_port key;
118         plogx_dbg("\tregistering IP %d.%d.%d.%d with port %d core %d and task %d\n", IP4(ip), port_id, core_id, task_id);
119
120         if (port_id >= PROX_MAX_PORTS) {
121                 plog_err("Unable to register ip %d.%d.%d.%d, port %d\n", IP4(ip), port_id);
122                 return;
123         }
124
125         /* TODO - stoe multiple rings if multiple cores able to handle IP
126            Remove them when such cores are stopped and de-register IP
127         */
128         task->internal_port_table[port_id].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
129         memcpy(&task->internal_port_table[port_id].mac, &prox_port_cfg[port_id].eth_addr, 6);
130         task->internal_port_table[port_id].ip = ip;
131
132         if (ip == RANDOM_IP) {
133                 task->internal_port_table[port_id].flags |= HANDLE_RANDOM_IP_FLAG;
134                 return;
135         }
136
137         key.ip = ip;
138         key.port = port_id;
139         int ret = rte_hash_add_key(task->internal_ip_hash, (const void *)&key);
140         if (unlikely(ret < 0)) {
141                 plog_err("Unable to register ip %d.%d.%d.%d\n", IP4(ip));
142                 return;
143         }
144         memcpy(&task->internal_ip_table[ret].mac, &prox_port_cfg[port_id].eth_addr, 6);
145         task->internal_ip_table[ret].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
146
147 }
148
149 static inline void handle_arp_reply(struct task_base *tbase, struct rte_mbuf *mbuf)
150 {
151         struct task_master *task = (struct task_master *)tbase;
152         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
153         int i, ret;
154         uint32_t key = hdr_arp->arp.data.spa;
155         plogx_dbg("\tMaster handling ARP reply for ip %d.%d.%d.%d\n", IP4(key));
156
157         ret = rte_hash_lookup(task->external_ip_hash, (const void *)&key);
158         if (unlikely(ret < 0)) {
159                 // entry not found for this IP: we did not ask a request, delete the reply
160                 tx_drop(mbuf);
161         } else {
162                 // entry found for this IP
163                 uint16_t nb_requests = task->external_ip_table[ret].nb_requests;
164                 memcpy(&hdr_arp->ether_hdr.d_addr.addr_bytes, &task->external_ip_table[ret].mac, 6);
165                 // If we receive a request from multiple task for the same IP, then we update all tasks
166                 if (task->external_ip_table[ret].nb_requests) {
167                         rte_mbuf_refcnt_set(mbuf, nb_requests);
168                         for (int i = 0; i < nb_requests; i++) {
169                                 struct rte_ring *ring = task->external_ip_table[ret].rings[i];
170                                 tx_ring_ip(tbase, ring, UPDATE_FROM_CTRL, mbuf, key);
171                         }
172                         task->external_ip_table[ret].nb_requests = 0;
173                 }
174         }
175 }
176
177 static inline void handle_arp_request(struct task_base *tbase, struct rte_mbuf *mbuf)
178 {
179         struct task_master *task = (struct task_master *)tbase;
180         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
181         int i, ret;
182         uint8_t port = get_port(mbuf);
183
184         struct ip_port key;
185         key.ip = hdr_arp->arp.data.tpa;
186         key.port = port;
187         if (task->internal_port_table[port].flags & HANDLE_RANDOM_IP_FLAG) {
188                 struct ether_addr mac;
189                 plogx_dbg("\tMaster handling ARP request for ip %d.%d.%d.%d on port %d which supports random ip\n", IP4(key.ip), key.port);
190                 struct rte_ring *ring = task->internal_port_table[port].ring;
191                 create_mac(hdr_arp, &mac);
192                 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
193                 build_arp_reply(hdr_arp, &mac);
194                 tx_ring(tbase, ring, ARP_REPLY_FROM_CTRL, mbuf);
195                 return;
196         }
197
198         plogx_dbg("\tMaster handling ARP request for ip %d.%d.%d.%d\n", IP4(key.ip));
199
200         ret = rte_hash_lookup(task->internal_ip_hash, (const void *)&key);
201         if (unlikely(ret < 0)) {
202                 // entry not found for this IP.
203                 plogx_dbg("Master ignoring ARP REQUEST received on un-registered IP %d.%d.%d.%d on port %d\n", IP4(hdr_arp->arp.data.tpa), port);
204                 tx_drop(mbuf);
205         } else {
206                 struct rte_ring *ring = task->internal_ip_table[ret].ring;
207                 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
208                 build_arp_reply(hdr_arp, &task->internal_ip_table[ret].mac);
209                 tx_ring(tbase, ring, ARP_REPLY_FROM_CTRL, mbuf);
210         }
211 }
212
213 static inline void handle_unknown_ip(struct task_base *tbase, struct rte_mbuf *mbuf)
214 {
215         struct task_master *task = (struct task_master *)tbase;
216         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
217         uint8_t port = get_port(mbuf);
218         uint32_t ip_dst = get_ip(mbuf);
219         int ret1, ret2, i;
220
221         plogx_dbg("\tMaster handling unknown ip %d.%d.%d.%d for port %d\n", IP4(ip_dst), port);
222         if (unlikely(port >= PROX_MAX_PORTS)) {
223                 plogx_dbg("Port %d not found", port);
224                 tx_drop(mbuf);
225                 return;
226         }
227         uint32_t ip_src = task->internal_port_table[port].ip;
228         struct rte_ring *ring = task->ctrl_tx_rings[get_core(mbuf) * MAX_TASKS_PER_CORE + get_task(mbuf)];
229
230         if (ring == NULL) {
231                 plogx_dbg("Port %d not registered", port);
232                 tx_drop(mbuf);
233                 return;
234         }
235
236         ret2 = rte_hash_add_key(task->external_ip_hash, (const void *)&ip_dst);
237         if (unlikely(ret2 < 0)) {
238                 // entry not found for this IP: delete the reply
239                 plogx_dbg("Unable to add IP %d.%d.%d.%d in external_ip_hash\n", IP4(ip_dst));
240                 tx_drop(mbuf);
241                 return;
242         }
243
244         // If multiple tasks requesting the same info, we will need to send a reply to all of them
245         // However if one task sends multiple requests to the same IP (e.g. because it is not answering)
246         // then we should not send multiple replies to the same task
247         if (task->external_ip_table[ret2].nb_requests >= PROX_MAX_ARP_REQUESTS) {
248                 // This can only happen if really many tasks requests the same IP
249                 plogx_dbg("Unable to add request for IP %d.%d.%d.%d in external_ip_table\n", IP4(ip_dst));
250                 tx_drop(mbuf);
251                 return;
252         }
253         for (i = 0; i < task->external_ip_table[ret2].nb_requests; i++) {
254                 if (task->external_ip_table[ret2].rings[i] == ring)
255                         break;
256         }
257         if (i >= task->external_ip_table[ret2].nb_requests) {
258                 // If this is a new request i.e. a new task requesting a new IP
259                 task->external_ip_table[ret2].rings[task->external_ip_table[ret2].nb_requests] = ring;
260                 task->external_ip_table[ret2].nb_requests++;
261                 // Only needed for first request - but avoid test and copy the same 6 bytes
262                 // In most cases we will only have one request per IP.
263                 memcpy(&task->external_ip_table[ret2].mac, &task->internal_port_table[port].mac, 6);
264         }
265
266         // We send an ARP request even if one was just sent (and not yet answered) by another task
267         mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
268         build_arp_request(mbuf, &task->internal_port_table[port].mac, ip_dst, ip_src);
269         tx_ring(tbase, ring, ARP_REQ_FROM_CTRL, mbuf);
270 }
271
272 static inline void handle_message(struct task_base *tbase, struct rte_mbuf *mbuf, int ring_id)
273 {
274         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
275         int command = get_command(mbuf);
276         uint32_t ip;
277         plogx_dbg("\tMaster received %s (%x) from mbuf %p\n", actions_string[command], command, mbuf);
278
279         switch(command) {
280         case ARP_TO_CTRL:
281                 if (hdr_arp->ether_hdr.ether_type != ETYPE_ARP) {
282                         tx_drop(mbuf);
283                         plog_err("\tUnexpected message received: ARP_TO_CTRL with ether_type %x\n", hdr_arp->ether_hdr.ether_type);
284                         return;
285                 } else if (arp_is_gratuitous(hdr_arp)) {
286                         plog_info("\tReceived gratuitous packet \n");
287                         tx_drop(mbuf);
288                         return;
289                 } else if (memcmp(&hdr_arp->arp, &arp_reply, 8) == 0) {
290                         uint32_t ip = hdr_arp->arp.data.spa;
291                         handle_arp_reply(tbase, mbuf);
292                 } else if (memcmp(&hdr_arp->arp, &arp_request, 8) == 0) {
293                         handle_arp_request(tbase, mbuf);
294                 } else {
295                         plog_info("\tReceived unexpected ARP operation %d\n", hdr_arp->arp.oper);
296                         tx_drop(mbuf);
297                         return;
298                 }
299                 break;
300         case REQ_MAC_TO_CTRL:
301                 handle_unknown_ip(tbase, mbuf);
302                 break;
303         default:
304                 plogx_dbg("\tMaster received unexpected message\n");
305                 tx_drop(mbuf);
306                 break;
307         }
308 }
309
310 void init_ctrl_plane(struct task_base *tbase)
311 {
312         prox_cfg.flags |= DSF_CTRL_PLANE_ENABLED;
313         struct task_master *task = (struct task_master *)tbase;
314         int socket = rte_lcore_to_socket_id(prox_cfg.master);
315         uint32_t n_entries = MAX_ARP_ENTRIES * 4;
316         static char hash_name[30];
317         sprintf(hash_name, "A%03d_hash_arp_table", prox_cfg.master);
318         struct rte_hash_parameters hash_params = {
319                 .name = hash_name,
320                 .entries = n_entries,
321                 .key_len = sizeof(uint32_t),
322                 .hash_func = rte_hash_crc,
323                 .hash_func_init_val = 0,
324         };
325         task->external_ip_hash = rte_hash_create(&hash_params);
326         PROX_PANIC(task->external_ip_hash == NULL, "Failed to set up external ip hash\n");
327         plog_info("\texternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
328         task->external_ip_table = (struct external_ip_table *)prox_zmalloc(n_entries * sizeof(struct external_ip_table), socket);
329         PROX_PANIC(task->external_ip_table == NULL, "Failed to allocate memory for %u entries in external ip table\n", n_entries);
330         plog_info("\texternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct external_ip_table));
331
332         hash_name[0]++;
333         hash_params.key_len = sizeof(struct ip_port);
334         task->internal_ip_hash = rte_hash_create(&hash_params);
335         PROX_PANIC(task->internal_ip_hash == NULL, "Failed to set up internal ip hash\n");
336         plog_info("\tinternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
337         task->internal_ip_table = (struct ip_table *)prox_zmalloc(n_entries * sizeof(struct ip_table), socket);
338         PROX_PANIC(task->internal_ip_table == NULL, "Failed to allocate memory for %u entries in internal ip table\n", n_entries);
339         plog_info("\tinternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct ip_table));
340 }
341
342 static int handle_ctrl_plane_f(struct task_base *tbase, __attribute__((unused)) struct rte_mbuf **mbuf, uint16_t n_pkts)
343 {
344         int ring_id = 0, j, ret = 0;
345         struct rte_mbuf *mbufs[MAX_RING_BURST];
346         struct task_master *task = (struct task_master *)tbase;
347
348         /*      Handle_master works differently than other handle functions
349                 It is not handled by a DPDK dataplane core
350                 It is no thread_generic based, hence do not receive packets the same way
351         */
352
353         ret = ring_deq(task->ctrl_rx_ring, mbufs);
354         for (j = 0; j < ret; j++) {
355                 handle_message(tbase, mbufs[j], ring_id);
356         }
357         return ret;
358 }
359
360 static void init_task_master(struct task_base *tbase, struct task_args *targs)
361 {
362         if (prox_cfg.flags & DSF_CTRL_PLANE_ENABLED) {
363                 struct task_master *task = (struct task_master *)tbase;
364
365                 task->ctrl_rx_ring = targs->lconf->ctrl_rings_p[0];
366                 task->ctrl_tx_rings = ctrl_rings;
367                 init_ctrl_plane(tbase);
368                 handle_ctrl_plane = handle_ctrl_plane_f;
369         }
370 }
371
372 static struct task_init task_init_master = {
373         .mode_str = "master",
374         .init = init_task_master,
375         .handle = NULL,
376         .flag_features = TASK_FEATURE_NEVER_DISCARDS,
377         .size = sizeof(struct task_master)
378 };
379
380 __attribute__((constructor)) static void reg_task_gen(void)
381 {
382         reg_task(&task_init_master);
383 }