Merge changes from PROX-v041
[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 #define IP4(x) x & 0xff, (x >> 8) & 0xff, (x >> 16) & 0xff, x >> 24
28
29 static inline int find_ip(struct ether_hdr_arp *pkt, uint16_t len, uint32_t *ip_dst)
30 {
31         struct vlan_hdr *vlan_hdr;
32         struct ether_hdr *eth_hdr = (struct ether_hdr*)pkt;
33         struct ipv4_hdr *ip;
34         uint16_t ether_type = eth_hdr->ether_type;
35         uint16_t l2_len = sizeof(struct ether_hdr);
36
37         // Unstack VLAN tags
38         while (((ether_type == ETYPE_8021ad) || (ether_type == ETYPE_VLAN)) && (l2_len + sizeof(struct vlan_hdr) < len)) {
39                 vlan_hdr = (struct 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(struct ipv4_hdr) <= len)) {
64                 struct ipv4_hdr *ip = (struct 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 int write_dst_mac(struct task_base *tbase, struct rte_mbuf *mbuf, uint32_t *ip_dst)
73 {
74         const uint64_t hz = rte_get_tsc_hz();
75         struct ether_hdr_arp *packet = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
76         struct ether_addr *mac = &packet->ether_hdr.d_addr;
77
78         uint64_t tsc = rte_rdtsc();
79         struct l3_base *l3 = &(tbase->l3);
80         if (l3->gw.ip) {
81                 if (likely((l3->flags & FLAG_DST_MAC_KNOWN) && (tsc < l3->gw.arp_update_time) && (tsc < l3->gw.arp_timeout))) {
82                         memcpy(mac, &l3->gw.mac, sizeof(struct ether_addr));
83                         return 0;
84                 } else if (tsc > l3->gw.arp_update_time) {
85                         // long time since we have sent an arp, send arp
86                         l3->gw.arp_update_time = tsc + hz;
87                         *ip_dst = l3->gw.ip;
88                         return -1;
89                 }
90                 return -2;
91         }
92
93         uint16_t len = rte_pktmbuf_pkt_len(mbuf);
94         if (find_ip(packet, len, ip_dst) != 0) {
95                 return 0;
96         }
97         if (likely(l3->n_pkts < 4)) {
98                 for (unsigned int idx = 0; idx < l3->n_pkts; idx++) {
99                         if (*ip_dst == l3->optimized_arp_table[idx].ip) {
100                                 if ((tsc < l3->optimized_arp_table[idx].arp_update_time) && (tsc < l3->optimized_arp_table[idx].arp_timeout)) {
101                                         memcpy(mac, &l3->optimized_arp_table[idx].mac, sizeof(struct ether_addr));
102                                         return 0;
103                                 } else if (tsc > l3->optimized_arp_table[idx].arp_update_time) {
104                                         l3->optimized_arp_table[idx].arp_update_time = tsc + hz;
105                                         return -1;
106                                 } else {
107                                         return -2;
108                                 }
109                         }
110                 }
111                 l3->optimized_arp_table[l3->n_pkts].ip = *ip_dst;
112                 l3->optimized_arp_table[l3->n_pkts].arp_update_time = tsc + hz;
113                 l3->n_pkts++;
114
115                 if (l3->n_pkts < 4)
116                         return -1;
117
118                 // We have ** many ** IP addresses; lets use hash table instead
119                 for (uint32_t idx = 0; idx < l3->n_pkts; idx++) {
120                         uint32_t ip = l3->optimized_arp_table[idx].ip;
121                         int ret = rte_hash_add_key(l3->ip_hash, (const void *)&ip);
122                         if (ret < 0) {
123                                 plogx_info("Unable add ip %d.%d.%d.%d in mac_hash\n", IP4(ip));
124                         } else {
125                                 memcpy(&l3->arp_table[ret], &l3->optimized_arp_table[idx], sizeof(struct arp_table));
126                         }
127                 }
128                 return -1;
129         } else {
130                 // Find mac in lookup table. Send ARP if not found
131                 int ret = rte_hash_lookup(l3->ip_hash, (const void *)ip_dst);
132                 if (unlikely(ret < 0)) {
133                         int ret = rte_hash_add_key(l3->ip_hash, (const void *)ip_dst);
134                         if (ret < 0) {
135                                 plogx_info("Unable add ip %d.%d.%d.%d in mac_hash\n", IP4(*ip_dst));
136                                 return -2;
137                         } else {
138                                 l3->arp_table[ret].ip = *ip_dst;
139                                 l3->arp_table[ret].arp_update_time = tsc + hz;
140                         }
141                         return -1;
142                 } else {
143                         if ((tsc < l3->arp_table[ret].arp_update_time) && (tsc < l3->arp_table[ret].arp_timeout)) {
144                                 memcpy(mac, &l3->arp_table[ret].mac, sizeof(struct ether_addr));
145                                 return 0;
146                         } else if (tsc > l3->arp_table[ret].arp_update_time) {
147                                 l3->arp_table[ret].arp_update_time = tsc + hz;
148                                 return -1;
149                         } else {
150                                 return -2;
151                         }
152                 }
153         }
154         return 0;
155 }
156
157 void task_init_l3(struct task_base *tbase, struct task_args *targ)
158 {
159         static char hash_name[30];
160         uint32_t n_entries = MAX_ARP_ENTRIES * 4;
161         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
162         sprintf(hash_name, "A%03d_mac_table", targ->lconf->id);
163
164         hash_name[0]++;
165
166         struct rte_hash_parameters hash_params = {
167                 .name = hash_name,
168                 .entries = n_entries,
169                 .key_len = sizeof(uint32_t),
170                 .hash_func = rte_hash_crc,
171                 .hash_func_init_val = 0,
172         };
173         tbase->l3.ip_hash = rte_hash_create(&hash_params);
174         PROX_PANIC(tbase->l3.ip_hash == NULL, "Failed to set up ip hash table\n");
175
176         tbase->l3.arp_table = (struct arp_table *)prox_zmalloc(n_entries * sizeof(struct arp_table), socket_id);
177         PROX_PANIC(tbase->l3.arp_table == NULL, "Failed to allocate memory for %u entries in arp table\n", n_entries);
178         plog_info("\tarp table, with %d entries of size %ld\n", n_entries, sizeof(struct l3_base));
179
180         targ->lconf->ctrl_func_p[targ->task] = handle_ctrl_plane_pkts;
181         targ->lconf->ctrl_timeout = freq_to_tsc(targ->ctrl_freq);
182         tbase->l3.gw.ip = rte_cpu_to_be_32(targ->gateway_ipv4);
183         tbase->flags |= TASK_L3;
184         tbase->l3.core_id = targ->lconf->id;
185         tbase->l3.task_id = targ->id;
186         tbase->l3.tmaster = targ->tmaster;
187 }
188
189 void task_start_l3(struct task_base *tbase, struct task_args *targ)
190 {
191         struct prox_port_cfg *port = find_reachable_port(targ);
192         if (port) {
193                 tbase->l3.reachable_port_id = port - prox_port_cfg;
194                 if (targ->local_ipv4) {
195                         tbase->local_ipv4 = rte_be_to_cpu_32(targ->local_ipv4);
196                         register_ip_to_ctrl_plane(tbase->l3.tmaster, tbase->local_ipv4, tbase->l3.reachable_port_id, targ->lconf->id, targ->id);
197                 }
198         }
199 }
200
201 void task_set_gateway_ip(struct task_base *tbase, uint32_t ip)
202 {
203         tbase->l3.gw.ip = ip;
204         tbase->flags &= ~FLAG_DST_MAC_KNOWN;
205 }
206
207 void task_set_local_ip(struct task_base *tbase, uint32_t ip)
208 {
209         tbase->local_ipv4 = ip;
210 }
211
212 void handle_ctrl_plane_pkts(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
213 {
214         uint8_t out[1];
215         const uint64_t hz = rte_get_tsc_hz();
216         uint32_t ip, ip_dst, idx;
217         int j;
218         uint16_t command;
219         struct ether_hdr_arp *hdr;
220         struct l3_base *l3 = &tbase->l3;
221         uint64_t tsc= rte_rdtsc();
222
223         for (j = 0; j < n_pkts; ++j) {
224                 PREFETCH0(mbufs[j]);
225         }
226         for (j = 0; j < n_pkts; ++j) {
227                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
228         }
229
230         for (j = 0; j < n_pkts; ++j) {
231                 out[0] = OUT_HANDLED;
232                 command = mbufs[j]->udata64 & 0xFFFF;
233                 plogx_dbg("\tReceived %s mbuf %p\n", actions_string[command], mbufs[j]);
234                 switch(command) {
235                 case UPDATE_FROM_CTRL:
236                         hdr = rte_pktmbuf_mtod(mbufs[j], struct ether_hdr_arp *);
237                         ip = (mbufs[j]->udata64 >> 32) & 0xFFFFFFFF;
238
239                         if (ip == l3->gw.ip) {
240                                 // MAC address of the gateway
241                                 memcpy(&l3->gw.mac, &hdr->arp.data.sha, 6);
242                                 l3->flags |= FLAG_DST_MAC_KNOWN;
243                                 l3->gw.arp_timeout = tsc + 30 * hz;
244                         } else if (l3->n_pkts < 4) {
245                                 // Few packets tracked - should be faster to loop through them thean using a hash table
246                                 for (idx = 0; idx < l3->n_pkts; idx++) {
247                                         ip_dst = l3->optimized_arp_table[idx].ip;
248                                         if (ip_dst == ip)
249                                                 break;
250                                 }
251                                 if (idx < l3->n_pkts) {
252                                         // IP not found; this is a reply while we never asked for the request!
253                                         memcpy(&l3->optimized_arp_table[idx].mac, &(hdr->arp.data.sha), sizeof(struct ether_addr));
254                                         l3->optimized_arp_table[idx].arp_timeout = tsc + 30 * hz;
255                                 }
256                         } else {
257                                 int ret = rte_hash_add_key(l3->ip_hash, (const void *)&ip);
258                                 if (ret < 0) {
259                                         plogx_info("Unable add ip %d.%d.%d.%d in mac_hash\n", IP4(ip));
260                                 } else {
261                                         memcpy(&l3->arp_table[ret].mac, &(hdr->arp.data.sha), sizeof(struct ether_addr));
262                                         l3->arp_table[ret].arp_timeout = tsc + 30 * hz;
263                                 }
264                         }
265                         tx_drop(mbufs[j]);
266                         break;
267                 case ARP_REPLY_FROM_CTRL:
268                 case ARP_REQ_FROM_CTRL:
269                         out[0] = 0;
270                         tbase->aux->tx_pkt_l2(tbase, &mbufs[j], 1, out);
271                         break;
272                 }
273         }
274 }