Fix command parser
[samplevnf.git] / VNFs / DPPD-PROX / handle_master.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_hash.h>
18 #include <rte_hash_crc.h>
19 #include "prox_cfg.h"
20
21 #include "prox_globals.h"
22 #include "rx_pkt.h"
23 #include "arp.h"
24 #include "handle_master.h"
25 #include "log.h"
26 #include "mbuf_utils.h"
27 #include "etypes.h"
28 #include "defaults.h"
29 #include "prox_cfg.h"
30 #include "prox_malloc.h"
31 #include "quit.h"
32 #include "task_init.h"
33 #include "prox_port_cfg.h"
34 #include "main.h"
35 #include "lconf.h"
36 #include "input.h"
37 #include "tx_pkt.h"
38
39 #define IP4(x) x & 0xff, (x >> 8) & 0xff, (x >> 16) & 0xff, x >> 24
40
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"};
42
43 static struct my_arp_t arp_reply = {
44         .htype = 0x100,
45         .ptype = 8,
46         .hlen = 6,
47         .plen = 4,
48         .oper = 0x200
49 };
50 static struct my_arp_t arp_request = {
51         .htype = 0x100,
52         .ptype = 8,
53         .hlen = 6,
54         .plen = 4,
55         .oper = 0x100
56 };
57
58 struct ip_table {
59         struct ether_addr       mac;
60         struct rte_ring         *ring;
61 };
62
63 struct port_table {
64         struct ether_addr       mac;
65         struct rte_ring         *ring;
66         uint32_t                ip;
67         uint8_t                 port;
68         uint8_t                 flags;
69 };
70
71 struct task_master {
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];
80 };
81
82 struct ip_port {
83         uint32_t ip;
84         uint8_t port;
85 } __attribute__((packed));
86
87 static inline uint8_t get_command(struct rte_mbuf *mbuf)
88 {
89         return mbuf->udata64 & 0xFF;
90 }
91 static inline uint8_t get_task(struct rte_mbuf *mbuf)
92 {
93         return (mbuf->udata64 >> 8) & 0xFF;
94 }
95 static inline uint8_t get_core(struct rte_mbuf *mbuf)
96 {
97         return (mbuf->udata64 >> 16) & 0xFF;
98 }
99 static inline uint8_t get_port(struct rte_mbuf *mbuf)
100 {
101         return mbuf->port;
102 }
103 static inline uint32_t get_ip(struct rte_mbuf *mbuf)
104 {
105         return (mbuf->udata64 >> 32) & 0xFFFFFFFF;
106 }
107
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)
109 {
110         struct task_master *task = (struct task_master *)tbase;
111         struct ip_port key;
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);
113
114         if (port_id >= PROX_MAX_PORTS) {
115                 plog_err("Unable to register ip %x, port %d\n", ip, port_id);
116                 return;
117         }
118
119         /* TODO - stoe multiple rings if multiple cores able to handle IP
120            Remove them when such cores are stopped and de-register IP
121         */
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;
125
126         if (ip == RANDOM_IP) {
127                 task->internal_port_table[port_id].flags |= HANDLE_RANDOM_IP_FLAG;
128                 return;
129         }
130
131         key.ip = ip;
132         key.port = port_id;
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);
136                 return;
137         }
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];
140
141 }
142
143 static inline void handle_arp_reply(struct task_base *tbase, struct rte_mbuf *mbuf)
144 {
145         struct task_master *task = (struct task_master *)tbase;
146         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
147         int i, ret;
148         uint32_t key = hdr_arp->arp.data.spa;
149         plogx_dbg("\tMaster handling ARP reply for ip %x\n", key);
150
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
154                 tx_drop(mbuf);
155         } else {
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);
160         }
161 }
162
163 static inline void handle_arp_request(struct task_base *tbase, struct rte_mbuf *mbuf)
164 {
165         struct task_master *task = (struct task_master *)tbase;
166         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
167         int i, ret;
168         uint8_t port = get_port(mbuf);
169
170         struct ip_port key;
171         key.ip = hdr_arp->arp.data.tpa;
172         key.port = port;
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);
181                 return;
182         }
183
184         plogx_dbg("\tMaster handling ARP request for ip %x\n", key.ip);
185
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);
190                 tx_drop(mbuf);
191         } else {
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);
196         }
197 }
198
199 static inline void handle_unknown_ip(struct task_base *tbase, struct rte_mbuf *mbuf)
200 {
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);
205         int ret1, ret2;
206
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);
210                 tx_drop(mbuf);
211                 return;
212         }
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)];
215
216         if (ring == NULL) {
217                 plogx_dbg("Port %d not registered", port);
218                 tx_drop(mbuf);
219                 return;
220         }
221
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));
226                 tx_drop(mbuf);
227                 return;
228         }
229         task->external_ip_table[ret2].ring = ring;
230         memcpy(&task->external_ip_table[ret2].mac, &task->internal_port_table[port].mac, 6);
231
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);
235 }
236
237 static inline void handle_message(struct task_base *tbase, struct rte_mbuf *mbuf, int ring_id)
238 {
239         struct ether_hdr_arp *hdr_arp = rte_pktmbuf_mtod(mbuf, struct ether_hdr_arp *);
240         int command = get_command(mbuf);
241         uint32_t ip;
242         plogx_dbg("\tMaster received %s (%x) from mbuf %p\n", actions_string[command], command, mbuf);
243
244         switch(command) {
245         case ARP_TO_CTRL:
246                 if (hdr_arp->ether_hdr.ether_type != ETYPE_ARP) {
247                         tx_drop(mbuf);
248                         plog_err("\tUnexpected message received: ARP_TO_CTRL with ether_type %x\n", hdr_arp->ether_hdr.ether_type);
249                         return;
250                 } else if (arp_is_gratuitous(hdr_arp)) {
251                         plog_info("\tReceived gratuitous packet \n");
252                         tx_drop(mbuf);
253                         return;
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);
259                 } else {
260                         plog_info("\tReceived unexpected ARP operation %d\n", hdr_arp->arp.oper);
261                         tx_drop(mbuf);
262                         return;
263                 }
264                 break;
265         case REQ_MAC_TO_CTRL:
266                 handle_unknown_ip(tbase, mbuf);
267                 break;
268         default:
269                 plogx_dbg("\tMaster received unexpected message\n");
270                 tx_drop(mbuf);
271                 break;
272         }
273 }
274
275 void init_ctrl_plane(struct task_base *tbase)
276 {
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 = {
284                 .name = hash_name,
285                 .entries = n_entries,
286                 .key_len = sizeof(uint32_t),
287                 .hash_func = rte_hash_crc,
288                 .hash_func_init_val = 0,
289         };
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));
296
297         hash_name[0]++;
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));
305 }
306
307 static int handle_ctrl_plane_f(struct task_base *tbase, __attribute__((unused)) struct rte_mbuf **mbuf, uint16_t n_pkts)
308 {
309         int ring_id, j, ret = 0;
310         struct rte_mbuf *mbufs[MAX_RING_BURST];
311         struct task_master *task = (struct task_master *)tbase;
312
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
316         */
317
318         ret = ring_deq(task->ctrl_rx_ring, mbufs);
319         for (j = 0; j < ret; j++) {
320                 handle_message(tbase, mbufs[j], ring_id);
321         }
322         return ret;
323 }
324
325 static void init_task_master(struct task_base *tbase, struct task_args *targs)
326 {
327         if (prox_cfg.flags & DSF_CTRL_PLANE_ENABLED) {
328                 struct task_master *task = (struct task_master *)tbase;
329
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;
334         }
335 }
336
337 static struct task_init task_init_master = {
338         .mode_str = "master",
339         .init = init_task_master,
340         .handle = NULL,
341         .flag_features = TASK_FEATURE_NEVER_DISCARDS,
342         .size = sizeof(struct task_master)
343 };
344
345 __attribute__((constructor)) static void reg_task_gen(void)
346 {
347         reg_task(&task_init_master);
348 }