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