2 // Copyright (c) 2010-2017 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <rte_lcore.h>
19 #include <rte_hash_crc.h>
20 #include "task_base.h"
24 #include "handle_master.h"
25 #include "prox_port_cfg.h"
27 #define IP4(x) x & 0xff, (x >> 8) & 0xff, (x >> 16) & 0xff, x >> 24
29 static inline int find_ip(struct ether_hdr_arp *pkt, uint16_t len, uint32_t *ip_dst)
31 struct vlan_hdr *vlan_hdr;
32 struct ether_hdr *eth_hdr = (struct ether_hdr*)pkt;
34 uint16_t ether_type = eth_hdr->ether_type;
35 uint16_t l2_len = sizeof(struct ether_hdr);
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);
41 ether_type = vlan_hdr->eth_proto;
47 // In case of MPLS, next hop MAC is based on MPLS, not destination IP
59 plog_warn("Unsupported packet type %x - CRC might be wrong\n", ether_type);
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;
72 int write_dst_mac(struct task_base *tbase, struct rte_mbuf *mbuf, uint32_t *ip_dst)
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;
78 uint64_t tsc = rte_rdtsc();
79 struct l3_base *l3 = &(tbase->l3);
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));
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;
93 uint16_t len = rte_pktmbuf_pkt_len(mbuf);
94 if (find_ip(packet, len, ip_dst) != 0) {
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));
103 } else if (tsc > l3->optimized_arp_table[idx].arp_update_time) {
104 l3->optimized_arp_table[idx].arp_update_time = tsc + hz;
111 l3->optimized_arp_table[l3->n_pkts].ip = *ip_dst;
112 l3->optimized_arp_table[l3->n_pkts].arp_update_time = tsc + hz;
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);
123 plogx_info("Unable add ip %d.%d.%d.%d in mac_hash\n", IP4(ip));
125 memcpy(&l3->arp_table[ret], &l3->optimized_arp_table[idx], sizeof(struct arp_table));
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);
135 plogx_info("Unable add ip %d.%d.%d.%d in mac_hash\n", IP4(*ip_dst));
138 l3->arp_table[ret].ip = *ip_dst;
139 l3->arp_table[ret].arp_update_time = tsc + hz;
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));
146 } else if (tsc > l3->arp_table[ret].arp_update_time) {
147 l3->arp_table[ret].arp_update_time = tsc + hz;
157 void task_init_l3(struct task_base *tbase, struct task_args *targ)
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);
166 struct rte_hash_parameters hash_params = {
168 .entries = n_entries,
169 .key_len = sizeof(uint32_t),
170 .hash_func = rte_hash_crc,
171 .hash_func_init_val = 0,
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");
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));
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;
189 void task_start_l3(struct task_base *tbase, struct task_args *targ)
191 struct prox_port_cfg *port = find_reachable_port(targ);
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);
201 void task_set_gateway_ip(struct task_base *tbase, uint32_t ip)
203 tbase->l3.gw.ip = ip;
204 tbase->flags &= ~FLAG_DST_MAC_KNOWN;
207 void task_set_local_ip(struct task_base *tbase, uint32_t ip)
209 tbase->local_ipv4 = ip;
212 void handle_ctrl_plane_pkts(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
215 const uint64_t hz = rte_get_tsc_hz();
216 uint32_t ip, ip_dst, idx;
219 struct ether_hdr_arp *hdr;
220 struct l3_base *l3 = &tbase->l3;
221 uint64_t tsc= rte_rdtsc();
223 for (j = 0; j < n_pkts; ++j) {
226 for (j = 0; j < n_pkts; ++j) {
227 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
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]);
235 case UPDATE_FROM_CTRL:
236 hdr = rte_pktmbuf_mtod(mbufs[j], struct ether_hdr_arp *);
237 ip = (mbufs[j]->udata64 >> 32) & 0xFFFFFFFF;
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;
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;
257 int ret = rte_hash_add_key(l3->ip_hash, (const void *)&ip);
259 plogx_info("Unable add ip %d.%d.%d.%d in mac_hash\n", IP4(ip));
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;
267 case ARP_REPLY_FROM_CTRL:
268 case ARP_REQ_FROM_CTRL:
270 tbase->aux->tx_pkt_l2(tbase, &mbufs[j], 1, out);