4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <netinet/in.h>
42 #include <rte_ether.h>
43 #include <rte_byteorder.h>
46 #include <rte_malloc.h>
47 #include <rte_string_fns.h>
48 #include <cmdline_rdline.h>
49 #include <cmdline_parse.h>
50 #include <cmdline_parse_num.h>
51 #include <cmdline_parse_string.h>
52 #include <cmdline_parse_ipaddr.h>
53 #include <cmdline_parse_etheraddr.h>
54 #include <cmdline_socket.h>
59 #define IS_RULE_PRESENT(res, rule_key, table, type) \
61 struct app_rule *it; \
64 TAILQ_FOREACH(it, &table, entries) { \
65 if (memcmp(&rule_key, &it->type.key, sizeof(rule_key)) == 0) {\
74 app_init_rule_tables(void);
76 TAILQ_HEAD(linked_list, app_rule) arp_table, routing_table, firewall_table,
80 uint32_t n_routing_rules;
81 uint32_t n_firewall_rules;
82 uint32_t n_flow_rules;
90 struct ether_addr nh_arp;
93 struct app_routing_rule {
103 struct app_firewall_rule {
106 uint32_t src_ip_mask;
108 uint32_t dst_ip_mask;
109 uint16_t src_port_from;
110 uint16_t src_port_to;
111 uint16_t dst_port_from;
112 uint16_t dst_port_to;
121 struct app_flow_rule {
135 struct app_arp_rule arp;
136 struct app_routing_rule routing;
137 struct app_firewall_rule firewall;
138 struct app_flow_rule flow;
141 TAILQ_ENTRY(app_rule) entries;
146 app_init_rule_tables(void)
148 TAILQ_INIT(&arp_table);
149 TAILQ_INIT(&routing_table);
150 TAILQ_INIT(&firewall_table);
151 TAILQ_INIT(&flow_table);
155 n_firewall_rules = 0;
161 print_arp_rule(struct app_arp_rule rule)
163 printf("(Iface = %u, Address = %u.%u.%u.%u) => "
164 "HWaddress = %02x:%02x:%02x:%02x:%02x:%02x\n",
166 (rule.key.nh_ip >> 24) & 0xFF,
167 (rule.key.nh_ip >> 16) & 0xFF,
168 (rule.key.nh_ip >> 8) & 0xFF,
169 rule.key.nh_ip & 0xFF,
171 rule.nh_arp.addr_bytes[0],
172 rule.nh_arp.addr_bytes[1],
173 rule.nh_arp.addr_bytes[2],
174 rule.nh_arp.addr_bytes[3],
175 rule.nh_arp.addr_bytes[4],
176 rule.nh_arp.addr_bytes[5]);
180 print_routing_rule(struct app_routing_rule rule)
182 printf("IP Prefix = %u.%u.%u.%u/%u => "
183 "(Iface = %u, Gateway = %u.%u.%u.%u)\n",
184 (rule.key.ip >> 24) & 0xFF,
185 (rule.key.ip >> 16) & 0xFF,
186 (rule.key.ip >> 8) & 0xFF,
192 (rule.nh_ip >> 24) & 0xFF,
193 (rule.nh_ip >> 16) & 0xFF,
194 (rule.nh_ip >> 8) & 0xFF,
198 #ifdef RTE_LIBRTE_ACL
201 print_firewall_rule(struct app_firewall_rule rule)
203 printf("Priority %d: (IP Src = %u.%u.%u.%u/%u, "
204 "IP Dst = %u.%u.%u.%u/%u, "
205 "Port Src = %u-%u, Port Dst = %u-%u, Proto = %u (%u)) => "
209 (rule.key.src_ip >> 24) & 0xFF,
210 (rule.key.src_ip >> 16) & 0xFF,
211 (rule.key.src_ip >> 8) & 0xFF,
212 rule.key.src_ip & 0xFF,
213 rule.key.src_ip_mask,
215 (rule.key.dst_ip >> 24) & 0xFF,
216 (rule.key.dst_ip >> 16) & 0xFF,
217 (rule.key.dst_ip >> 8) & 0xFF,
218 rule.key.dst_ip & 0xFF,
219 rule.key.dst_ip_mask,
221 rule.key.src_port_from,
222 rule.key.src_port_to,
223 rule.key.dst_port_from,
224 rule.key.dst_port_to,
233 print_flow_rule(struct app_flow_rule rule)
235 printf("(IP Src = %u.%u.%u.%u, IP Dst = %u.%u.%u.%u, Port Src = %u, "
236 "Port Dst = %u, Proto = %u) => Port = %u\n",
237 (rule.key.src_ip >> 24) & 0xFF,
238 (rule.key.src_ip >> 16) & 0xFF,
239 (rule.key.src_ip >> 8) & 0xFF,
240 rule.key.src_ip & 0xFF,
242 (rule.key.dst_ip >> 24) & 0xFF,
243 (rule.key.dst_ip >> 16) & 0xFF,
244 (rule.key.dst_ip >> 8) & 0xFF,
245 rule.key.dst_ip & 0xFF,
249 (uint32_t) rule.key.proto,
255 /* *** Run file (script) *** */
256 struct cmd_run_file_result {
257 cmdline_fixed_string_t run_string;
265 __attribute__((unused)) void *data)
267 struct cmd_run_file_result *params = parsed_result;
268 struct cmdline *file_cl;
272 if (!params->file_path) {
273 printf("Illegal value for file path (%s)\n", params->file_path);
277 fd = open(params->file_path, O_RDONLY, 0);
279 printf("Illegal value for file path (%s)\n", params->file_path);
283 file_cl = cmdline_new(cl->ctx, "", fd, 1);
284 cmdline_interact(file_cl);
288 cmdline_parse_token_string_t cmd_run_file_run_string =
289 TOKEN_STRING_INITIALIZER(struct cmd_run_file_result, run_string, "run");
291 cmdline_parse_token_string_t cmd_run_file_file_path =
292 TOKEN_STRING_INITIALIZER(struct cmd_run_file_result, file_path, NULL);
294 cmdline_parse_inst_t cmd_run_file = {
295 .f = cmd_run_file_parsed,
297 .help_str = "Run commands from file",
299 (void *)&cmd_run_file_run_string,
300 (void *)&cmd_run_file_file_path,
305 /* *** Link - Enable *** */
306 struct cmd_link_enable_result {
307 cmdline_fixed_string_t link_string;
309 cmdline_fixed_string_t up_string;
313 cmd_link_enable_parsed(
315 __attribute__((unused)) struct cmdline *cl,
316 __attribute__((unused)) void *data)
318 struct cmd_link_enable_result *params = parsed_result;
320 struct app_msg_req *req;
321 struct app_msg_resp *resp;
324 uint32_t core_id = app_get_first_core_id(APP_CORE_RX);
326 if (core_id == RTE_MAX_LCORE) {
327 printf("RX core not preformed by any CPU core\n");
331 struct rte_ring *ring_req = app_get_ring_req(core_id);
332 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
335 if (params->port >= app.n_ports) {
336 printf("Illegal value for port parameter (%u)\n", params->port);
340 printf("Enabling port %d\n", params->port);
342 /* Allocate message buffer */
343 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
345 rte_panic("Unable to allocate new message\n");
347 /* Fill request message */
348 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
349 req->type = APP_MSG_REQ_RX_PORT_ENABLE;
350 req->rx_up.port = params->port;
354 status = rte_ring_sp_enqueue(ring_req, msg);
355 } while (status == -ENOBUFS);
357 /* Wait for response */
359 status = rte_ring_sc_dequeue(ring_resp, &msg);
360 } while (status != 0);
361 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
363 if (resp->result != 0)
364 printf("Request LINK_UP failed (%u)\n", resp->result);
366 /* Free message buffer */
367 rte_ctrlmbuf_free(msg);
370 cmdline_parse_token_string_t cmd_link_enable_link_string =
371 TOKEN_STRING_INITIALIZER(struct cmd_link_enable_result, link_string,
374 cmdline_parse_token_num_t cmd_link_enable_port =
375 TOKEN_NUM_INITIALIZER(struct cmd_link_enable_result, port, UINT8);
377 cmdline_parse_token_string_t cmd_link_enable_up_string =
378 TOKEN_STRING_INITIALIZER(struct cmd_link_enable_result, up_string,
381 cmdline_parse_inst_t cmd_link_enable = {
382 .f = cmd_link_enable_parsed,
384 .help_str = "Link down",
386 (void *)&cmd_link_enable_link_string,
387 (void *)&cmd_link_enable_port,
388 (void *)&cmd_link_enable_up_string,
393 /* *** Link - Disable *** */
394 struct cmd_link_disable_result {
395 cmdline_fixed_string_t link_string;
397 cmdline_fixed_string_t down_string;
401 cmd_link_disable_parsed(
403 __attribute__((unused)) struct cmdline *cl,
404 __attribute__((unused)) void *data)
406 struct cmd_link_disable_result *params = parsed_result;
407 struct app_msg_req *req;
408 struct app_msg_resp *resp;
412 uint32_t core_id = app_get_first_core_id(APP_CORE_RX);
414 if (core_id == RTE_MAX_LCORE) {
415 printf("RX not performed by any CPU core\n");
419 struct rte_ring *ring_req = app_get_ring_req(core_id);
420 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
423 if (params->port >= app.n_ports) {
424 printf("Illegal value for port parameter (%u)\n", params->port);
428 printf("Disabling port %d\n", params->port);
430 /* Allocate message buffer */
431 msg = rte_ctrlmbuf_alloc(app.msg_pool);
433 rte_panic("Unable to allocate new message\n");
435 /* Fill request message */
436 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
437 req->type = APP_MSG_REQ_RX_PORT_DISABLE;
438 req->rx_down.port = params->port;
442 status = rte_ring_sp_enqueue(ring_req, msg);
443 } while (status == -ENOBUFS);
445 /* Wait for response */
447 status = rte_ring_sc_dequeue(ring_resp, &msg);
448 } while (status != 0);
449 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
452 if (resp->result != 0)
453 printf("Request LINK_DOWN failed (%u)\n", resp->result);
455 /* Free message buffer */
456 rte_ctrlmbuf_free((struct rte_mbuf *)msg);
459 cmdline_parse_token_string_t cmd_link_disable_link_string =
460 TOKEN_STRING_INITIALIZER(struct cmd_link_disable_result, link_string,
463 cmdline_parse_token_num_t cmd_link_disable_port =
464 TOKEN_NUM_INITIALIZER(struct cmd_link_disable_result, port, UINT8);
466 cmdline_parse_token_string_t cmd_link_disable_down_string =
467 TOKEN_STRING_INITIALIZER(struct cmd_link_disable_result, down_string,
470 cmdline_parse_inst_t cmd_link_disable = {
471 .f = cmd_link_disable_parsed,
473 .help_str = "Link up",
475 (void *)&cmd_link_disable_link_string,
476 (void *)&cmd_link_disable_port,
477 (void *)&cmd_link_disable_down_string,
483 /* *** ARP - Add *** */
484 struct cmd_arp_add_result {
485 cmdline_fixed_string_t arp_string;
486 cmdline_fixed_string_t add_string;
488 cmdline_ipaddr_t nh_ip;
489 struct ether_addr nh_arp;
496 __attribute__((unused)) struct cmdline *cl,
497 __attribute__((unused)) void *data)
499 struct cmd_arp_add_result *params = parsed_result;
500 struct app_rule rule, *old_rule;
501 struct app_msg_req *req;
502 struct app_msg_resp *resp;
506 uint32_t core_id = app_get_first_core_id(APP_CORE_RT);
508 if (core_id == RTE_MAX_LCORE) {
509 printf("ARP not performed by any CPU core\n");
513 struct rte_ring *ring_req = app_get_ring_req(core_id);
514 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
517 if (params->out_iface >= app.n_ports) {
518 printf("Illegal value for output interface parameter (%u)\n",
524 memset(&rule, 0, sizeof(rule));
525 rule.arp.key.out_iface = params->out_iface;
527 rte_bswap32((uint32_t) params->nh_ip.addr.ipv4.s_addr);
528 rule.arp.nh_arp = params->nh_arp;
530 /* Check rule existence */
531 IS_RULE_PRESENT(old_rule, rule.arp.key, arp_table, arp);
532 if ((old_rule == NULL) && (n_arp_rules == app.max_arp_rules)) {
533 printf("ARP table is full.\n");
537 printf("Adding ARP entry: ");
538 print_arp_rule(rule.arp);
540 /* Allocate message buffer */
541 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
543 rte_panic("Unable to allocate new message\n");
545 /* Fill request message */
546 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
547 req->type = APP_MSG_REQ_ARP_ADD;
548 req->arp_add.out_iface = rule.arp.key.out_iface;
549 req->arp_add.nh_ip = rule.arp.key.nh_ip;
550 req->arp_add.nh_arp = rule.arp.nh_arp;
554 status = rte_ring_sp_enqueue(ring_req, msg);
555 } while (status == -ENOBUFS);
557 /* Wait for response */
559 status = rte_ring_sc_dequeue(ring_resp, &msg);
560 } while (status != 0);
561 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
564 if (resp->result != 0)
565 printf("Request ARP_ADD failed (%u)\n", resp->result);
567 if (old_rule == NULL) {
568 struct app_rule *new_rule = (struct app_rule *)
569 rte_zmalloc_socket("CLI",
570 sizeof(struct app_rule),
574 if (new_rule == NULL)
575 rte_panic("Unable to allocate new rule\n");
577 memcpy(new_rule, &rule, sizeof(rule));
578 TAILQ_INSERT_TAIL(&arp_table, new_rule, entries);
581 old_rule->arp.nh_arp = rule.arp.nh_arp;
584 /* Free message buffer */
585 rte_ctrlmbuf_free((struct rte_mbuf *) msg);
588 cmdline_parse_token_string_t cmd_arp_add_arp_string =
589 TOKEN_STRING_INITIALIZER(struct cmd_arp_add_result, arp_string, "arp");
591 cmdline_parse_token_string_t cmd_arp_add_add_string =
592 TOKEN_STRING_INITIALIZER(struct cmd_arp_add_result, add_string, "add");
594 cmdline_parse_token_num_t cmd_arp_add_out_iface =
595 TOKEN_NUM_INITIALIZER(struct cmd_arp_add_result, out_iface, UINT8);
597 cmdline_parse_token_ipaddr_t cmd_arp_add_nh_ip =
598 TOKEN_IPADDR_INITIALIZER(struct cmd_arp_add_result, nh_ip);
600 cmdline_parse_token_etheraddr_t cmd_arp_add_nh_arp =
601 TOKEN_ETHERADDR_INITIALIZER(struct cmd_arp_add_result, nh_arp);
603 cmdline_parse_inst_t cmd_arp_add = {
604 .f = cmd_arp_add_parsed,
606 .help_str = "ARP add",
608 (void *)&cmd_arp_add_arp_string,
609 (void *)&cmd_arp_add_add_string,
610 (void *)&cmd_arp_add_out_iface,
611 (void *)&cmd_arp_add_nh_ip,
612 (void *)&cmd_arp_add_nh_arp,
617 /* *** ARP - Del *** */
618 struct cmd_arp_del_result {
619 cmdline_fixed_string_t arp_string;
620 cmdline_fixed_string_t del_string;
622 cmdline_ipaddr_t nh_ip;
628 __attribute__((unused)) struct cmdline *cl,
629 __attribute__((unused)) void *data)
631 struct cmd_arp_del_result *params = parsed_result;
632 struct app_rule rule, *old_rule;
633 struct app_msg_req *req;
634 struct app_msg_resp *resp;
638 uint32_t core_id = app_get_first_core_id(APP_CORE_RT);
640 if (core_id == RTE_MAX_LCORE) {
641 printf("ARP not performed by any CPU core\n");
645 struct rte_ring *ring_req = app_get_ring_req(core_id);
646 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
649 if (params->out_iface > app.n_ports) {
650 printf("Illegal value for output interface parameter (%u)\n",
656 memset(&rule, 0, sizeof(rule));
657 rule.arp.key.out_iface = params->out_iface;
659 rte_bswap32((uint32_t) params->nh_ip.addr.ipv4.s_addr);
661 /* Check rule existence */
662 IS_RULE_PRESENT(old_rule, rule.arp.key, arp_table, arp);
663 if (old_rule == NULL)
666 printf("Deleting ARP entry: ");
667 print_arp_rule(old_rule->arp);
669 /* Allocate message buffer */
670 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
672 rte_panic("Unable to allocate new message\n");
674 /* Fill request message */
675 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
676 req->type = APP_MSG_REQ_ARP_DEL;
677 req->arp_del.out_iface = rule.arp.key.out_iface;
678 req->arp_del.nh_ip = rule.arp.key.nh_ip;
682 status = rte_ring_sp_enqueue(ring_req, msg);
683 } while (status == -ENOBUFS);
685 /* Wait for response */
687 status = rte_ring_sc_dequeue(ring_resp, &msg);
688 } while (status != 0);
689 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
692 if (resp->result != 0)
693 printf("Request ARP_DEL failed (%u)\n", resp->result);
695 TAILQ_REMOVE(&arp_table, old_rule, entries);
700 /* Free message buffer */
701 rte_ctrlmbuf_free((struct rte_mbuf *) msg);
704 cmdline_parse_token_string_t cmd_arp_del_arp_string =
705 TOKEN_STRING_INITIALIZER(struct cmd_arp_del_result, arp_string, "arp");
707 cmdline_parse_token_string_t cmd_arp_del_del_string =
708 TOKEN_STRING_INITIALIZER(struct cmd_arp_del_result, del_string, "del");
710 cmdline_parse_token_num_t cmd_arp_del_out_iface =
711 TOKEN_NUM_INITIALIZER(struct cmd_arp_del_result, out_iface, UINT8);
713 cmdline_parse_token_ipaddr_t cmd_arp_del_nh_ip =
714 TOKEN_IPADDR_INITIALIZER(struct cmd_arp_del_result, nh_ip);
716 cmdline_parse_inst_t cmd_arp_del = {
717 .f = cmd_arp_del_parsed,
719 .help_str = "ARP delete",
721 (void *)&cmd_arp_del_arp_string,
722 (void *)&cmd_arp_del_del_string,
723 (void *)&cmd_arp_del_out_iface,
724 (void *)&cmd_arp_del_nh_ip,
729 /* *** ARP - Print *** */
730 struct cmd_arp_print_result {
731 cmdline_fixed_string_t arp_string;
732 cmdline_fixed_string_t print_string;
736 cmd_arp_print_parsed(
737 __attribute__((unused)) void *parsed_result,
738 __attribute__((unused)) struct cmdline *cl,
739 __attribute__((unused)) void *data)
743 TAILQ_FOREACH(it, &arp_table, entries) {
744 print_arp_rule(it->arp);
748 cmdline_parse_token_string_t cmd_arp_print_arp_string =
749 TOKEN_STRING_INITIALIZER(struct cmd_arp_print_result, arp_string,
752 cmdline_parse_token_string_t cmd_arp_print_print_string =
753 TOKEN_STRING_INITIALIZER(struct cmd_arp_print_result, print_string,
756 cmdline_parse_inst_t cmd_arp_print = {
757 .f = cmd_arp_print_parsed,
759 .help_str = "ARP list",
761 (void *)&cmd_arp_print_arp_string,
762 (void *)&cmd_arp_print_print_string,
767 /* *** Routing - Add *** */
768 struct cmd_route_add_result {
769 cmdline_fixed_string_t route_string;
770 cmdline_fixed_string_t add_string;
774 cmdline_ipaddr_t nh_ip;
778 cmd_route_add_parsed(
780 __attribute__((unused)) struct cmdline *cl,
781 __attribute__((unused)) void *data)
783 struct cmd_route_add_result *params = parsed_result;
784 struct app_rule rule, *old_rule;
785 struct app_msg_req *req;
786 struct app_msg_resp *resp;
790 uint32_t core_id = app_get_first_core_id(APP_CORE_RT);
792 if (core_id == RTE_MAX_LCORE) {
793 printf("Routing not performed by any CPU core\n");
797 struct rte_ring *ring_req = app_get_ring_req(core_id);
798 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
801 if ((params->depth == 0) || (params->depth > 32)) {
802 printf("Illegal value for depth parameter (%u)\n",
807 if (params->port >= app.n_ports) {
808 printf("Illegal value for port parameter (%u)\n", params->port);
813 memset(&rule, 0, sizeof(rule));
814 rule.routing.key.ip = rte_bswap32((uint32_t)
815 params->ip.addr.ipv4.s_addr);
816 rule.routing.key.depth = params->depth;
817 rule.routing.port = params->port;
819 rte_bswap32((uint32_t) params->nh_ip.addr.ipv4.s_addr);
821 /* Check rule existence */
822 IS_RULE_PRESENT(old_rule, rule.routing.key, routing_table, routing);
823 if ((old_rule == NULL) && (n_routing_rules == app.max_routing_rules)) {
824 printf("Routing table is full.\n");
828 printf("Adding route: ");
829 print_routing_rule(rule.routing);
831 /* Allocate message buffer */
832 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
834 rte_panic("Unable to allocate new message\n");
836 /* Fill request message */
837 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
838 req->type = APP_MSG_REQ_RT_ADD;
839 req->routing_add.ip = rule.routing.key.ip;
840 req->routing_add.depth = rule.routing.key.depth;
841 req->routing_add.port = rule.routing.port;
842 req->routing_add.nh_ip = rule.routing.nh_ip;
846 status = rte_ring_sp_enqueue(ring_req, msg);
847 } while (status == -ENOBUFS);
849 /* Wait for response */
851 status = rte_ring_sc_dequeue(ring_resp, &msg);
852 } while (status != 0);
853 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
856 if (resp->result != 0)
857 printf("Request ROUTE_ADD failed (%u)\n", resp->result);
859 if (old_rule == NULL) {
860 struct app_rule *new_rule = (struct app_rule *)
861 rte_zmalloc_socket("CLI",
862 sizeof(struct app_rule),
866 if (new_rule == NULL)
867 rte_panic("Unable to allocate new rule\n");
869 memcpy(new_rule, &rule, sizeof(rule));
870 TAILQ_INSERT_TAIL(&routing_table, new_rule, entries);
873 old_rule->routing.port = rule.routing.port;
874 old_rule->routing.nh_ip = rule.routing.nh_ip;
878 /* Free message buffer */
879 rte_ctrlmbuf_free((struct rte_mbuf *) msg);
882 cmdline_parse_token_string_t cmd_route_add_route_string =
883 TOKEN_STRING_INITIALIZER(struct cmd_route_add_result, route_string,
886 cmdline_parse_token_string_t cmd_route_add_add_string =
887 TOKEN_STRING_INITIALIZER(struct cmd_route_add_result, add_string,
890 cmdline_parse_token_ipaddr_t cmd_route_add_ip =
891 TOKEN_IPADDR_INITIALIZER(struct cmd_route_add_result, ip);
893 cmdline_parse_token_num_t cmd_route_add_depth =
894 TOKEN_NUM_INITIALIZER(struct cmd_route_add_result, depth, UINT8);
896 cmdline_parse_token_num_t cmd_route_add_port =
897 TOKEN_NUM_INITIALIZER(struct cmd_route_add_result, port, UINT8);
899 cmdline_parse_token_ipaddr_t cmd_route_add_nh_ip =
900 TOKEN_IPADDR_INITIALIZER(struct cmd_route_add_result, nh_ip);
902 cmdline_parse_inst_t cmd_route_add = {
903 .f = cmd_route_add_parsed,
905 .help_str = "Route add",
907 (void *)&cmd_route_add_route_string,
908 (void *)&cmd_route_add_add_string,
909 (void *)&cmd_route_add_ip,
910 (void *)&cmd_route_add_depth,
911 (void *)&cmd_route_add_port,
912 (void *)&cmd_route_add_nh_ip,
917 /* *** Routing - Del *** */
918 struct cmd_route_del_result {
919 cmdline_fixed_string_t route_string;
920 cmdline_fixed_string_t del_string;
926 cmd_route_del_parsed(
928 __attribute__((unused)) struct cmdline *cl,
929 __attribute__((unused)) void *data)
931 struct cmd_route_del_result *params = parsed_result;
932 struct app_rule rule, *old_rule;
933 struct app_msg_req *req;
934 struct app_msg_resp *resp;
938 uint32_t core_id = app_get_first_core_id(APP_CORE_RT);
940 if (core_id == RTE_MAX_LCORE) {
941 printf("Routing not performed by any CPU core\n");
945 struct rte_ring *ring_req = app_get_ring_req(core_id);
946 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
949 if ((params->depth == 0) || (params->depth > 32)) {
950 printf("Illegal value for depth parameter (%u)\n",
956 memset(&rule, 0, sizeof(rule));
957 rule.routing.key.ip = rte_bswap32((uint32_t)
958 params->ip.addr.ipv4.s_addr);
959 rule.routing.key.depth = params->depth;
961 /* Check rule existence */
962 IS_RULE_PRESENT(old_rule, rule.routing.key, routing_table, routing);
963 if (old_rule == NULL)
966 printf("Deleting route: ");
967 print_routing_rule(old_rule->routing);
969 /* Allocate message buffer */
970 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
972 rte_panic("Unable to allocate new message\n");
974 /* Fill request message */
975 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
976 req->type = APP_MSG_REQ_RT_DEL;
977 req->routing_del.ip = rule.routing.key.ip;
978 req->routing_del.depth = rule.routing.key.depth;
982 status = rte_ring_sp_enqueue(ring_req, msg);
983 } while (status == -ENOBUFS);
985 /* Wait for response */
987 status = rte_ring_sc_dequeue(ring_resp, &msg);
988 } while (status != 0);
989 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
992 if (resp->result != 0)
993 printf("Request ROUTE_DEL failed %u)\n", resp->result);
995 TAILQ_REMOVE(&routing_table, old_rule, entries);
1000 /* Free message buffer */
1001 rte_ctrlmbuf_free((struct rte_mbuf *)msg);
1004 cmdline_parse_token_string_t cmd_route_del_route_string =
1005 TOKEN_STRING_INITIALIZER(struct cmd_route_del_result, route_string,
1008 cmdline_parse_token_string_t cmd_route_del_del_string =
1009 TOKEN_STRING_INITIALIZER(struct cmd_route_del_result, del_string,
1012 cmdline_parse_token_ipaddr_t cmd_route_del_ip =
1013 TOKEN_IPADDR_INITIALIZER(struct cmd_route_del_result, ip);
1015 cmdline_parse_token_num_t cmd_route_del_depth =
1016 TOKEN_NUM_INITIALIZER(struct cmd_route_del_result, depth, UINT8);
1018 cmdline_parse_inst_t cmd_route_del = {
1019 .f = cmd_route_del_parsed,
1021 .help_str = "Route delete",
1023 (void *)&cmd_route_del_route_string,
1024 (void *)&cmd_route_del_del_string,
1025 (void *)&cmd_route_del_ip,
1026 (void *)&cmd_route_del_depth,
1031 /* *** Routing - Print *** */
1032 struct cmd_routing_print_result {
1033 cmdline_fixed_string_t routing_string;
1034 cmdline_fixed_string_t print_string;
1038 cmd_routing_print_parsed(
1039 __attribute__((unused)) void *parsed_result,
1040 __attribute__((unused)) struct cmdline *cl,
1041 __attribute__((unused)) void *data)
1043 struct app_rule *it;
1045 TAILQ_FOREACH(it, &routing_table, entries) {
1046 print_routing_rule(it->routing);
1050 cmdline_parse_token_string_t cmd_routing_print_routing_string =
1051 TOKEN_STRING_INITIALIZER(struct cmd_routing_print_result,
1052 routing_string, "route");
1054 cmdline_parse_token_string_t cmd_routing_print_print_string =
1055 TOKEN_STRING_INITIALIZER(struct cmd_routing_print_result, print_string,
1058 cmdline_parse_inst_t cmd_routing_print = {
1059 .f = cmd_routing_print_parsed,
1061 .help_str = "Route list",
1063 (void *)&cmd_routing_print_routing_string,
1064 (void *)&cmd_routing_print_print_string,
1069 #ifdef RTE_LIBRTE_ACL
1071 /* *** Firewall - Add *** */
1072 struct cmd_firewall_add_result {
1073 cmdline_fixed_string_t firewall_string;
1074 cmdline_fixed_string_t add_string;
1076 cmdline_ipaddr_t src_ip;
1077 uint32_t src_ip_mask;
1078 cmdline_ipaddr_t dst_ip;
1079 uint32_t dst_ip_mask;
1080 uint16_t src_port_from;
1081 uint16_t src_port_to;
1082 uint16_t dst_port_from;
1083 uint16_t dst_port_to;
1090 cmd_firewall_add_parsed(
1091 void *parsed_result,
1092 __attribute__((unused)) struct cmdline *cl,
1093 __attribute__((unused)) void *data)
1095 struct cmd_firewall_add_result *params = parsed_result;
1096 struct app_rule rule, *old_rule;
1097 struct rte_mbuf *msg;
1098 struct app_msg_req *req;
1099 struct app_msg_resp *resp;
1102 uint32_t core_id = app_get_first_core_id(APP_CORE_FW);
1104 if (core_id == RTE_MAX_LCORE) {
1105 printf("Firewall not performed by any CPU core\n");
1109 struct rte_ring *ring_req = app_get_ring_req(core_id);
1110 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
1113 if (params->port >= app.n_ports) {
1114 printf("Illegal value for port parameter (%u)\n", params->port);
1119 memset(&rule, 0, sizeof(rule));
1120 rule.firewall.priority = params->priority;
1121 rule.firewall.key.src_ip =
1122 rte_bswap32((uint32_t)params->src_ip.addr.ipv4.s_addr);
1123 rule.firewall.key.src_ip_mask = params->src_ip_mask;
1124 rule.firewall.key.dst_ip =
1125 rte_bswap32((uint32_t)params->dst_ip.addr.ipv4.s_addr);
1126 rule.firewall.key.dst_ip_mask = params->dst_ip_mask;
1127 rule.firewall.key.src_port_from = params->src_port_from;
1128 rule.firewall.key.src_port_to = params->src_port_to;
1129 rule.firewall.key.dst_port_from = params->dst_port_from;
1130 rule.firewall.key.dst_port_to = params->dst_port_to;
1131 rule.firewall.key.proto = params->proto;
1132 rule.firewall.key.proto_mask = params->proto_mask;
1133 rule.firewall.port = params->port;
1135 /* Check rule existence */
1136 IS_RULE_PRESENT(old_rule, rule.firewall.key, firewall_table, firewall);
1137 if ((old_rule == NULL) &&
1138 (n_firewall_rules == app.max_firewall_rules)) {
1139 printf("Firewall table is full.\n");
1143 printf("Adding firewall rule: ");
1144 print_firewall_rule(rule.firewall);
1146 /* Allocate message buffer */
1147 msg = rte_ctrlmbuf_alloc(app.msg_pool);
1149 rte_panic("Unable to allocate new message\n");
1151 /* Fill request message */
1152 req = (struct app_msg_req *)rte_ctrlmbuf_data(msg);
1153 req->type = APP_MSG_REQ_FW_ADD;
1154 req->firewall_add.add_params.priority = rule.firewall.priority;
1155 req->firewall_add.add_params.field_value[1].value.u32 =
1156 rule.firewall.key.src_ip;
1157 req->firewall_add.add_params.field_value[1].mask_range.u32 =
1158 rule.firewall.key.src_ip_mask;
1159 req->firewall_add.add_params.field_value[2].value.u32 =
1160 rule.firewall.key.dst_ip;
1161 req->firewall_add.add_params.field_value[2].mask_range.u32 =
1162 rule.firewall.key.dst_ip_mask;
1163 req->firewall_add.add_params.field_value[3].value.u16 =
1164 rule.firewall.key.src_port_from;
1165 req->firewall_add.add_params.field_value[3].mask_range.u16 =
1166 rule.firewall.key.src_port_to;
1167 req->firewall_add.add_params.field_value[4].value.u16 =
1168 rule.firewall.key.dst_port_from;
1169 req->firewall_add.add_params.field_value[4].mask_range.u16 =
1170 rule.firewall.key.dst_port_to;
1171 req->firewall_add.add_params.field_value[0].value.u8 =
1172 rule.firewall.key.proto;
1173 req->firewall_add.add_params.field_value[0].mask_range.u8 =
1174 rule.firewall.key.proto_mask;
1175 req->firewall_add.port = rule.firewall.port;
1179 status = rte_ring_sp_enqueue(ring_req, (void *) msg);
1180 } while (status == -ENOBUFS);
1182 /* Wait for response */
1184 status = rte_ring_sc_dequeue(ring_resp, (void **) &msg);
1185 } while (status != 0);
1186 resp = (struct app_msg_resp *)rte_ctrlmbuf_data(msg);
1188 /* Check response */
1189 if (resp->result != 0)
1190 printf("Request FIREWALL_ADD failed (%u)\n", resp->result);
1192 if (old_rule == NULL) {
1193 struct app_rule *new_rule = (struct app_rule *)
1194 rte_zmalloc_socket("CLI",
1195 sizeof(struct app_rule),
1196 RTE_CACHE_LINE_SIZE,
1199 memcpy(new_rule, &rule, sizeof(rule));
1200 TAILQ_INSERT_TAIL(&firewall_table, new_rule, entries);
1203 old_rule->firewall.priority = rule.firewall.priority;
1204 old_rule->firewall.port = rule.firewall.port;
1208 /* Free message buffer */
1209 rte_ctrlmbuf_free(msg);
1212 cmdline_parse_token_string_t cmd_firewall_add_firewall_string =
1213 TOKEN_STRING_INITIALIZER(struct cmd_firewall_add_result,
1214 firewall_string, "firewall");
1216 cmdline_parse_token_string_t cmd_firewall_add_add_string =
1217 TOKEN_STRING_INITIALIZER(struct cmd_firewall_add_result, add_string,
1220 cmdline_parse_token_num_t cmd_firewall_add_priority =
1221 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, priority, INT32);
1223 cmdline_parse_token_ipaddr_t cmd_firewall_add_src_ip =
1224 TOKEN_IPADDR_INITIALIZER(struct cmd_firewall_add_result, src_ip);
1225 cmdline_parse_token_num_t cmd_firewall_add_src_ip_mask =
1226 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, src_ip_mask,
1229 cmdline_parse_token_ipaddr_t cmd_firewall_add_dst_ip =
1230 TOKEN_IPADDR_INITIALIZER(struct cmd_firewall_add_result, dst_ip);
1231 cmdline_parse_token_num_t cmd_firewall_add_dst_ip_mask =
1232 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, dst_ip_mask,
1235 cmdline_parse_token_num_t cmd_firewall_add_src_port_from =
1236 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, src_port_from,
1238 cmdline_parse_token_num_t cmd_firewall_add_src_port_to =
1239 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, src_port_to,
1242 cmdline_parse_token_num_t cmd_firewall_add_dst_port_from =
1243 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, dst_port_from,
1245 cmdline_parse_token_num_t cmd_firewall_add_dst_port_to =
1246 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, dst_port_to,
1249 cmdline_parse_token_num_t cmd_firewall_add_proto =
1250 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, proto, UINT8);
1251 cmdline_parse_token_num_t cmd_firewall_add_proto_mask =
1252 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, proto_mask,
1254 cmdline_parse_token_num_t cmd_firewall_add_port =
1255 TOKEN_NUM_INITIALIZER(struct cmd_firewall_add_result, port, UINT8);
1257 cmdline_parse_inst_t cmd_firewall_add = {
1258 .f = cmd_firewall_add_parsed,
1260 .help_str = "Firewall rule add",
1262 (void *)&cmd_firewall_add_firewall_string,
1263 (void *)&cmd_firewall_add_add_string,
1264 (void *)&cmd_firewall_add_priority,
1265 (void *)&cmd_firewall_add_src_ip,
1266 (void *)&cmd_firewall_add_src_ip_mask,
1267 (void *)&cmd_firewall_add_dst_ip,
1268 (void *)&cmd_firewall_add_dst_ip_mask,
1269 (void *)&cmd_firewall_add_src_port_from,
1270 (void *)&cmd_firewall_add_src_port_to,
1271 (void *)&cmd_firewall_add_dst_port_from,
1272 (void *)&cmd_firewall_add_dst_port_to,
1273 (void *)&cmd_firewall_add_proto,
1274 (void *)&cmd_firewall_add_proto_mask,
1275 (void *)&cmd_firewall_add_port,
1280 /* *** firewall - Del *** */
1281 struct cmd_firewall_del_result {
1282 cmdline_fixed_string_t firewall_string;
1283 cmdline_fixed_string_t del_string;
1284 cmdline_ipaddr_t src_ip;
1285 uint32_t src_ip_mask;
1286 cmdline_ipaddr_t dst_ip;
1287 uint32_t dst_ip_mask;
1288 uint16_t src_port_from;
1289 uint16_t src_port_to;
1290 uint16_t dst_port_from;
1291 uint16_t dst_port_to;
1297 cmd_firewall_del_parsed(
1298 void *parsed_result,
1299 __attribute__((unused)) struct cmdline *cl,
1300 __attribute__((unused)) void *data)
1302 struct cmd_firewall_del_result *params = parsed_result;
1303 struct app_rule rule, *old_rule;
1304 struct rte_mbuf *msg;
1305 struct app_msg_req *req;
1306 struct app_msg_resp *resp;
1309 uint32_t core_id = app_get_first_core_id(APP_CORE_FW);
1311 if (core_id == RTE_MAX_LCORE) {
1312 printf("Firewall not performed by any CPU core\n");
1316 struct rte_ring *ring_req = app_get_ring_req(core_id);
1317 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
1322 memset(&rule, 0, sizeof(rule));
1323 rule.firewall.key.src_ip =
1324 rte_bswap32((uint32_t) params->src_ip.addr.ipv4.s_addr);
1325 rule.firewall.key.src_ip_mask = params->src_ip_mask;
1326 rule.firewall.key.dst_ip =
1327 rte_bswap32((uint32_t) params->dst_ip.addr.ipv4.s_addr);
1328 rule.firewall.key.dst_ip_mask = params->dst_ip_mask;
1329 rule.firewall.key.src_port_from = params->src_port_from;
1330 rule.firewall.key.src_port_to = params->src_port_to;
1331 rule.firewall.key.dst_port_from = params->dst_port_from;
1332 rule.firewall.key.dst_port_to = params->dst_port_to;
1333 rule.firewall.key.proto = params->proto;
1334 rule.firewall.key.proto_mask = params->proto_mask;
1336 /* Check rule existence */
1337 IS_RULE_PRESENT(old_rule, rule.firewall.key, firewall_table, firewall);
1338 if (old_rule == NULL)
1341 printf("Deleting firewall rule: ");
1342 print_firewall_rule(old_rule->firewall);
1344 /* Allocate message buffer */
1345 msg = rte_ctrlmbuf_alloc(app.msg_pool);
1347 rte_panic("Unable to allocate new message\n");
1349 /* Fill request message */
1350 req = (struct app_msg_req *)rte_ctrlmbuf_data(msg);
1351 memset(&req->firewall_del, 0, sizeof(req->firewall_del));
1352 req->type = APP_MSG_REQ_FW_DEL;
1353 req->firewall_del.delete_params.field_value[1].value.u32 =
1354 rule.firewall.key.src_ip;
1355 req->firewall_del.delete_params.field_value[1].mask_range.u32 =
1356 rule.firewall.key.src_ip_mask;
1357 req->firewall_del.delete_params.field_value[2].value.u32 =
1358 rule.firewall.key.dst_ip;
1359 req->firewall_del.delete_params.field_value[2].mask_range.u32 =
1360 rule.firewall.key.dst_ip_mask;
1361 req->firewall_del.delete_params.field_value[3].value.u16 =
1362 rule.firewall.key.src_port_from;
1363 req->firewall_del.delete_params.field_value[3].mask_range.u16 =
1364 rule.firewall.key.src_port_to;
1365 req->firewall_del.delete_params.field_value[4].value.u16 =
1366 rule.firewall.key.dst_port_from;
1367 req->firewall_del.delete_params.field_value[4].mask_range.u16 =
1368 rule.firewall.key.dst_port_to;
1369 req->firewall_del.delete_params.field_value[0].value.u8 =
1370 rule.firewall.key.proto;
1371 req->firewall_del.delete_params.field_value[0].mask_range.u8 =
1372 rule.firewall.key.proto_mask;
1376 status = rte_ring_sp_enqueue(ring_req, (void *) msg);
1377 } while (status == -ENOBUFS);
1379 /* Wait for response */
1381 status = rte_ring_sc_dequeue(ring_resp, (void **) &msg);
1382 } while (status != 0);
1383 resp = (struct app_msg_resp *)rte_ctrlmbuf_data(msg);
1385 /* Check response */
1386 if (resp->result != 0)
1387 printf("Request FIREWALL_DEL failed %u)\n", resp->result);
1389 TAILQ_REMOVE(&firewall_table, old_rule, entries);
1394 /* Free message buffer */
1395 rte_ctrlmbuf_free(msg);
1398 cmdline_parse_token_string_t cmd_firewall_del_firewall_string =
1399 TOKEN_STRING_INITIALIZER(struct cmd_firewall_del_result,
1400 firewall_string, "firewall");
1402 cmdline_parse_token_string_t cmd_firewall_del_del_string =
1403 TOKEN_STRING_INITIALIZER(struct cmd_firewall_del_result, del_string,
1406 cmdline_parse_token_ipaddr_t cmd_firewall_del_src_ip =
1407 TOKEN_IPADDR_INITIALIZER(struct cmd_firewall_del_result, src_ip);
1408 cmdline_parse_token_num_t cmd_firewall_del_src_ip_mask =
1409 TOKEN_NUM_INITIALIZER(struct cmd_firewall_del_result, src_ip_mask,
1412 cmdline_parse_token_ipaddr_t cmd_firewall_del_dst_ip =
1413 TOKEN_IPADDR_INITIALIZER(struct cmd_firewall_del_result, dst_ip);
1414 cmdline_parse_token_num_t cmd_firewall_del_dst_ip_mask =
1415 TOKEN_NUM_INITIALIZER(struct cmd_firewall_del_result, dst_ip_mask,
1418 cmdline_parse_token_num_t cmd_firewall_del_src_port_from =
1419 TOKEN_NUM_INITIALIZER(struct cmd_firewall_del_result, src_port_from,
1421 cmdline_parse_token_num_t cmd_firewall_del_src_port_to =
1422 TOKEN_NUM_INITIALIZER(struct cmd_firewall_del_result, src_port_to,
1425 cmdline_parse_token_num_t cmd_firewall_del_dst_port_from =
1426 TOKEN_NUM_INITIALIZER(struct cmd_firewall_del_result, dst_port_from,
1428 cmdline_parse_token_num_t cmd_firewall_del_dst_port_to =
1429 TOKEN_NUM_INITIALIZER(struct cmd_firewall_del_result, dst_port_to,
1432 cmdline_parse_token_num_t cmd_firewall_del_proto =
1433 TOKEN_NUM_INITIALIZER(struct cmd_firewall_del_result, proto, UINT8);
1434 cmdline_parse_token_num_t cmd_firewall_del_proto_mask =
1435 TOKEN_NUM_INITIALIZER(struct cmd_firewall_del_result, proto_mask,
1438 cmdline_parse_inst_t cmd_firewall_del = {
1439 .f = cmd_firewall_del_parsed,
1441 .help_str = "Firewall rule delete",
1443 (void *)&cmd_firewall_del_firewall_string,
1444 (void *)&cmd_firewall_del_del_string,
1445 (void *)&cmd_firewall_del_src_ip,
1446 (void *)&cmd_firewall_del_src_ip_mask,
1447 (void *)&cmd_firewall_del_dst_ip,
1448 (void *)&cmd_firewall_del_dst_ip_mask,
1449 (void *)&cmd_firewall_del_src_port_from,
1450 (void *)&cmd_firewall_del_src_port_to,
1451 (void *)&cmd_firewall_del_dst_port_from,
1452 (void *)&cmd_firewall_del_dst_port_to,
1453 (void *)&cmd_firewall_del_proto,
1454 (void *)&cmd_firewall_del_proto_mask,
1459 /* *** Firewall - Print *** */
1460 struct cmd_firewall_print_result {
1461 cmdline_fixed_string_t firewall_string;
1462 cmdline_fixed_string_t print_string;
1466 cmd_firewall_print_parsed(
1467 __attribute__((unused)) void *parsed_result,
1468 __attribute__((unused)) struct cmdline *cl,
1469 __attribute__((unused)) void *data)
1471 struct app_rule *it;
1473 TAILQ_FOREACH(it, &firewall_table, entries) {
1474 print_firewall_rule(it->firewall);
1478 cmdline_parse_token_string_t cmd_firewall_print_firewall_string =
1479 TOKEN_STRING_INITIALIZER(struct cmd_firewall_print_result,
1480 firewall_string, "firewall");
1482 cmdline_parse_token_string_t cmd_firewall_print_print_string =
1483 TOKEN_STRING_INITIALIZER(struct cmd_firewall_print_result, print_string,
1486 cmdline_parse_inst_t cmd_firewall_print = {
1487 .f = cmd_firewall_print_parsed,
1489 .help_str = "Firewall rules list",
1491 (void *)&cmd_firewall_print_firewall_string,
1492 (void *)&cmd_firewall_print_print_string,
1499 /* *** Flow Classification - Add All *** */
1500 struct cmd_flow_add_all_result {
1501 cmdline_fixed_string_t flow_string;
1502 cmdline_fixed_string_t add_string;
1503 cmdline_fixed_string_t all_string;
1507 cmd_flow_add_all_parsed(
1508 __attribute__((unused)) void *parsed_result,
1509 __attribute__((unused)) struct cmdline *cl,
1510 __attribute__((unused)) void *data)
1512 struct app_msg_req *req;
1513 struct app_msg_resp *resp;
1517 struct rte_ring *ring_req =
1518 app_get_ring_req(app_get_first_core_id(APP_CORE_FC));
1519 struct rte_ring *ring_resp =
1520 app_get_ring_resp(app_get_first_core_id(APP_CORE_FC));
1522 /* Allocate message buffer */
1523 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
1525 rte_panic("Unable to allocate new message\n");
1527 /* Fill request message */
1528 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
1529 memset(req, 0, sizeof(struct app_msg_req));
1531 req->type = APP_MSG_REQ_FC_ADD_ALL;
1535 status = rte_ring_sp_enqueue(ring_req, msg);
1536 } while (status == -ENOBUFS);
1538 /* Wait for response */
1540 status = rte_ring_sc_dequeue(ring_resp, &msg);
1541 } while (status != 0);
1542 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
1544 /* Check response */
1545 if (resp->result != 0)
1546 printf("Request FLOW_ADD_ALL failed (%u)\n", resp->result);
1548 /* Free message buffer */
1549 rte_ctrlmbuf_free((struct rte_mbuf *)msg);
1552 cmdline_parse_token_string_t cmd_flow_add_all_flow_string =
1553 TOKEN_STRING_INITIALIZER(struct cmd_flow_add_all_result, flow_string,
1556 cmdline_parse_token_string_t cmd_flow_add_all_add_string =
1557 TOKEN_STRING_INITIALIZER(struct cmd_flow_add_all_result, add_string,
1560 cmdline_parse_token_string_t cmd_flow_add_all_all_string =
1561 TOKEN_STRING_INITIALIZER(struct cmd_flow_add_all_result, all_string,
1564 cmdline_parse_inst_t cmd_flow_add_all = {
1565 .f = cmd_flow_add_all_parsed,
1567 .help_str = "Flow table initialization based on hard-coded rule",
1569 (void *)&cmd_flow_add_all_flow_string,
1570 (void *)&cmd_flow_add_all_add_string,
1571 (void *)&cmd_flow_add_all_all_string,
1576 /* *** Flow Classification - Add *** */
1577 struct cmd_flow_add_result {
1578 cmdline_fixed_string_t flow_string;
1579 cmdline_fixed_string_t add_string;
1580 cmdline_ipaddr_t src_ip;
1581 cmdline_ipaddr_t dst_ip;
1589 cmd_flow_add_parsed(
1590 void *parsed_result,
1591 __attribute__((unused)) struct cmdline *cl,
1592 __attribute__((unused)) void *data)
1594 struct cmd_flow_add_result *params = parsed_result;
1595 struct app_rule rule, *old_rule;
1596 struct app_msg_req *req;
1597 struct app_msg_resp *resp;
1601 uint32_t core_id = app_get_first_core_id(APP_CORE_FC);
1603 if (core_id == RTE_MAX_LCORE) {
1604 printf("Flow classification not performed by any CPU core\n");
1608 struct rte_ring *ring_req = app_get_ring_req(core_id);
1609 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
1612 if (params->port >= app.n_ports) {
1613 printf("Illegal value for port parameter (%u)\n", params->port);
1618 memset(&rule, 0, sizeof(rule));
1619 rule.flow.key.src_ip =
1620 rte_bswap32((uint32_t)params->src_ip.addr.ipv4.s_addr);
1621 rule.flow.key.dst_ip =
1622 rte_bswap32((uint32_t)params->dst_ip.addr.ipv4.s_addr);
1623 rule.flow.key.src_port = params->src_port;
1624 rule.flow.key.dst_port = params->dst_port;
1625 rule.flow.key.proto = params->proto;
1626 rule.flow.port = params->port;
1628 /* Check rule existence */
1629 IS_RULE_PRESENT(old_rule, rule.flow.key, flow_table, flow);
1630 if ((old_rule == NULL) && (n_flow_rules == app.max_flow_rules)) {
1631 printf("Flow table is full.\n");
1635 printf("Adding flow: ");
1636 print_flow_rule(rule.flow);
1638 /* Allocate message buffer */
1639 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
1641 rte_panic("Unable to allocate new message\n");
1643 /* Fill request message */
1644 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
1645 memset(req, 0, sizeof(struct app_msg_req));
1647 req->type = APP_MSG_REQ_FC_ADD;
1648 req->flow_classif_add.key.ip_src = rte_bswap32(rule.flow.key.src_ip);
1649 req->flow_classif_add.key.ip_dst = rte_bswap32(rule.flow.key.dst_ip);
1650 req->flow_classif_add.key.port_src =
1651 rte_bswap16(rule.flow.key.src_port);
1652 req->flow_classif_add.key.port_dst =
1653 rte_bswap16(rule.flow.key.dst_port);
1654 req->flow_classif_add.key.proto = rule.flow.key.proto;
1655 req->flow_classif_add.port = rule.flow.port;
1659 status = rte_ring_sp_enqueue(ring_req, msg);
1660 } while (status == -ENOBUFS);
1662 /* Wait for response */
1664 status = rte_ring_sc_dequeue(ring_resp, &msg);
1665 } while (status != 0);
1666 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
1668 /* Check response */
1669 if (resp->result != 0)
1670 printf("Request FLOW_ADD failed (%u)\n", resp->result);
1672 if (old_rule == NULL) {
1673 struct app_rule *new_rule = (struct app_rule *)
1674 rte_zmalloc_socket("CLI",
1675 sizeof(struct app_rule),
1676 RTE_CACHE_LINE_SIZE,
1679 if (new_rule == NULL)
1680 rte_panic("Unable to allocate new rule\n");
1682 memcpy(new_rule, &rule, sizeof(rule));
1683 TAILQ_INSERT_TAIL(&flow_table, new_rule, entries);
1686 old_rule->flow.port = rule.flow.port;
1689 /* Free message buffer */
1690 rte_ctrlmbuf_free((struct rte_mbuf *)msg);
1693 cmdline_parse_token_string_t cmd_flow_add_flow_string =
1694 TOKEN_STRING_INITIALIZER(struct cmd_flow_add_result, flow_string,
1697 cmdline_parse_token_string_t cmd_flow_add_add_string =
1698 TOKEN_STRING_INITIALIZER(struct cmd_flow_add_result, add_string, "add");
1700 cmdline_parse_token_ipaddr_t cmd_flow_add_src_ip =
1701 TOKEN_IPADDR_INITIALIZER(struct cmd_flow_add_result, src_ip);
1703 cmdline_parse_token_ipaddr_t cmd_flow_add_dst_ip =
1704 TOKEN_IPADDR_INITIALIZER(struct cmd_flow_add_result, dst_ip);
1706 cmdline_parse_token_num_t cmd_flow_add_src_port =
1707 TOKEN_NUM_INITIALIZER(struct cmd_flow_add_result, src_port, UINT16);
1709 cmdline_parse_token_num_t cmd_flow_add_dst_port =
1710 TOKEN_NUM_INITIALIZER(struct cmd_flow_add_result, dst_port, UINT16);
1712 cmdline_parse_token_num_t cmd_flow_add_proto =
1713 TOKEN_NUM_INITIALIZER(struct cmd_flow_add_result, proto, UINT8);
1715 cmdline_parse_token_num_t cmd_flow_add_port =
1716 TOKEN_NUM_INITIALIZER(struct cmd_flow_add_result, port, UINT8);
1718 cmdline_parse_inst_t cmd_flow_add = {
1719 .f = cmd_flow_add_parsed,
1721 .help_str = "Flow add",
1723 (void *)&cmd_flow_add_flow_string,
1724 (void *)&cmd_flow_add_add_string,
1725 (void *)&cmd_flow_add_src_ip,
1726 (void *)&cmd_flow_add_dst_ip,
1727 (void *)&cmd_flow_add_src_port,
1728 (void *)&cmd_flow_add_dst_port,
1729 (void *)&cmd_flow_add_proto,
1730 (void *)&cmd_flow_add_port,
1735 /* *** Flow Classification - Del *** */
1736 struct cmd_flow_del_result {
1737 cmdline_fixed_string_t flow_string;
1738 cmdline_fixed_string_t del_string;
1739 cmdline_ipaddr_t src_ip;
1740 cmdline_ipaddr_t dst_ip;
1747 cmd_flow_del_parsed(
1748 void *parsed_result,
1749 __attribute__((unused)) struct cmdline *cl,
1750 __attribute__((unused)) void *data)
1752 struct cmd_flow_del_result *params = parsed_result;
1753 struct app_rule rule, *old_rule;
1754 struct app_msg_req *req;
1755 struct app_msg_resp *resp;
1759 uint32_t core_id = app_get_first_core_id(APP_CORE_FC);
1761 if (core_id == RTE_MAX_LCORE) {
1762 printf("Flow classification not performed by any CPU core.\n");
1766 struct rte_ring *ring_req = app_get_ring_req(core_id);
1767 struct rte_ring *ring_resp = app_get_ring_resp(core_id);
1770 memset(&rule, 0, sizeof(rule));
1771 rule.flow.key.src_ip =
1772 rte_bswap32((uint32_t)params->src_ip.addr.ipv4.s_addr);
1773 rule.flow.key.dst_ip =
1774 rte_bswap32((uint32_t)params->dst_ip.addr.ipv4.s_addr);
1775 rule.flow.key.src_port = params->src_port;
1776 rule.flow.key.dst_port = params->dst_port;
1777 rule.flow.key.proto = params->proto;
1779 /* Check rule existence */
1780 IS_RULE_PRESENT(old_rule, rule.flow.key, flow_table, flow);
1781 if (old_rule == NULL)
1784 printf("Deleting flow: ");
1785 print_flow_rule(old_rule->flow);
1787 /* Allocate message buffer */
1788 msg = (void *)rte_ctrlmbuf_alloc(app.msg_pool);
1790 rte_panic("Unable to allocate new message\n");
1792 /* Fill request message */
1793 req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
1794 memset(req, 0, sizeof(struct app_msg_req));
1796 req->type = APP_MSG_REQ_FC_DEL;
1797 req->flow_classif_del.key.ip_src = rte_bswap32(rule.flow.key.src_ip);
1798 req->flow_classif_del.key.ip_dst = rte_bswap32(rule.flow.key.dst_ip);
1799 req->flow_classif_del.key.port_src =
1800 rte_bswap32(rule.flow.key.src_port);
1801 req->flow_classif_del.key.port_dst =
1802 rte_bswap32(rule.flow.key.dst_port);
1803 req->flow_classif_del.key.proto = rule.flow.key.proto;
1807 status = rte_ring_sp_enqueue(ring_req, msg);
1808 } while (status == -ENOBUFS);
1810 /* Wait for response */
1812 status = rte_ring_sc_dequeue(ring_resp, &msg);
1813 } while (status != 0);
1814 resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
1816 /* Check response */
1817 if (resp->result != 0)
1818 printf("Request FLOW_DEL failed (%u)\n", resp->result);
1820 TAILQ_REMOVE(&flow_table, old_rule, entries);
1825 /* Free message buffer */
1826 rte_ctrlmbuf_free((struct rte_mbuf *)msg);
1829 cmdline_parse_token_string_t cmd_flow_del_flow_string =
1830 TOKEN_STRING_INITIALIZER(struct cmd_flow_del_result, flow_string,
1833 cmdline_parse_token_string_t cmd_flow_del_del_string =
1834 TOKEN_STRING_INITIALIZER(struct cmd_flow_del_result, del_string, "del");
1836 cmdline_parse_token_ipaddr_t cmd_flow_del_src_ip =
1837 TOKEN_IPADDR_INITIALIZER(struct cmd_flow_del_result, src_ip);
1839 cmdline_parse_token_ipaddr_t cmd_flow_del_dst_ip =
1840 TOKEN_IPADDR_INITIALIZER(struct cmd_flow_del_result, dst_ip);
1842 cmdline_parse_token_num_t cmd_flow_del_src_port =
1843 TOKEN_NUM_INITIALIZER(struct cmd_flow_del_result, src_port, UINT16);
1845 cmdline_parse_token_num_t cmd_flow_del_dst_port =
1846 TOKEN_NUM_INITIALIZER(struct cmd_flow_del_result, dst_port, UINT16);
1848 cmdline_parse_token_num_t cmd_flow_del_proto =
1849 TOKEN_NUM_INITIALIZER(struct cmd_flow_del_result, proto, UINT8);
1851 cmdline_parse_inst_t cmd_flow_del = {
1852 .f = cmd_flow_del_parsed,
1854 .help_str = "Flow delete",
1856 (void *)&cmd_flow_del_flow_string,
1857 (void *)&cmd_flow_del_del_string,
1858 (void *)&cmd_flow_del_src_ip,
1859 (void *)&cmd_flow_del_dst_ip,
1860 (void *)&cmd_flow_del_src_port,
1861 (void *)&cmd_flow_del_dst_port,
1862 (void *)&cmd_flow_del_proto,
1867 /* *** Flow Classification - Print *** */
1868 struct cmd_flow_print_result {
1869 cmdline_fixed_string_t flow_string;
1870 cmdline_fixed_string_t print_string;
1874 cmd_flow_print_parsed(
1875 __attribute__((unused)) void *parsed_result,
1876 __attribute__((unused)) struct cmdline *cl,
1877 __attribute__((unused)) void *data)
1879 struct app_rule *it;
1881 TAILQ_FOREACH(it, &flow_table, entries) {
1882 print_flow_rule(it->flow);
1886 cmdline_parse_token_string_t cmd_flow_print_flow_string =
1887 TOKEN_STRING_INITIALIZER(struct cmd_flow_print_result, flow_string,
1890 cmdline_parse_token_string_t cmd_flow_print_print_string =
1891 TOKEN_STRING_INITIALIZER(struct cmd_flow_print_result, print_string,
1894 cmdline_parse_inst_t cmd_flow_print = {
1895 .f = cmd_flow_print_parsed,
1897 .help_str = "Flow list",
1899 (void *)&cmd_flow_print_flow_string,
1900 (void *)&cmd_flow_print_print_string,
1906 struct cmd_quit_result {
1907 cmdline_fixed_string_t quit;
1910 static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
1912 __attribute__((unused)) void *data)
1917 cmdline_parse_token_string_t cmd_quit_quit =
1918 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
1920 cmdline_parse_inst_t cmd_quit = {
1921 .f = cmd_quit_parsed,
1923 .help_str = "Exit application",
1925 (void *)&cmd_quit_quit,
1930 /* List of commands */
1931 cmdline_parse_ctx_t main_ctx[] = {
1932 (cmdline_parse_inst_t *)&cmd_flow_add,
1933 (cmdline_parse_inst_t *)&cmd_flow_del,
1934 (cmdline_parse_inst_t *)&cmd_flow_add_all,
1935 (cmdline_parse_inst_t *)&cmd_flow_print,
1936 #ifdef RTE_LIBRTE_ACL
1937 (cmdline_parse_inst_t *)&cmd_firewall_add,
1938 (cmdline_parse_inst_t *)&cmd_firewall_del,
1939 (cmdline_parse_inst_t *)&cmd_firewall_print,
1941 (cmdline_parse_inst_t *)&cmd_route_add,
1942 (cmdline_parse_inst_t *)&cmd_route_del,
1943 (cmdline_parse_inst_t *)&cmd_routing_print,
1944 (cmdline_parse_inst_t *)&cmd_arp_add,
1945 (cmdline_parse_inst_t *)&cmd_arp_del,
1946 (cmdline_parse_inst_t *)&cmd_arp_print,
1947 (cmdline_parse_inst_t *)&cmd_run_file,
1948 (cmdline_parse_inst_t *)&cmd_link_enable,
1949 (cmdline_parse_inst_t *)&cmd_link_disable,
1950 (cmdline_parse_inst_t *)&cmd_quit,
1956 app_main_loop_cmdline(void)
1959 uint32_t core_id = rte_lcore_id();
1961 RTE_LOG(INFO, USER1, "Core %u is running the command line interface\n",
1965 n_routing_rules = 0;
1966 n_firewall_rules = 0;
1969 app_init_rule_tables();
1971 cl = cmdline_stdin_new(main_ctx, "pipeline> ");
1974 cmdline_interact(cl);
1975 cmdline_stdin_exit(cl);