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 IP4(x) x & 0xff, (x >> 8) & 0xff, (x >> 16) & 0xff, x >> 24
40 #define PROX_MAX_ARP_REQUESTS 32 // Maximum number of tasks requesting the same MAC address
42 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"};
44 static struct my_arp_t arp_reply = {
51 static struct my_arp_t arp_request = {
60 struct ether_addr mac;
61 struct rte_ring *ring;
64 struct external_ip_table {
65 struct ether_addr mac;
66 struct rte_ring *rings[PROX_MAX_ARP_REQUESTS];
71 struct ether_addr mac;
72 struct rte_ring *ring;
79 struct task_base base;
80 struct rte_ring *ctrl_rx_ring;
81 struct rte_ring **ctrl_tx_rings;
82 struct ip_table *internal_ip_table;
83 struct external_ip_table *external_ip_table;
84 struct rte_hash *external_ip_hash;
85 struct rte_hash *internal_ip_hash;
86 struct port_table internal_port_table[PROX_MAX_PORTS];
92 } __attribute__((packed));
94 static inline uint8_t get_command(struct rte_mbuf *mbuf)
96 return mbuf->udata64 & 0xFF;
98 static inline uint8_t get_task(struct rte_mbuf *mbuf)
100 return (mbuf->udata64 >> 8) & 0xFF;
102 static inline uint8_t get_core(struct rte_mbuf *mbuf)
104 return (mbuf->udata64 >> 16) & 0xFF;
106 static inline uint8_t get_port(struct rte_mbuf *mbuf)
110 static inline uint32_t get_ip(struct rte_mbuf *mbuf)
112 return (mbuf->udata64 >> 32) & 0xFFFFFFFF;
115 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)
117 struct task_master *task = (struct task_master *)tbase;
119 plogx_dbg("\tregistering IP %x.%x.%x.%x with port %d core %d and task %d\n", IP4(ip), port_id, core_id, task_id);
121 if (port_id >= PROX_MAX_PORTS) {
122 plog_err("Unable to register ip %x, port %d\n", ip, port_id);
126 /* TODO - stoe multiple rings if multiple cores able to handle IP
127 Remove them when such cores are stopped and de-register IP
129 task->internal_port_table[port_id].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
130 memcpy(&task->internal_port_table[port_id].mac, &prox_port_cfg[port_id].eth_addr, 6);
131 task->internal_port_table[port_id].ip = ip;
133 if (ip == RANDOM_IP) {
134 task->internal_port_table[port_id].flags |= HANDLE_RANDOM_IP_FLAG;
140 int ret = rte_hash_add_key(task->internal_ip_hash, (const void *)&key);
141 if (unlikely(ret < 0)) {
142 plog_err("Unable to register ip %x\n", ip);
145 memcpy(&task->internal_ip_table[ret].mac, &prox_port_cfg[port_id].eth_addr, 6);
146 task->internal_ip_table[ret].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
150 static inline void handle_arp_reply(struct task_base *tbase, struct rte_mbuf *mbuf)
152 struct task_master *task = (struct task_master *)tbase;
153 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
155 uint32_t key = hdr_arp->arp.data.spa;
156 plogx_dbg("\tMaster handling ARP reply for ip %x\n", key);
158 ret = rte_hash_lookup(task->external_ip_hash, (const void *)&key);
159 if (unlikely(ret < 0)) {
160 // entry not found for this IP: we did not ask a request, delete the reply
163 // entry found for this IP
164 uint16_t nb_requests = task->external_ip_table[ret].nb_requests;
165 memcpy(&hdr_arp->ether_hdr.d_addr.addr_bytes, &task->external_ip_table[ret].mac, 6);
166 // If we receive a request from multiple task for the same IP, then we update all tasks
167 if (task->external_ip_table[ret].nb_requests) {
168 rte_mbuf_refcnt_set(mbuf, nb_requests);
169 for (int i = 0; i < nb_requests; i++) {
170 struct rte_ring *ring = task->external_ip_table[ret].rings[i];
171 tx_ring_ip(tbase, ring, UPDATE_FROM_CTRL, mbuf, key);
173 task->external_ip_table[ret].nb_requests = 0;
178 static inline void handle_arp_request(struct task_base *tbase, struct rte_mbuf *mbuf)
180 struct task_master *task = (struct task_master *)tbase;
181 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
183 uint8_t port = get_port(mbuf);
186 key.ip = hdr_arp->arp.data.tpa;
188 if (task->internal_port_table[port].flags & HANDLE_RANDOM_IP_FLAG) {
189 struct ether_addr mac;
190 plogx_dbg("\tMaster handling ARP request for ip %x on port %d which supports random ip\n", key.ip, key.port);
191 struct rte_ring *ring = task->internal_port_table[port].ring;
192 create_mac(hdr_arp, &mac);
193 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
194 build_arp_reply(hdr_arp, &mac);
195 tx_ring(tbase, ring, ARP_REPLY_FROM_CTRL, mbuf);
199 plogx_dbg("\tMaster handling ARP request for ip %x\n", key.ip);
201 ret = rte_hash_lookup(task->internal_ip_hash, (const void *)&key);
202 if (unlikely(ret < 0)) {
203 // entry not found for this IP.
204 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);
207 struct rte_ring *ring = task->internal_ip_table[ret].ring;
208 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
209 build_arp_reply(hdr_arp, &task->internal_ip_table[ret].mac);
210 tx_ring(tbase, ring, ARP_REPLY_FROM_CTRL, mbuf);
214 static inline void handle_unknown_ip(struct task_base *tbase, struct rte_mbuf *mbuf)
216 struct task_master *task = (struct task_master *)tbase;
217 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
218 uint8_t port = get_port(mbuf);
219 uint32_t ip_dst = get_ip(mbuf);
222 plogx_dbg("\tMaster handling unknown ip %x for port %d\n", ip_dst, port);
223 if (unlikely(port >= PROX_MAX_PORTS)) {
224 plogx_dbg("Port %d not found", port);
228 uint32_t ip_src = task->internal_port_table[port].ip;
229 struct rte_ring *ring = task->ctrl_tx_rings[get_core(mbuf) * MAX_TASKS_PER_CORE + get_task(mbuf)];
232 plogx_dbg("Port %d not registered", port);
237 ret2 = rte_hash_add_key(task->external_ip_hash, (const void *)&ip_dst);
238 if (unlikely(ret2 < 0)) {
239 // entry not found for this IP: delete the reply
240 plogx_dbg("Unable to add IP %x in external_ip_hash\n", rte_be_to_cpu_32(hdr_arp->arp.data.tpa));
244 task->external_ip_table[ret2].rings[task->external_ip_table[ret2].nb_requests] = ring;
245 task->external_ip_table[ret2].nb_requests++;
246 memcpy(&task->external_ip_table[ret2].mac, &task->internal_port_table[port].mac, 6);
248 // We send an ARP request even if one was just sent (and not yet answered) by another task
249 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
250 build_arp_request(mbuf, &task->internal_port_table[port].mac, ip_dst, ip_src);
251 tx_ring(tbase, ring, ARP_REQ_FROM_CTRL, mbuf);
254 static inline void handle_message(struct task_base *tbase, struct rte_mbuf *mbuf, int ring_id)
256 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
257 int command = get_command(mbuf);
259 plogx_dbg("\tMaster received %s (%x) from mbuf %p\n", actions_string[command], command, mbuf);
263 if (hdr_arp->ether_hdr.ether_type != ETYPE_ARP) {
265 plog_err("\tUnexpected message received: ARP_TO_CTRL with ether_type %x\n", hdr_arp->ether_hdr.ether_type);
267 } else if (arp_is_gratuitous(hdr_arp)) {
268 plog_info("\tReceived gratuitous packet \n");
271 } else if (memcmp(&hdr_arp->arp, &arp_reply, 8) == 0) {
272 uint32_t ip = hdr_arp->arp.data.spa;
273 handle_arp_reply(tbase, mbuf);
274 } else if (memcmp(&hdr_arp->arp, &arp_request, 8) == 0) {
275 handle_arp_request(tbase, mbuf);
277 plog_info("\tReceived unexpected ARP operation %d\n", hdr_arp->arp.oper);
282 case REQ_MAC_TO_CTRL:
283 handle_unknown_ip(tbase, mbuf);
286 plogx_dbg("\tMaster received unexpected message\n");
292 void init_ctrl_plane(struct task_base *tbase)
294 prox_cfg.flags |= DSF_CTRL_PLANE_ENABLED;
295 struct task_master *task = (struct task_master *)tbase;
296 int socket = rte_lcore_to_socket_id(prox_cfg.master);
297 uint32_t n_entries = MAX_ARP_ENTRIES * 4;
298 static char hash_name[30];
299 sprintf(hash_name, "A%03d_hash_arp_table", prox_cfg.master);
300 struct rte_hash_parameters hash_params = {
302 .entries = n_entries,
303 .key_len = sizeof(uint32_t),
304 .hash_func = rte_hash_crc,
305 .hash_func_init_val = 0,
307 task->external_ip_hash = rte_hash_create(&hash_params);
308 PROX_PANIC(task->external_ip_hash == NULL, "Failed to set up external ip hash\n");
309 plog_info("\texternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
310 task->external_ip_table = (struct external_ip_table *)prox_zmalloc(n_entries * sizeof(struct external_ip_table), socket);
311 PROX_PANIC(task->external_ip_table == NULL, "Failed to allocate memory for %u entries in external ip table\n", n_entries);
312 plog_info("\texternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct external_ip_table));
315 hash_params.key_len = sizeof(struct ip_port);
316 task->internal_ip_hash = rte_hash_create(&hash_params);
317 PROX_PANIC(task->internal_ip_hash == NULL, "Failed to set up internal ip hash\n");
318 plog_info("\tinternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
319 task->internal_ip_table = (struct ip_table *)prox_zmalloc(n_entries * sizeof(struct ip_table), socket);
320 PROX_PANIC(task->internal_ip_table == NULL, "Failed to allocate memory for %u entries in internal ip table\n", n_entries);
321 plog_info("\tinternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct ip_table));
324 static int handle_ctrl_plane_f(struct task_base *tbase, __attribute__((unused)) struct rte_mbuf **mbuf, uint16_t n_pkts)
326 int ring_id = 0, j, ret = 0;
327 struct rte_mbuf *mbufs[MAX_RING_BURST];
328 struct task_master *task = (struct task_master *)tbase;
330 /* Handle_master works differently than other handle functions
331 It is not handled by a DPDK dataplane core
332 It is no thread_generic based, hence do not receive packets the same way
335 ret = ring_deq(task->ctrl_rx_ring, mbufs);
336 for (j = 0; j < ret; j++) {
337 handle_message(tbase, mbufs[j], ring_id);
342 static void init_task_master(struct task_base *tbase, struct task_args *targs)
344 if (prox_cfg.flags & DSF_CTRL_PLANE_ENABLED) {
345 struct task_master *task = (struct task_master *)tbase;
347 task->ctrl_rx_ring = targs->lconf->ctrl_rings_p[0];
348 task->ctrl_tx_rings = ctrl_rings;
349 init_ctrl_plane(tbase);
350 handle_ctrl_plane = handle_ctrl_plane_f;
354 static struct task_init task_init_master = {
355 .mode_str = "master",
356 .init = init_task_master,
358 .flag_features = TASK_FEATURE_NEVER_DISCARDS,
359 .size = sizeof(struct task_master)
362 __attribute__((constructor)) static void reg_task_gen(void)
364 reg_task(&task_init_master);