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