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
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;
64 struct ether_addr mac;
65 struct rte_ring *ring;
72 struct task_base base;
73 struct rte_ring *ctrl_rx_ring;
74 struct rte_ring **ctrl_tx_rings;
75 struct ip_table *internal_ip_table;
76 struct ip_table *external_ip_table;
77 struct rte_hash *external_ip_hash;
78 struct rte_hash *internal_ip_hash;
79 struct port_table internal_port_table[PROX_MAX_PORTS];
85 } __attribute__((packed));
87 static inline uint8_t get_command(struct rte_mbuf *mbuf)
89 return mbuf->udata64 & 0xFF;
91 static inline uint8_t get_task(struct rte_mbuf *mbuf)
93 return (mbuf->udata64 >> 8) & 0xFF;
95 static inline uint8_t get_core(struct rte_mbuf *mbuf)
97 return (mbuf->udata64 >> 16) & 0xFF;
99 static inline uint8_t get_port(struct rte_mbuf *mbuf)
103 static inline uint32_t get_ip(struct rte_mbuf *mbuf)
105 return (mbuf->udata64 >> 32) & 0xFFFFFFFF;
108 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)
110 struct task_master *task = (struct task_master *)tbase;
112 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);
114 if (port_id >= PROX_MAX_PORTS) {
115 plog_err("Unable to register ip %x, port %d\n", ip, port_id);
119 /* TODO - stoe multiple rings if multiple cores able to handle IP
120 Remove them when such cores are stopped and de-register IP
122 task->internal_port_table[port_id].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
123 memcpy(&task->internal_port_table[port_id].mac, &prox_port_cfg[port_id].eth_addr, 6);
124 task->internal_port_table[port_id].ip = ip;
126 if (ip == RANDOM_IP) {
127 task->internal_port_table[port_id].flags |= HANDLE_RANDOM_IP_FLAG;
133 int ret = rte_hash_add_key(task->internal_ip_hash, (const void *)&key);
134 if (unlikely(ret < 0)) {
135 plog_err("Unable to register ip %x\n", ip);
138 memcpy(&task->internal_ip_table[ret].mac, &prox_port_cfg[port_id].eth_addr, 6);
139 task->internal_ip_table[ret].ring = task->ctrl_tx_rings[core_id * MAX_TASKS_PER_CORE + task_id];
143 static inline void handle_arp_reply(struct task_base *tbase, struct rte_mbuf *mbuf)
145 struct task_master *task = (struct task_master *)tbase;
146 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
148 uint32_t key = hdr_arp->arp.data.spa;
149 plogx_dbg("\tMaster handling ARP reply for ip %x\n", key);
151 ret = rte_hash_lookup(task->external_ip_hash, (const void *)&key);
152 if (unlikely(ret < 0)) {
153 // entry not found for this IP: we did not ask a request, delete the reply
156 // entry found for this IP
157 struct rte_ring *ring = task->external_ip_table[ret].ring;
158 memcpy(&hdr_arp->ether_hdr.d_addr.addr_bytes, &task->external_ip_table[ret].mac, 6);
159 tx_ring_ip(tbase, ring, UPDATE_FROM_CTRL, mbuf, key);
163 static inline void handle_arp_request(struct task_base *tbase, struct rte_mbuf *mbuf)
165 struct task_master *task = (struct task_master *)tbase;
166 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
168 uint8_t port = get_port(mbuf);
171 key.ip = hdr_arp->arp.data.tpa;
173 if (task->internal_port_table[port].flags & HANDLE_RANDOM_IP_FLAG) {
174 struct ether_addr mac;
175 plogx_dbg("\tMaster handling ARP request for ip %x on port %d which supports random ip\n", key.ip, key.port);
176 struct rte_ring *ring = task->internal_port_table[port].ring;
177 create_mac(hdr_arp, &mac);
178 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
179 build_arp_reply(hdr_arp, &mac);
180 tx_ring(tbase, ring, ARP_REPLY_FROM_CTRL, mbuf);
184 plogx_dbg("\tMaster handling ARP request for ip %x\n", key.ip);
186 ret = rte_hash_lookup(task->internal_ip_hash, (const void *)&key);
187 if (unlikely(ret < 0)) {
188 // entry not found for this IP.
189 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);
192 struct rte_ring *ring = task->internal_ip_table[ret].ring;
193 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
194 build_arp_reply(hdr_arp, &task->internal_ip_table[ret].mac);
195 tx_ring(tbase, ring, ARP_REPLY_FROM_CTRL, mbuf);
199 static inline void handle_unknown_ip(struct task_base *tbase, struct rte_mbuf *mbuf)
201 struct task_master *task = (struct task_master *)tbase;
202 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
203 uint8_t port = get_port(mbuf);
204 uint32_t ip_dst = get_ip(mbuf);
207 plogx_dbg("\tMaster handling unknown ip %x for port %d\n", ip_dst, port);
208 if (unlikely(port >= PROX_MAX_PORTS)) {
209 plogx_dbg("Port %d not found", port);
213 uint32_t ip_src = task->internal_port_table[port].ip;
214 struct rte_ring *ring = task->ctrl_tx_rings[get_core(mbuf) * MAX_TASKS_PER_CORE + get_task(mbuf)];
217 plogx_dbg("Port %d not registered", port);
222 ret2 = rte_hash_add_key(task->external_ip_hash, (const void *)&ip_dst);
223 if (unlikely(ret2 < 0)) {
224 // entry not found for this IP: delete the reply
225 plogx_dbg("Unable to add IP %x in external_ip_hash\n", rte_be_to_cpu_32(hdr_arp->arp.data.tpa));
229 task->external_ip_table[ret2].ring = ring;
230 memcpy(&task->external_ip_table[ret2].mac, &task->internal_port_table[port].mac, 6);
232 mbuf->ol_flags &= ~(PKT_TX_IP_CKSUM|PKT_TX_UDP_CKSUM);
233 build_arp_request(mbuf, &task->internal_port_table[port].mac, ip_dst, ip_src);
234 tx_ring(tbase, ring, ARP_REQ_FROM_CTRL, mbuf);
237 static inline void handle_message(struct task_base *tbase, struct rte_mbuf *mbuf, int ring_id)
239 struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
240 int command = get_command(mbuf);
242 plogx_dbg("\tMaster received %s (%x) from mbuf %p\n", actions_string[command], command, mbuf);
246 if (hdr_arp->ether_hdr.ether_type != ETYPE_ARP) {
248 plog_err("\tUnexpected message received: ARP_TO_CTRL with ether_type %x\n", hdr_arp->ether_hdr.ether_type);
250 } else if (arp_is_gratuitous(hdr_arp)) {
251 plog_info("\tReceived gratuitous packet \n");
254 } else if (memcmp(&hdr_arp->arp, &arp_reply, 8) == 0) {
255 uint32_t ip = hdr_arp->arp.data.spa;
256 handle_arp_reply(tbase, mbuf);
257 } else if (memcmp(&hdr_arp->arp, &arp_request, 8) == 0) {
258 handle_arp_request(tbase, mbuf);
260 plog_info("\tReceived unexpected ARP operation %d\n", hdr_arp->arp.oper);
265 case REQ_MAC_TO_CTRL:
266 handle_unknown_ip(tbase, mbuf);
269 plogx_dbg("\tMaster received unexpected message\n");
275 void init_ctrl_plane(struct task_base *tbase)
277 prox_cfg.flags |= DSF_CTRL_PLANE_ENABLED;
278 struct task_master *task = (struct task_master *)tbase;
279 int socket = rte_lcore_to_socket_id(prox_cfg.master);
280 uint32_t n_entries = MAX_ARP_ENTRIES * 4;
281 static char hash_name[30];
282 sprintf(hash_name, "A%03d_hash_arp_table", prox_cfg.master);
283 struct rte_hash_parameters hash_params = {
285 .entries = n_entries,
286 .key_len = sizeof(uint32_t),
287 .hash_func = rte_hash_crc,
288 .hash_func_init_val = 0,
290 task->external_ip_hash = rte_hash_create(&hash_params);
291 PROX_PANIC(task->external_ip_hash == NULL, "Failed to set up external ip hash\n");
292 plog_info("\texternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
293 task->external_ip_table = (struct ip_table *)prox_zmalloc(n_entries * sizeof(struct ip_table), socket);
294 PROX_PANIC(task->external_ip_table == NULL, "Failed to allocate memory for %u entries in external ip table\n", n_entries);
295 plog_info("\texternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct ip_table));
298 hash_params.key_len = sizeof(struct ip_port);
299 task->internal_ip_hash = rte_hash_create(&hash_params);
300 PROX_PANIC(task->internal_ip_hash == NULL, "Failed to set up internal ip hash\n");
301 plog_info("\tinternal ip hash table allocated, with %d entries of size %d\n", hash_params.entries, hash_params.key_len);
302 task->internal_ip_table = (struct ip_table *)prox_zmalloc(n_entries * sizeof(struct ip_table), socket);
303 PROX_PANIC(task->internal_ip_table == NULL, "Failed to allocate memory for %u entries in internal ip table\n", n_entries);
304 plog_info("\tinternal ip table, with %d entries of size %ld\n", n_entries, sizeof(struct ip_table));
307 static int handle_ctrl_plane_f(struct task_base *tbase, __attribute__((unused)) struct rte_mbuf **mbuf, uint16_t n_pkts)
309 int ring_id, j, ret = 0;
310 struct rte_mbuf *mbufs[MAX_RING_BURST];
311 struct task_master *task = (struct task_master *)tbase;
313 /* Handle_master works differently than other handle functions
314 It is not handled by a DPDK dataplane core
315 It is no thread_generic based, hence do not receive packets the same way
318 ret = ring_deq(task->ctrl_rx_ring, mbufs);
319 for (j = 0; j < ret; j++) {
320 handle_message(tbase, mbufs[j], ring_id);
325 static void init_task_master(struct task_base *tbase, struct task_args *targs)
327 if (prox_cfg.flags & DSF_CTRL_PLANE_ENABLED) {
328 struct task_master *task = (struct task_master *)tbase;
330 task->ctrl_rx_ring = targs->lconf->ctrl_rings_p[0];
331 task->ctrl_tx_rings = ctrl_rings;
332 init_ctrl_plane(tbase);
333 handle_ctrl_plane = handle_ctrl_plane_f;
337 static struct task_init task_init_master = {
338 .mode_str = "master",
339 .init = init_task_master,
341 .flag_features = TASK_FEATURE_NEVER_DISCARDS,
342 .size = sizeof(struct task_master)
345 __attribute__((constructor)) static void reg_task_gen(void)
347 reg_task(&task_init_master);