Improve ctrlplane performance
[samplevnf.git] / VNFs / DPPD-PROX / packet_utils.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 <rte_lcore.h>
18 #include <rte_hash.h>
19 #include <rte_hash_crc.h>
20 #include "task_base.h"
21 #include "lconf.h"
22 #include "prefetch.h"
23 #include "log.h"
24 #include "defines.h"
25 #include "handle_master.h"
26 #include "prox_port_cfg.h"
27 #include "packet_utils.h"
28
29 static inline int find_ip(struct ether_hdr_arp *pkt, uint16_t len, uint32_t *ip_dst)
30 {
31         prox_rte_vlan_hdr *vlan_hdr;
32         prox_rte_ether_hdr *eth_hdr = (prox_rte_ether_hdr*)pkt;
33         prox_rte_ipv4_hdr *ip;
34         uint16_t ether_type = eth_hdr->ether_type;
35         uint16_t l2_len = sizeof(prox_rte_ether_hdr);
36
37         // Unstack VLAN tags
38         while (((ether_type == ETYPE_8021ad) || (ether_type == ETYPE_VLAN)) && (l2_len + sizeof(prox_rte_vlan_hdr) < len)) {
39                 vlan_hdr = (prox_rte_vlan_hdr *)((uint8_t *)pkt + l2_len);
40                 l2_len +=4;
41                 ether_type = vlan_hdr->eth_proto;
42         }
43
44         switch (ether_type) {
45         case ETYPE_MPLSU:
46         case ETYPE_MPLSM:
47                 // In case of MPLS, next hop MAC is based on MPLS, not destination IP
48                 l2_len = 0;
49                 break;
50         case ETYPE_IPv4:
51                 break;
52         case ETYPE_EoGRE:
53         case ETYPE_ARP:
54         case ETYPE_IPv6:
55                 l2_len = 0;
56                 break;
57         default:
58                 l2_len = 0;
59                 plog_warn("Unsupported packet type %x - CRC might be wrong\n", ether_type);
60                 break;
61         }
62
63         if (l2_len && (l2_len + sizeof(prox_rte_ipv4_hdr) <= len)) {
64                 prox_rte_ipv4_hdr *ip = (prox_rte_ipv4_hdr *)((uint8_t *)pkt + l2_len);
65                 // TODO: implement LPM => replace ip_dst by next hop IP DST
66                 *ip_dst = ip->dst_addr;
67                 return 0;
68         }
69         return -1;
70 }
71
72 /* This implementation could be improved: instead of checking each time we send a packet whether we need also
73    to send an ARP, we should only check whether the MAC is valid.
74    We should check arp_update_time in the master process. This would also require the generating task to clear its arp ring
75    to avoid sending many ARP while starting after a long stop.
76    We could also check for arp_timeout in the master so that dataplane has only to check whether MAC is available
77    but this would require either thread safety, or the the exchange of information between master and generating core.
78 */
79
80 int write_dst_mac(struct task_base *tbase, struct rte_mbuf *mbuf, uint32_t *ip_dst, uint64_t **time)
81 {
82         const uint64_t hz = rte_get_tsc_hz();
83         struct ether_hdr_arp *packet = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
84         prox_rte_ether_addr *mac = &packet->ether_hdr.d_addr;
85
86         uint64_t tsc = rte_rdtsc();
87         struct l3_base *l3 = &(tbase->l3);
88         if (l3->gw.ip) {
89                 if (likely((l3->flags & FLAG_DST_MAC_KNOWN) && (tsc < l3->gw.arp_update_time) && (tsc < l3->gw.arp_timeout))) {
90                         memcpy(mac, &l3->gw.mac, sizeof(prox_rte_ether_addr));
91                         return SEND_MBUF;
92                 } else if (tsc > l3->gw.arp_update_time) {
93                         // long time since we have successfully sent an arp, send arp
94                         // If sending ARP failed (ring full) then arp_update_time is not updated to avoid having to wait 1 sec to send ARP REQ again
95                         *time = &l3->gw.arp_update_time;
96                         *ip_dst = l3->gw.ip;
97                         if ((l3->flags & FLAG_DST_MAC_KNOWN) && (tsc < l3->gw.arp_timeout)){
98                                 // MAC is valid in the table => send also the mbuf
99                                 memcpy(mac, &l3->gw.mac, sizeof(prox_rte_ether_addr));
100                                 return SEND_MBUF_AND_ARP;
101                         } else {
102                                 // MAC still unknown, or timed out => only send ARP
103                                 return SEND_ARP;
104                         }
105                 } else {
106                         // MAC is unknown and we already sent an ARP recently, drop mbuf and wait for ARP reply
107                         return DROP_MBUF;
108                 }
109         }
110
111         uint16_t len = rte_pktmbuf_pkt_len(mbuf);
112         if (find_ip(packet, len, ip_dst) != 0) {
113                 // Unable to find IP address => non IP packet => send it as it
114                 return SEND_MBUF;
115         }
116         if (likely(l3->n_pkts < 4)) {
117                 for (unsigned int idx = 0; idx < l3->n_pkts; idx++) {
118                         if (*ip_dst == l3->optimized_arp_table[idx].ip) {
119                                  // IP address already in table
120                                 if ((tsc < l3->optimized_arp_table[idx].arp_update_time) && (tsc < l3->optimized_arp_table[idx].arp_timeout)) {
121                                         // MAC address was recently updated in table, use it
122                                         memcpy(mac, &l3->optimized_arp_table[idx].mac, sizeof(prox_rte_ether_addr));
123                                         return SEND_MBUF;
124                                 } else if (tsc > l3->optimized_arp_table[idx].arp_update_time) {
125                                         // ARP not sent since a long time, send ARP
126                                                 *time = &l3->optimized_arp_table[idx].arp_update_time;
127                                         if (tsc < l3->optimized_arp_table[idx].arp_timeout) {
128                                                 // MAC still valid => also send mbuf
129                                                 memcpy(mac, &l3->optimized_arp_table[idx].mac, sizeof(prox_rte_ether_addr));
130                                                 return SEND_MBUF_AND_ARP;
131                                         } else {
132                                                 // MAC unvalid => only send ARP
133                                                 return SEND_ARP;
134                                         }
135                                 } else {
136                                         //  ARP timeout elapsed, MAC not valid anymore but waiting for ARP reply
137                                         return DROP_MBUF;
138                                 }
139                         }
140                 }
141                 // IP address not found in table
142                 l3->optimized_arp_table[l3->n_pkts].ip = *ip_dst;
143                 *time = &l3->optimized_arp_table[l3->n_pkts].arp_update_time;
144                 l3->n_pkts++;
145
146                 if (l3->n_pkts < 4) {
147                         return SEND_ARP;
148                 }
149
150                 // We have too many IP addresses to search linearly; lets use hash table instead => copy all entries in hash table
151                 for (uint32_t idx = 0; idx < l3->n_pkts; idx++) {
152                         uint32_t ip = l3->optimized_arp_table[idx].ip;
153                         int ret = rte_hash_add_key(l3->ip_hash, (const void *)&ip);
154                         if (ret < 0) {
155                                 // This should not happen as few entries so far.
156                                 // If it happens, we still send the ARP as easier:
157                                 //      If the ARP corresponds to this error, the ARP reply will be ignored
158                                 //      If ARP does not correspond to this error/ip, then ARP reply will be handled.
159                                 plogx_err("Unable add ip %d.%d.%d.%d in mac_hash (already %d entries)\n", IP4(ip), idx);
160                         } else {
161                                 memcpy(&l3->arp_table[ret], &l3->optimized_arp_table[idx], sizeof(struct arp_table));
162                         }
163                 }
164                 return SEND_ARP;
165         } else {
166                 // Find IP in lookup table. Send ARP if not found
167                 int ret = rte_hash_lookup(l3->ip_hash, (const void *)ip_dst);
168                 if (unlikely(ret < 0)) {
169                         // IP not found, try to send an ARP
170                         int ret = rte_hash_add_key(l3->ip_hash, (const void *)ip_dst);
171                         if (ret < 0) {
172                                 // No reason to send ARP, as reply would be anyhow ignored
173                                 plogx_err("Unable to add ip %d.%d.%d.%d in mac_hash\n", IP4(*ip_dst));
174                                 return DROP_MBUF;
175                         } else {
176                                 l3->arp_table[ret].ip = *ip_dst;
177                                 *time = &l3->arp_table[ret].arp_update_time;
178                         }
179                         return SEND_ARP;
180                 } else {
181                         // IP has been found
182                         if (likely((tsc < l3->arp_table[ret].arp_update_time) && (tsc < l3->arp_table[ret].arp_timeout))) {
183                                 // MAC still valid and ARP sent recently
184                                 memcpy(mac, &l3->arp_table[ret].mac, sizeof(prox_rte_ether_addr));
185                                 return SEND_MBUF;
186                         } else if (tsc > l3->arp_table[ret].arp_update_time) {
187                                 // ARP not sent since a long time, send ARP
188                                 *time = &l3->arp_table[ret].arp_update_time;
189                                 if (tsc < l3->arp_table[ret].arp_timeout) {
190                                         // MAC still valid => send also MBUF
191                                         memcpy(mac, &l3->arp_table[ret].mac, sizeof(prox_rte_ether_addr));
192                                         return SEND_MBUF_AND_ARP;
193                                 } else {
194                                         return SEND_ARP;
195                                 }
196                         } else {
197                                 return DROP_MBUF;
198                         }
199                 }
200         }
201         // Should not happen
202         return DROP_MBUF;
203 }
204
205 void task_init_l3(struct task_base *tbase, struct task_args *targ)
206 {
207         static char hash_name[30];
208         uint32_t n_entries = MAX_ARP_ENTRIES * 4;
209         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
210         sprintf(hash_name, "A%03d_%03d_mac_table", targ->lconf->id, targ->id);
211
212         hash_name[0]++;
213
214         struct rte_hash_parameters hash_params = {
215                 .name = hash_name,
216                 .entries = n_entries,
217                 .key_len = sizeof(uint32_t),
218                 .hash_func = rte_hash_crc,
219                 .hash_func_init_val = 0,
220         };
221         tbase->l3.ip_hash = rte_hash_create(&hash_params);
222         PROX_PANIC(tbase->l3.ip_hash == NULL, "Failed to set up ip hash table\n");
223
224         tbase->l3.arp_table = (struct arp_table *)prox_zmalloc(n_entries * sizeof(struct arp_table), socket_id);
225         PROX_PANIC(tbase->l3.arp_table == NULL, "Failed to allocate memory for %u entries in arp table\n", n_entries);
226         plog_info("\tarp table, with %d entries of size %ld\n", n_entries, sizeof(struct l3_base));
227
228         targ->lconf->ctrl_func_p[targ->task] = handle_ctrl_plane_pkts;
229         targ->lconf->ctrl_timeout = freq_to_tsc(targ->ctrl_freq);
230         tbase->l3.gw.ip = rte_cpu_to_be_32(targ->gateway_ipv4);
231         tbase->flags |= TASK_L3;
232         tbase->l3.core_id = targ->lconf->id;
233         tbase->l3.task_id = targ->id;
234         tbase->l3.tmaster = targ->tmaster;
235         tbase->l3.seed = (uint)rte_rdtsc();
236         if (targ->arp_timeout != 0)
237                 tbase->l3.arp_timeout = targ->arp_timeout;
238         else
239                 tbase->l3.arp_timeout = DEFAULT_ARP_TIMEOUT;
240         if (targ->arp_update_time != 0)
241                 tbase->l3.arp_update_time = targ->arp_update_time;
242         else
243                 tbase->l3.arp_update_time = DEFAULT_ARP_UPDATE_TIME;
244 }
245
246 void task_start_l3(struct task_base *tbase, struct task_args *targ)
247 {
248         const int NB_ARP_MBUF = 1024;
249         const int ARP_MBUF_SIZE = 2048;
250         const int NB_CACHE_ARP_MBUF = 256;
251
252         struct prox_port_cfg *port = find_reachable_port(targ);
253         if (port && (tbase->l3.arp_pool == NULL)) {
254                 static char name[] = "arp0_pool";
255                 tbase->l3.reachable_port_id = port - prox_port_cfg;
256                 if (targ->local_ipv4) {
257                         tbase->local_ipv4 = rte_be_to_cpu_32(targ->local_ipv4);
258                         register_ip_to_ctrl_plane(tbase->l3.tmaster, tbase->local_ipv4, tbase->l3.reachable_port_id, targ->lconf->id, targ->id);
259                 }
260                 master_init_vdev(tbase->l3.tmaster, tbase->l3.reachable_port_id, targ->lconf->id, targ->id);
261                 name[3]++;
262                 struct rte_mempool *ret = rte_mempool_create(name, NB_ARP_MBUF, ARP_MBUF_SIZE, NB_CACHE_ARP_MBUF,
263                         sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, 0,
264                         rte_socket_id(), 0);
265                 PROX_PANIC(ret == NULL, "Failed to allocate ARP memory pool on socket %u with %u elements\n",
266                         rte_socket_id(), NB_ARP_MBUF);
267                 plog_info("\t\tMempool %p (%s) size = %u * %u cache %u, socket %d\n", ret, name, NB_ARP_MBUF,
268                         ARP_MBUF_SIZE, NB_CACHE_ARP_MBUF, rte_socket_id());
269                 tbase->l3.arp_pool = ret;
270         }
271 }
272
273 void task_set_gateway_ip(struct task_base *tbase, uint32_t ip)
274 {
275         tbase->l3.gw.ip = ip;
276         tbase->flags &= ~FLAG_DST_MAC_KNOWN;
277 }
278
279 void task_set_local_ip(struct task_base *tbase, uint32_t ip)
280 {
281         tbase->local_ipv4 = ip;
282 }
283
284 static void reset_arp_update_time(struct l3_base *l3, uint32_t ip)
285 {
286         uint32_t idx;
287         plogx_info("\tMAC entry for IP "IPv4_BYTES_FMT" timeout in kernel\n", IP4(ip));
288         if (ip == l3->gw.ip) {
289                 l3->gw.arp_update_time = 0;
290         } else if (l3->n_pkts < 4) {
291                 for (idx = 0; idx < l3->n_pkts; idx++) {
292                         uint32_t ip_dst = l3->optimized_arp_table[idx].ip;
293                         if (ip_dst == ip)
294                                 break;
295                 }
296                 if (idx < l3->n_pkts) {
297                         l3->optimized_arp_table[idx].arp_update_time = 0;
298                 }
299         } else {
300                 int ret = rte_hash_lookup(l3->ip_hash, (const void *)&ip);
301                 if (ret >= 0)
302                         l3->arp_table[ret].arp_update_time = 0;
303         }
304         return;
305 }
306
307 void handle_ctrl_plane_pkts(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
308 {
309         uint8_t out[1];
310         const uint64_t hz = rte_get_tsc_hz();
311         uint32_t ip, ip_dst, idx;
312         int j;
313         uint16_t command;
314         struct ether_hdr_arp *hdr;
315         struct l3_base *l3 = &tbase->l3;
316         uint64_t tsc= rte_rdtsc();
317         uint64_t update_time = l3->arp_timeout * hz / 1000;
318
319         for (j = 0; j < n_pkts; ++j) {
320                 PREFETCH0(mbufs[j]);
321         }
322         for (j = 0; j < n_pkts; ++j) {
323                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
324         }
325
326         for (j = 0; j < n_pkts; ++j) {
327                 out[0] = OUT_HANDLED;
328                 command = mbufs[j]->udata64 & 0xFFFF;
329                 plogx_dbg("\tReceived %s mbuf %p\n", actions_string[command], mbufs[j]);
330                 switch(command) {
331                 case UPDATE_FROM_CTRL:
332                         hdr = rte_pktmbuf_mtod(mbufs[j], struct ether_hdr_arp *);
333                         ip = (mbufs[j]->udata64 >> 32) & 0xFFFFFFFF;
334
335                         if (prox_rte_is_zero_ether_addr(&hdr->arp.data.sha)) {
336                                 // MAC timeout or deleted from kernel table => reset update_time
337                                 // This will cause us to send new ARP request
338                                 // However, as arp_timeout not touched, we should continue sending our regular IP packets
339                                 reset_arp_update_time(l3, ip);
340                                 plogx_info("\tTimeout for MAC entry for IP "IPv4_BYTES_FMT"\n", IP4(ip));
341                                 return;
342                         } else
343                                 plogx_dbg("\tUpdating MAC entry for IP "IPv4_BYTES_FMT" with MAC "MAC_BYTES_FMT"\n",
344                                         IP4(ip), MAC_BYTES(hdr->arp.data.sha.addr_bytes));
345                         if (ip == l3->gw.ip) {
346                                 // MAC address of the gateway
347                                 memcpy(&l3->gw.mac, &hdr->arp.data.sha, 6);
348                                 l3->flags |= FLAG_DST_MAC_KNOWN;
349                                 l3->gw.arp_timeout = tsc + update_time;
350                                 update_arp_update_time(l3, &l3->gw.arp_update_time, l3->arp_update_time);
351                         } else if (l3->n_pkts < 4) {
352                                 // Few packets tracked - should be faster to loop through them thean using a hash table
353                                 for (idx = 0; idx < l3->n_pkts; idx++) {
354                                         ip_dst = l3->optimized_arp_table[idx].ip;
355                                         if (ip_dst == ip)
356                                                 break;
357                                 }
358                                 if (idx < l3->n_pkts) {
359                                         memcpy(&l3->optimized_arp_table[idx].mac, &(hdr->arp.data.sha), sizeof(prox_rte_ether_addr));
360                                         l3->optimized_arp_table[idx].arp_timeout = tsc + update_time;
361                                         update_arp_update_time(l3, &l3->optimized_arp_table[idx].arp_update_time, l3->arp_update_time);
362                                 }
363                         } else {
364                                 int ret = rte_hash_add_key(l3->ip_hash, (const void *)&ip);
365                                 if (ret < 0) {
366                                         plogx_info("Unable add ip %d.%d.%d.%d in mac_hash\n", IP4(ip));
367                                 } else {
368                                         memcpy(&l3->arp_table[ret].mac, &(hdr->arp.data.sha), sizeof(prox_rte_ether_addr));
369                                         l3->arp_table[ret].arp_timeout = tsc + update_time;
370                                         update_arp_update_time(l3, &l3->arp_table[ret].arp_update_time, l3->arp_update_time);
371                                 }
372                         }
373                         tx_drop(mbufs[j]);
374                         break;
375                 case ARP_REPLY_FROM_CTRL:
376                 case ICMP_FROM_CTRL:
377                 case ARP_REQ_FROM_CTRL:
378                 case PKT_FROM_TAP:
379                         out[0] = 0;
380                         // tx_ctrlplane_pkt does not drop packets
381                         plogx_dbg("\tForwarding (ARP/PING) packet from master\n");
382                         tbase->aux->tx_ctrlplane_pkt(tbase, &mbufs[j], 1, out);
383                         TASK_STATS_ADD_TX_NON_DP(&tbase->aux->stats, 1);
384                         break;
385                 }
386         }
387 }