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.
18 #include <rte_hash_crc.h>
21 #include "prox_globals.h"
24 #include "handle_master.h"
26 #include "mbuf_utils.h"
30 #include "prox_malloc.h"
32 #include "task_init.h"
33 #include "prox_port_cfg.h"
39 #define PROX_MAX_ARP_REQUESTS 32 // Maximum number of tasks requesting the same MAC address
41 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 static struct my_arp_t arp_reply = {
50 static struct my_arp_t arp_request = {
59 struct ether_addr mac;
60 struct rte_ring *ring;
63 struct external_ip_table {
64 struct ether_addr mac;
65 struct rte_ring *rings[PROX_MAX_ARP_REQUESTS];
70 struct ether_addr mac;
71 struct rte_ring *ring;
78 struct task_base base;
79 struct rte_ring *ctrl_rx_ring;
80 struct rte_ring **ctrl_tx_rings;
81 struct ip_table *internal_ip_table;
82 struct external_ip_table *external_ip_table;
83 struct rte_hash *external_ip_hash;
84 struct rte_hash *internal_ip_hash;
85 struct port_table internal_port_table[PROX_MAX_PORTS];
91 } __attribute__((packed));
93 static inline uint8_t get_command(struct rte_mbuf *mbuf)
95 return mbuf->udata64 & 0xFF;
97 static inline uint8_t get_task(struct rte_mbuf *mbuf)
99 return (mbuf->udata64 >> 8) & 0xFF;
101 static inline uint8_t get_core(struct rte_mbuf *mbuf)
103 return (mbuf->udata64 >> 16) & 0xFF;
105 static inline uint8_t get_port(struct rte_mbuf *mbuf)
109 static inline uint32_t get_ip(struct rte_mbuf *mbuf)
111 return (mbuf->udata64 >> 32) & 0xFFFFFFFF;
114 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 struct task_master *task = (struct task_master *)tbase;
118 plogx_dbg("\tregistering IP %d.%d.%d.%d with port %d core %d and task %d\n", IP4(ip), port_id, core_id, task_id);
120 if (port_id >= PROX_MAX_PORTS) {
121 plog_err("Unable to register ip %d.%d.%d.%d, port %d\n", IP4(ip), port_id);
125 /* TODO - stoe multiple rings if multiple cores able to handle IP
126 Remove them when such cores are stopped and de-register IP
128 task->internal_port_table[port_id].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
129 memcpy(&task->internal_port_table[port_id].mac, &prox_port_cfg[port_id].eth_addr, 6);
130 task->internal_port_table[port_id].ip = ip;
132 if (ip == RANDOM_IP) {
133 task->internal_port_table[port_id].flags |= HANDLE_RANDOM_IP_FLAG;
139 int ret = rte_hash_add_key(task->internal_ip_hash, (const void *)&key);
140 if (unlikely(ret < 0)) {
141 plog_err("Unable to register ip %d.%d.%d.%d\n", IP4(ip));
144 memcpy(&task->internal_ip_table[ret].mac, &prox_port_cfg[port_id].eth_addr, 6);
145 task->internal_ip_table[ret].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
149 static inline void handle_arp_reply(struct task_base *tbase, struct rte_mbuf *mbuf)
151 struct task_master *task = (struct task_master *)tbase;
152 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
154 uint32_t key = hdr_arp->arp.data.spa;
155 plogx_dbg("\tMaster handling ARP reply for ip %d.%d.%d.%d\n", IP4(key));
157 ret = rte_hash_lookup(task->external_ip_hash, (const void *)&key);
158 if (unlikely(ret < 0)) {
159 // entry not found for this IP: we did not ask a request, delete the reply
162 // entry found for this IP
163 uint16_t nb_requests = task->external_ip_table[ret].nb_requests;
164 memcpy(&hdr_arp->ether_hdr.d_addr.addr_bytes, &task->external_ip_table[ret].mac, 6);
165 // If we receive a request from multiple task for the same IP, then we update all tasks
166 if (task->external_ip_table[ret].nb_requests) {
167 rte_mbuf_refcnt_set(mbuf, nb_requests);
168 for (int i = 0; i < nb_requests; i++) {
169 struct rte_ring *ring = task->external_ip_table[ret].rings[i];
170 tx_ring_ip(tbase, ring, UPDATE_FROM_CTRL, mbuf, key);
172 task->external_ip_table[ret].nb_requests = 0;
179 static inline void handle_arp_request(struct task_base *tbase, struct rte_mbuf *mbuf)
181 struct task_master *task = (struct task_master *)tbase;
182 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
184 uint8_t port = get_port(mbuf);
187 key.ip = hdr_arp->arp.data.tpa;
189 if (task->internal_port_table[port].flags & HANDLE_RANDOM_IP_FLAG) {
190 struct ether_addr mac;
191 plogx_dbg("\tMaster handling ARP request for ip %d.%d.%d.%d on port %d which supports random ip\n", IP4(key.ip), key.port);
192 struct rte_ring *ring = task->internal_port_table[port].ring;
193 create_mac(hdr_arp, &mac);
194 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
195 build_arp_reply(hdr_arp, &mac);
196 tx_ring(tbase, ring, ARP_REPLY_FROM_CTRL, mbuf);
200 plogx_dbg("\tMaster handling ARP request for ip %d.%d.%d.%d\n", IP4(key.ip));
202 ret = rte_hash_lookup(task->internal_ip_hash, (const void *)&key);
203 if (unlikely(ret < 0)) {
204 // entry not found for this IP.
205 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);
208 struct rte_ring *ring = task->internal_ip_table[ret].ring;
209 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
210 build_arp_reply(hdr_arp, &task->internal_ip_table[ret].mac);
211 tx_ring(tbase, ring, ARP_REPLY_FROM_CTRL, mbuf);
215 static inline void handle_unknown_ip(struct task_base *tbase, struct rte_mbuf *mbuf)
217 struct task_master *task = (struct task_master *)tbase;
218 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
219 uint8_t port = get_port(mbuf);
220 uint32_t ip_dst = get_ip(mbuf);
223 plogx_dbg("\tMaster handling unknown ip %d.%d.%d.%d for port %d\n", IP4(ip_dst), port);
224 if (unlikely(port >= PROX_MAX_PORTS)) {
225 plogx_dbg("Port %d not found", port);
229 uint32_t ip_src = task->internal_port_table[port].ip;
230 struct rte_ring *ring = task->ctrl_tx_rings[get_core(mbuf) * MAX_TASKS_PER_CORE + get_task(mbuf)];
233 plogx_dbg("Port %d not registered", port);
238 ret2 = rte_hash_add_key(task->external_ip_hash, (const void *)&ip_dst);
239 if (unlikely(ret2 < 0)) {
240 // entry not found for this IP: delete the reply
241 plogx_dbg("Unable to add IP %d.%d.%d.%d in external_ip_hash\n", IP4(ip_dst));
246 // If multiple tasks requesting the same info, we will need to send a reply to all of them
247 // However if one task sends multiple requests to the same IP (e.g. because it is not answering)
248 // then we should not send multiple replies to the same task
249 if (task->external_ip_table[ret2].nb_requests >= PROX_MAX_ARP_REQUESTS) {
250 // This can only happen if really many tasks requests the same IP
251 plogx_dbg("Unable to add request for IP %d.%d.%d.%d in external_ip_table\n", IP4(ip_dst));
255 for (i = 0; i < task->external_ip_table[ret2].nb_requests; i++) {
256 if (task->external_ip_table[ret2].rings[i] == ring)
259 if (i >= task->external_ip_table[ret2].nb_requests) {
260 // If this is a new request i.e. a new task requesting a new IP
261 task->external_ip_table[ret2].rings[task->external_ip_table[ret2].nb_requests] = ring;
262 task->external_ip_table[ret2].nb_requests++;
263 // Only needed for first request - but avoid test and copy the same 6 bytes
264 // In most cases we will only have one request per IP.
265 memcpy(&task->external_ip_table[ret2].mac, &task->internal_port_table[port].mac, 6);
268 // We send an ARP request even if one was just sent (and not yet answered) by another task
269 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
270 build_arp_request(mbuf, &task->internal_port_table[port].mac, ip_dst, ip_src);
271 tx_ring(tbase, ring, ARP_REQ_FROM_CTRL, mbuf);
274 static inline void handle_message(struct task_base *tbase, struct rte_mbuf *mbuf, int ring_id)
276 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
277 int command = get_command(mbuf);
279 plogx_dbg("\tMaster received %s (%x) from mbuf %p\n", actions_string[command], command, mbuf);
283 if (hdr_arp->ether_hdr.ether_type != ETYPE_ARP) {
285 plog_err("\tUnexpected message received: ARP_TO_CTRL with ether_type %x\n", hdr_arp->ether_hdr.ether_type);
287 } else if (arp_is_gratuitous(hdr_arp)) {
288 plog_info("\tReceived gratuitous packet \n");
291 } else if (memcmp(&hdr_arp->arp, &arp_reply, 8) == 0) {
292 uint32_t ip = hdr_arp->arp.data.spa;
293 handle_arp_reply(tbase, mbuf);
294 } else if (memcmp(&hdr_arp->arp, &arp_request, 8) == 0) {
295 handle_arp_request(tbase, mbuf);
297 plog_info("\tReceived unexpected ARP operation %d\n", hdr_arp->arp.oper);
302 case REQ_MAC_TO_CTRL:
303 handle_unknown_ip(tbase, mbuf);
306 plogx_dbg("\tMaster received unexpected message\n");
312 void init_ctrl_plane(struct task_base *tbase)
314 prox_cfg.flags |= DSF_CTRL_PLANE_ENABLED;
315 struct task_master *task = (struct task_master *)tbase;
316 int socket = rte_lcore_to_socket_id(prox_cfg.master);
317 uint32_t n_entries = MAX_ARP_ENTRIES * 4;
318 static char hash_name[30];
319 sprintf(hash_name, "A%03d_hash_arp_table", prox_cfg.master);
320 struct rte_hash_parameters hash_params = {
322 .entries = n_entries,
323 .key_len = sizeof(uint32_t),
324 .hash_func = rte_hash_crc,
325 .hash_func_init_val = 0,
327 task->external_ip_hash = rte_hash_create(&hash_params);
328 PROX_PANIC(task->external_ip_hash == NULL, "Failed to set up external ip hash\n");
329 plog_info("\texternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
330 task->external_ip_table = (struct external_ip_table *)prox_zmalloc(n_entries * sizeof(struct external_ip_table), socket);
331 PROX_PANIC(task->external_ip_table == NULL, "Failed to allocate memory for %u entries in external ip table\n", n_entries);
332 plog_info("\texternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct external_ip_table));
335 hash_params.key_len = sizeof(struct ip_port);
336 task->internal_ip_hash = rte_hash_create(&hash_params);
337 PROX_PANIC(task->internal_ip_hash == NULL, "Failed to set up internal ip hash\n");
338 plog_info("\tinternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
339 task->internal_ip_table = (struct ip_table *)prox_zmalloc(n_entries * sizeof(struct ip_table), socket);
340 PROX_PANIC(task->internal_ip_table == NULL, "Failed to allocate memory for %u entries in internal ip table\n", n_entries);
341 plog_info("\tinternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct ip_table));
344 static int handle_ctrl_plane_f(struct task_base *tbase, __attribute__((unused)) struct rte_mbuf **mbuf, uint16_t n_pkts)
346 int ring_id = 0, j, ret = 0;
347 struct rte_mbuf *mbufs[MAX_RING_BURST];
348 struct task_master *task = (struct task_master *)tbase;
350 /* Handle_master works differently than other handle functions
351 It is not handled by a DPDK dataplane core
352 It is no thread_generic based, hence do not receive packets the same way
355 ret = ring_deq(task->ctrl_rx_ring, mbufs);
356 for (j = 0; j < ret; j++) {
357 handle_message(tbase, mbufs[j], ring_id);
362 static void init_task_master(struct task_base *tbase, struct task_args *targs)
364 if (prox_cfg.flags & DSF_CTRL_PLANE_ENABLED) {
365 struct task_master *task = (struct task_master *)tbase;
367 task->ctrl_rx_ring = targs->lconf->ctrl_rings_p[0];
368 task->ctrl_tx_rings = ctrl_rings;
369 init_ctrl_plane(tbase);
370 handle_ctrl_plane = handle_ctrl_plane_f;
374 static struct task_init task_init_master = {
375 .mode_str = "master",
376 .init = init_task_master,
378 .flag_features = TASK_FEATURE_NEVER_DISCARDS,
379 .size = sizeof(struct task_master)
382 __attribute__((constructor)) static void reg_task_gen(void)
384 reg_task(&task_init_master);