1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2017 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <inttypes.h> 7 #include <getopt.h> 8 9 #include <rte_eal.h> 10 #include <rte_ethdev.h> 11 #include <rte_cycles.h> 12 #include <rte_lcore.h> 13 #include <rte_mbuf.h> 14 #include <rte_flow.h> 15 #include <rte_flow_classify.h> 16 #include <rte_table_acl.h> 17 18 #define RX_RING_SIZE 1024 19 #define TX_RING_SIZE 1024 20 21 #define NUM_MBUFS 8191 22 #define MBUF_CACHE_SIZE 250 23 #define BURST_SIZE 32 24 25 #define MAX_NUM_CLASSIFY 30 26 #define FLOW_CLASSIFY_MAX_RULE_NUM 91 27 #define FLOW_CLASSIFY_MAX_PRIORITY 8 28 #define FLOW_CLASSIFIER_NAME_SIZE 64 29 30 #define COMMENT_LEAD_CHAR ('#') 31 #define OPTION_RULE_IPV4 "rule_ipv4" 32 #define RTE_LOGTYPE_FLOW_CLASSIFY RTE_LOGTYPE_USER3 33 #define flow_classify_log(format, ...) \ 34 RTE_LOG(ERR, FLOW_CLASSIFY, format, ##__VA_ARGS__) 35 36 #define uint32_t_to_char(ip, a, b, c, d) do {\ 37 *a = (unsigned char)(ip >> 24 & 0xff);\ 38 *b = (unsigned char)(ip >> 16 & 0xff);\ 39 *c = (unsigned char)(ip >> 8 & 0xff);\ 40 *d = (unsigned char)(ip & 0xff);\ 41 } while (0) 42 43 enum { 44 CB_FLD_SRC_ADDR, 45 CB_FLD_DST_ADDR, 46 CB_FLD_SRC_PORT, 47 CB_FLD_SRC_PORT_DLM, 48 CB_FLD_SRC_PORT_MASK, 49 CB_FLD_DST_PORT, 50 CB_FLD_DST_PORT_DLM, 51 CB_FLD_DST_PORT_MASK, 52 CB_FLD_PROTO, 53 CB_FLD_PRIORITY, 54 CB_FLD_NUM, 55 }; 56 57 static struct{ 58 const char *rule_ipv4_name; 59 } parm_config; 60 const char cb_port_delim[] = ":"; 61 62 static const struct rte_eth_conf port_conf_default = { 63 .rxmode = { 64 .max_rx_pkt_len = RTE_ETHER_MAX_LEN, 65 }, 66 }; 67 68 struct flow_classifier { 69 struct rte_flow_classifier *cls; 70 }; 71 72 struct flow_classifier_acl { 73 struct flow_classifier cls; 74 } __rte_cache_aligned; 75 76 /* ACL field definitions for IPv4 5 tuple rule */ 77 78 enum { 79 PROTO_FIELD_IPV4, 80 SRC_FIELD_IPV4, 81 DST_FIELD_IPV4, 82 SRCP_FIELD_IPV4, 83 DSTP_FIELD_IPV4, 84 NUM_FIELDS_IPV4 85 }; 86 87 enum { 88 PROTO_INPUT_IPV4, 89 SRC_INPUT_IPV4, 90 DST_INPUT_IPV4, 91 SRCP_DESTP_INPUT_IPV4 92 }; 93 94 static struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = { 95 /* first input field - always one byte long. */ 96 { 97 .type = RTE_ACL_FIELD_TYPE_BITMASK, 98 .size = sizeof(uint8_t), 99 .field_index = PROTO_FIELD_IPV4, 100 .input_index = PROTO_INPUT_IPV4, 101 .offset = sizeof(struct rte_ether_hdr) + 102 offsetof(struct rte_ipv4_hdr, next_proto_id), 103 }, 104 /* next input field (IPv4 source address) - 4 consecutive bytes. */ 105 { 106 /* rte_flow uses a bit mask for IPv4 addresses */ 107 .type = RTE_ACL_FIELD_TYPE_BITMASK, 108 .size = sizeof(uint32_t), 109 .field_index = SRC_FIELD_IPV4, 110 .input_index = SRC_INPUT_IPV4, 111 .offset = sizeof(struct rte_ether_hdr) + 112 offsetof(struct rte_ipv4_hdr, src_addr), 113 }, 114 /* next input field (IPv4 destination address) - 4 consecutive bytes. */ 115 { 116 /* rte_flow uses a bit mask for IPv4 addresses */ 117 .type = RTE_ACL_FIELD_TYPE_BITMASK, 118 .size = sizeof(uint32_t), 119 .field_index = DST_FIELD_IPV4, 120 .input_index = DST_INPUT_IPV4, 121 .offset = sizeof(struct rte_ether_hdr) + 122 offsetof(struct rte_ipv4_hdr, dst_addr), 123 }, 124 /* 125 * Next 2 fields (src & dst ports) form 4 consecutive bytes. 126 * They share the same input index. 127 */ 128 { 129 /* rte_flow uses a bit mask for protocol ports */ 130 .type = RTE_ACL_FIELD_TYPE_BITMASK, 131 .size = sizeof(uint16_t), 132 .field_index = SRCP_FIELD_IPV4, 133 .input_index = SRCP_DESTP_INPUT_IPV4, 134 .offset = sizeof(struct rte_ether_hdr) + 135 sizeof(struct rte_ipv4_hdr) + 136 offsetof(struct rte_tcp_hdr, src_port), 137 }, 138 { 139 /* rte_flow uses a bit mask for protocol ports */ 140 .type = RTE_ACL_FIELD_TYPE_BITMASK, 141 .size = sizeof(uint16_t), 142 .field_index = DSTP_FIELD_IPV4, 143 .input_index = SRCP_DESTP_INPUT_IPV4, 144 .offset = sizeof(struct rte_ether_hdr) + 145 sizeof(struct rte_ipv4_hdr) + 146 offsetof(struct rte_tcp_hdr, dst_port), 147 }, 148 }; 149 150 /* flow classify data */ 151 static int num_classify_rules; 152 static struct rte_flow_classify_rule *rules[MAX_NUM_CLASSIFY]; 153 static struct rte_flow_classify_ipv4_5tuple_stats ntuple_stats; 154 static struct rte_flow_classify_stats classify_stats = { 155 .stats = (void **)&ntuple_stats 156 }; 157 158 /* parameters for rte_flow_classify_validate and 159 * rte_flow_classify_table_entry_add functions 160 */ 161 162 static struct rte_flow_item eth_item = { RTE_FLOW_ITEM_TYPE_ETH, 163 0, 0, 0 }; 164 static struct rte_flow_item end_item = { RTE_FLOW_ITEM_TYPE_END, 165 0, 0, 0 }; 166 167 /* sample actions: 168 * "actions count / end" 169 */ 170 struct rte_flow_query_count count = { 171 .reset = 1, 172 .hits_set = 1, 173 .bytes_set = 1, 174 .hits = 0, 175 .bytes = 0, 176 }; 177 static struct rte_flow_action count_action = { RTE_FLOW_ACTION_TYPE_COUNT, 178 &count}; 179 static struct rte_flow_action end_action = { RTE_FLOW_ACTION_TYPE_END, 0}; 180 static struct rte_flow_action actions[2]; 181 182 /* sample attributes */ 183 static struct rte_flow_attr attr; 184 185 /* flow_classify.c: * Based on DPDK skeleton forwarding example. */ 186 187 /* 188 * Initializes a given port using global settings and with the RX buffers 189 * coming from the mbuf_pool passed as a parameter. 190 */ 191 static inline int 192 port_init(uint8_t port, struct rte_mempool *mbuf_pool) 193 { 194 struct rte_eth_conf port_conf = port_conf_default; 195 struct rte_ether_addr addr; 196 const uint16_t rx_rings = 1, tx_rings = 1; 197 int retval; 198 uint16_t q; 199 struct rte_eth_dev_info dev_info; 200 struct rte_eth_txconf txconf; 201 202 if (!rte_eth_dev_is_valid_port(port)) 203 return -1; 204 205 retval = rte_eth_dev_info_get(port, &dev_info); 206 if (retval != 0) { 207 printf("Error during getting device (port %u) info: %s\n", 208 port, strerror(-retval)); 209 return retval; 210 } 211 212 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 213 port_conf.txmode.offloads |= 214 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 215 216 /* Configure the Ethernet device. */ 217 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); 218 if (retval != 0) 219 return retval; 220 221 /* Allocate and set up 1 RX queue per Ethernet port. */ 222 for (q = 0; q < rx_rings; q++) { 223 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE, 224 rte_eth_dev_socket_id(port), NULL, mbuf_pool); 225 if (retval < 0) 226 return retval; 227 } 228 229 txconf = dev_info.default_txconf; 230 txconf.offloads = port_conf.txmode.offloads; 231 /* Allocate and set up 1 TX queue per Ethernet port. */ 232 for (q = 0; q < tx_rings; q++) { 233 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE, 234 rte_eth_dev_socket_id(port), &txconf); 235 if (retval < 0) 236 return retval; 237 } 238 239 /* Start the Ethernet port. */ 240 retval = rte_eth_dev_start(port); 241 if (retval < 0) 242 return retval; 243 244 /* Display the port MAC address. */ 245 rte_eth_macaddr_get(port, &addr); 246 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8 247 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n", 248 port, 249 addr.addr_bytes[0], addr.addr_bytes[1], 250 addr.addr_bytes[2], addr.addr_bytes[3], 251 addr.addr_bytes[4], addr.addr_bytes[5]); 252 253 /* Enable RX in promiscuous mode for the Ethernet device. */ 254 rte_eth_promiscuous_enable(port); 255 256 return 0; 257 } 258 259 /* 260 * The lcore main. This is the main thread that does the work, reading from 261 * an input port classifying the packets and writing to an output port. 262 */ 263 static __attribute__((noreturn)) void 264 lcore_main(struct flow_classifier *cls_app) 265 { 266 uint16_t port; 267 int ret; 268 int i = 0; 269 270 ret = rte_flow_classify_table_entry_delete(cls_app->cls, 271 rules[7]); 272 if (ret) 273 printf("table_entry_delete failed [7] %d\n\n", ret); 274 else 275 printf("table_entry_delete succeeded [7]\n\n"); 276 277 /* 278 * Check that the port is on the same NUMA node as the polling thread 279 * for best performance. 280 */ 281 RTE_ETH_FOREACH_DEV(port) 282 if (rte_eth_dev_socket_id(port) > 0 && 283 rte_eth_dev_socket_id(port) != (int)rte_socket_id()) { 284 printf("\n\n"); 285 printf("WARNING: port %u is on remote NUMA node\n", 286 port); 287 printf("to polling thread.\n"); 288 printf("Performance will not be optimal.\n"); 289 } 290 printf("\nCore %u forwarding packets. ", rte_lcore_id()); 291 printf("[Ctrl+C to quit]\n"); 292 293 /* Run until the application is quit or killed. */ 294 for (;;) { 295 /* 296 * Receive packets on a port, classify them and forward them 297 * on the paired port. 298 * The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc. 299 */ 300 RTE_ETH_FOREACH_DEV(port) { 301 /* Get burst of RX packets, from first port of pair. */ 302 struct rte_mbuf *bufs[BURST_SIZE]; 303 const uint16_t nb_rx = rte_eth_rx_burst(port, 0, 304 bufs, BURST_SIZE); 305 306 if (unlikely(nb_rx == 0)) 307 continue; 308 309 for (i = 0; i < MAX_NUM_CLASSIFY; i++) { 310 if (rules[i]) { 311 ret = rte_flow_classifier_query( 312 cls_app->cls, 313 bufs, nb_rx, rules[i], 314 &classify_stats); 315 if (ret) 316 printf( 317 "rule [%d] query failed ret [%d]\n\n", 318 i, ret); 319 else { 320 printf( 321 "rule[%d] count=%"PRIu64"\n", 322 i, ntuple_stats.counter1); 323 324 printf("proto = %d\n", 325 ntuple_stats.ipv4_5tuple.proto); 326 } 327 } 328 } 329 330 /* Send burst of TX packets, to second port of pair. */ 331 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0, 332 bufs, nb_rx); 333 334 /* Free any unsent packets. */ 335 if (unlikely(nb_tx < nb_rx)) { 336 uint16_t buf; 337 338 for (buf = nb_tx; buf < nb_rx; buf++) 339 rte_pktmbuf_free(bufs[buf]); 340 } 341 } 342 } 343 } 344 345 /* 346 * Parse IPv4 5 tuple rules file, ipv4_rules_file.txt. 347 * Expected format: 348 * <src_ipv4_addr>'/'<masklen> <space> \ 349 * <dst_ipv4_addr>'/'<masklen> <space> \ 350 * <src_port> <space> ":" <src_port_mask> <space> \ 351 * <dst_port> <space> ":" <dst_port_mask> <space> \ 352 * <proto>'/'<proto_mask> <space> \ 353 * <priority> 354 */ 355 356 static int 357 get_cb_field(char **in, uint32_t *fd, int base, unsigned long lim, 358 char dlm) 359 { 360 unsigned long val; 361 char *end; 362 363 errno = 0; 364 val = strtoul(*in, &end, base); 365 if (errno != 0 || end[0] != dlm || val > lim) 366 return -EINVAL; 367 *fd = (uint32_t)val; 368 *in = end + 1; 369 return 0; 370 } 371 372 static int 373 parse_ipv4_net(char *in, uint32_t *addr, uint32_t *mask_len) 374 { 375 uint32_t a, b, c, d, m; 376 377 if (get_cb_field(&in, &a, 0, UINT8_MAX, '.')) 378 return -EINVAL; 379 if (get_cb_field(&in, &b, 0, UINT8_MAX, '.')) 380 return -EINVAL; 381 if (get_cb_field(&in, &c, 0, UINT8_MAX, '.')) 382 return -EINVAL; 383 if (get_cb_field(&in, &d, 0, UINT8_MAX, '/')) 384 return -EINVAL; 385 if (get_cb_field(&in, &m, 0, sizeof(uint32_t) * CHAR_BIT, 0)) 386 return -EINVAL; 387 388 addr[0] = RTE_IPV4(a, b, c, d); 389 mask_len[0] = m; 390 return 0; 391 } 392 393 static int 394 parse_ipv4_5tuple_rule(char *str, struct rte_eth_ntuple_filter *ntuple_filter) 395 { 396 int i, ret; 397 char *s, *sp, *in[CB_FLD_NUM]; 398 static const char *dlm = " \t\n"; 399 int dim = CB_FLD_NUM; 400 uint32_t temp; 401 402 s = str; 403 for (i = 0; i != dim; i++, s = NULL) { 404 in[i] = strtok_r(s, dlm, &sp); 405 if (in[i] == NULL) 406 return -EINVAL; 407 } 408 409 ret = parse_ipv4_net(in[CB_FLD_SRC_ADDR], 410 &ntuple_filter->src_ip, 411 &ntuple_filter->src_ip_mask); 412 if (ret != 0) { 413 flow_classify_log("failed to read source address/mask: %s\n", 414 in[CB_FLD_SRC_ADDR]); 415 return ret; 416 } 417 418 ret = parse_ipv4_net(in[CB_FLD_DST_ADDR], 419 &ntuple_filter->dst_ip, 420 &ntuple_filter->dst_ip_mask); 421 if (ret != 0) { 422 flow_classify_log("failed to read source address/mask: %s\n", 423 in[CB_FLD_DST_ADDR]); 424 return ret; 425 } 426 427 if (get_cb_field(&in[CB_FLD_SRC_PORT], &temp, 0, UINT16_MAX, 0)) 428 return -EINVAL; 429 ntuple_filter->src_port = (uint16_t)temp; 430 431 if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim, 432 sizeof(cb_port_delim)) != 0) 433 return -EINVAL; 434 435 if (get_cb_field(&in[CB_FLD_SRC_PORT_MASK], &temp, 0, UINT16_MAX, 0)) 436 return -EINVAL; 437 ntuple_filter->src_port_mask = (uint16_t)temp; 438 439 if (get_cb_field(&in[CB_FLD_DST_PORT], &temp, 0, UINT16_MAX, 0)) 440 return -EINVAL; 441 ntuple_filter->dst_port = (uint16_t)temp; 442 443 if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim, 444 sizeof(cb_port_delim)) != 0) 445 return -EINVAL; 446 447 if (get_cb_field(&in[CB_FLD_DST_PORT_MASK], &temp, 0, UINT16_MAX, 0)) 448 return -EINVAL; 449 ntuple_filter->dst_port_mask = (uint16_t)temp; 450 451 if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, '/')) 452 return -EINVAL; 453 ntuple_filter->proto = (uint8_t)temp; 454 455 if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, 0)) 456 return -EINVAL; 457 ntuple_filter->proto_mask = (uint8_t)temp; 458 459 if (get_cb_field(&in[CB_FLD_PRIORITY], &temp, 0, UINT16_MAX, 0)) 460 return -EINVAL; 461 ntuple_filter->priority = (uint16_t)temp; 462 if (ntuple_filter->priority > FLOW_CLASSIFY_MAX_PRIORITY) 463 ret = -EINVAL; 464 465 return ret; 466 } 467 468 /* Bypass comment and empty lines */ 469 static inline int 470 is_bypass_line(char *buff) 471 { 472 int i = 0; 473 474 /* comment line */ 475 if (buff[0] == COMMENT_LEAD_CHAR) 476 return 1; 477 /* empty line */ 478 while (buff[i] != '\0') { 479 if (!isspace(buff[i])) 480 return 0; 481 i++; 482 } 483 return 1; 484 } 485 486 static uint32_t 487 convert_depth_to_bitmask(uint32_t depth_val) 488 { 489 uint32_t bitmask = 0; 490 int i, j; 491 492 for (i = depth_val, j = 0; i > 0; i--, j++) 493 bitmask |= (1 << (31 - j)); 494 return bitmask; 495 } 496 497 static int 498 add_classify_rule(struct rte_eth_ntuple_filter *ntuple_filter, 499 struct flow_classifier *cls_app) 500 { 501 int ret = -1; 502 int key_found; 503 struct rte_flow_error error; 504 struct rte_flow_item_ipv4 ipv4_spec; 505 struct rte_flow_item_ipv4 ipv4_mask; 506 struct rte_flow_item ipv4_udp_item; 507 struct rte_flow_item ipv4_tcp_item; 508 struct rte_flow_item ipv4_sctp_item; 509 struct rte_flow_item_udp udp_spec; 510 struct rte_flow_item_udp udp_mask; 511 struct rte_flow_item udp_item; 512 struct rte_flow_item_tcp tcp_spec; 513 struct rte_flow_item_tcp tcp_mask; 514 struct rte_flow_item tcp_item; 515 struct rte_flow_item_sctp sctp_spec; 516 struct rte_flow_item_sctp sctp_mask; 517 struct rte_flow_item sctp_item; 518 struct rte_flow_item pattern_ipv4_5tuple[4]; 519 struct rte_flow_classify_rule *rule; 520 uint8_t ipv4_proto; 521 522 if (num_classify_rules >= MAX_NUM_CLASSIFY) { 523 printf( 524 "\nINFO: classify rule capacity %d reached\n", 525 num_classify_rules); 526 return ret; 527 } 528 529 /* set up parameters for validate and add */ 530 memset(&ipv4_spec, 0, sizeof(ipv4_spec)); 531 ipv4_spec.hdr.next_proto_id = ntuple_filter->proto; 532 ipv4_spec.hdr.src_addr = ntuple_filter->src_ip; 533 ipv4_spec.hdr.dst_addr = ntuple_filter->dst_ip; 534 ipv4_proto = ipv4_spec.hdr.next_proto_id; 535 536 memset(&ipv4_mask, 0, sizeof(ipv4_mask)); 537 ipv4_mask.hdr.next_proto_id = ntuple_filter->proto_mask; 538 ipv4_mask.hdr.src_addr = ntuple_filter->src_ip_mask; 539 ipv4_mask.hdr.src_addr = 540 convert_depth_to_bitmask(ipv4_mask.hdr.src_addr); 541 ipv4_mask.hdr.dst_addr = ntuple_filter->dst_ip_mask; 542 ipv4_mask.hdr.dst_addr = 543 convert_depth_to_bitmask(ipv4_mask.hdr.dst_addr); 544 545 switch (ipv4_proto) { 546 case IPPROTO_UDP: 547 ipv4_udp_item.type = RTE_FLOW_ITEM_TYPE_IPV4; 548 ipv4_udp_item.spec = &ipv4_spec; 549 ipv4_udp_item.mask = &ipv4_mask; 550 ipv4_udp_item.last = NULL; 551 552 udp_spec.hdr.src_port = ntuple_filter->src_port; 553 udp_spec.hdr.dst_port = ntuple_filter->dst_port; 554 udp_spec.hdr.dgram_len = 0; 555 udp_spec.hdr.dgram_cksum = 0; 556 557 udp_mask.hdr.src_port = ntuple_filter->src_port_mask; 558 udp_mask.hdr.dst_port = ntuple_filter->dst_port_mask; 559 udp_mask.hdr.dgram_len = 0; 560 udp_mask.hdr.dgram_cksum = 0; 561 562 udp_item.type = RTE_FLOW_ITEM_TYPE_UDP; 563 udp_item.spec = &udp_spec; 564 udp_item.mask = &udp_mask; 565 udp_item.last = NULL; 566 567 attr.priority = ntuple_filter->priority; 568 pattern_ipv4_5tuple[1] = ipv4_udp_item; 569 pattern_ipv4_5tuple[2] = udp_item; 570 break; 571 case IPPROTO_TCP: 572 ipv4_tcp_item.type = RTE_FLOW_ITEM_TYPE_IPV4; 573 ipv4_tcp_item.spec = &ipv4_spec; 574 ipv4_tcp_item.mask = &ipv4_mask; 575 ipv4_tcp_item.last = NULL; 576 577 memset(&tcp_spec, 0, sizeof(tcp_spec)); 578 tcp_spec.hdr.src_port = ntuple_filter->src_port; 579 tcp_spec.hdr.dst_port = ntuple_filter->dst_port; 580 581 memset(&tcp_mask, 0, sizeof(tcp_mask)); 582 tcp_mask.hdr.src_port = ntuple_filter->src_port_mask; 583 tcp_mask.hdr.dst_port = ntuple_filter->dst_port_mask; 584 585 tcp_item.type = RTE_FLOW_ITEM_TYPE_TCP; 586 tcp_item.spec = &tcp_spec; 587 tcp_item.mask = &tcp_mask; 588 tcp_item.last = NULL; 589 590 attr.priority = ntuple_filter->priority; 591 pattern_ipv4_5tuple[1] = ipv4_tcp_item; 592 pattern_ipv4_5tuple[2] = tcp_item; 593 break; 594 case IPPROTO_SCTP: 595 ipv4_sctp_item.type = RTE_FLOW_ITEM_TYPE_IPV4; 596 ipv4_sctp_item.spec = &ipv4_spec; 597 ipv4_sctp_item.mask = &ipv4_mask; 598 ipv4_sctp_item.last = NULL; 599 600 sctp_spec.hdr.src_port = ntuple_filter->src_port; 601 sctp_spec.hdr.dst_port = ntuple_filter->dst_port; 602 sctp_spec.hdr.cksum = 0; 603 sctp_spec.hdr.tag = 0; 604 605 sctp_mask.hdr.src_port = ntuple_filter->src_port_mask; 606 sctp_mask.hdr.dst_port = ntuple_filter->dst_port_mask; 607 sctp_mask.hdr.cksum = 0; 608 sctp_mask.hdr.tag = 0; 609 610 sctp_item.type = RTE_FLOW_ITEM_TYPE_SCTP; 611 sctp_item.spec = &sctp_spec; 612 sctp_item.mask = &sctp_mask; 613 sctp_item.last = NULL; 614 615 attr.priority = ntuple_filter->priority; 616 pattern_ipv4_5tuple[1] = ipv4_sctp_item; 617 pattern_ipv4_5tuple[2] = sctp_item; 618 break; 619 default: 620 return ret; 621 } 622 623 attr.ingress = 1; 624 pattern_ipv4_5tuple[0] = eth_item; 625 pattern_ipv4_5tuple[3] = end_item; 626 actions[0] = count_action; 627 actions[1] = end_action; 628 629 /* Validate and add rule */ 630 ret = rte_flow_classify_validate(cls_app->cls, &attr, 631 pattern_ipv4_5tuple, actions, &error); 632 if (ret) { 633 printf("table entry validate failed ipv4_proto = %u\n", 634 ipv4_proto); 635 return ret; 636 } 637 638 rule = rte_flow_classify_table_entry_add( 639 cls_app->cls, &attr, pattern_ipv4_5tuple, 640 actions, &key_found, &error); 641 if (rule == NULL) { 642 printf("table entry add failed ipv4_proto = %u\n", 643 ipv4_proto); 644 ret = -1; 645 return ret; 646 } 647 648 rules[num_classify_rules] = rule; 649 num_classify_rules++; 650 return 0; 651 } 652 653 static int 654 add_rules(const char *rule_path, struct flow_classifier *cls_app) 655 { 656 FILE *fh; 657 char buff[LINE_MAX]; 658 unsigned int i = 0; 659 unsigned int total_num = 0; 660 struct rte_eth_ntuple_filter ntuple_filter; 661 int ret; 662 663 fh = fopen(rule_path, "rb"); 664 if (fh == NULL) 665 rte_exit(EXIT_FAILURE, "%s: fopen %s failed\n", __func__, 666 rule_path); 667 668 ret = fseek(fh, 0, SEEK_SET); 669 if (ret) 670 rte_exit(EXIT_FAILURE, "%s: fseek %d failed\n", __func__, 671 ret); 672 673 i = 0; 674 while (fgets(buff, LINE_MAX, fh) != NULL) { 675 i++; 676 677 if (is_bypass_line(buff)) 678 continue; 679 680 if (total_num >= FLOW_CLASSIFY_MAX_RULE_NUM - 1) { 681 printf("\nINFO: classify rule capacity %d reached\n", 682 total_num); 683 break; 684 } 685 686 if (parse_ipv4_5tuple_rule(buff, &ntuple_filter) != 0) 687 rte_exit(EXIT_FAILURE, 688 "%s Line %u: parse rules error\n", 689 rule_path, i); 690 691 if (add_classify_rule(&ntuple_filter, cls_app) != 0) 692 rte_exit(EXIT_FAILURE, "add rule error\n"); 693 694 total_num++; 695 } 696 697 fclose(fh); 698 return 0; 699 } 700 701 /* display usage */ 702 static void 703 print_usage(const char *prgname) 704 { 705 printf("%s usage:\n", prgname); 706 printf("[EAL options] -- --"OPTION_RULE_IPV4"=FILE: "); 707 printf("specify the ipv4 rules file.\n"); 708 printf("Each rule occupies one line in the file.\n"); 709 } 710 711 /* Parse the argument given in the command line of the application */ 712 static int 713 parse_args(int argc, char **argv) 714 { 715 int opt, ret; 716 char **argvopt; 717 int option_index; 718 char *prgname = argv[0]; 719 static struct option lgopts[] = { 720 {OPTION_RULE_IPV4, 1, 0, 0}, 721 {NULL, 0, 0, 0} 722 }; 723 724 argvopt = argv; 725 726 while ((opt = getopt_long(argc, argvopt, "", 727 lgopts, &option_index)) != EOF) { 728 729 switch (opt) { 730 /* long options */ 731 case 0: 732 if (!strncmp(lgopts[option_index].name, 733 OPTION_RULE_IPV4, 734 sizeof(OPTION_RULE_IPV4))) 735 parm_config.rule_ipv4_name = optarg; 736 break; 737 default: 738 print_usage(prgname); 739 return -1; 740 } 741 } 742 743 if (optind >= 0) 744 argv[optind-1] = prgname; 745 746 ret = optind-1; 747 optind = 1; /* reset getopt lib */ 748 return ret; 749 } 750 751 /* 752 * The main function, which does initialization and calls the lcore_main 753 * function. 754 */ 755 int 756 main(int argc, char *argv[]) 757 { 758 struct rte_mempool *mbuf_pool; 759 uint16_t nb_ports; 760 uint16_t portid; 761 int ret; 762 int socket_id; 763 struct rte_table_acl_params table_acl_params; 764 struct rte_flow_classify_table_params cls_table_params; 765 struct flow_classifier *cls_app; 766 struct rte_flow_classifier_params cls_params; 767 uint32_t size; 768 769 /* Initialize the Environment Abstraction Layer (EAL). */ 770 ret = rte_eal_init(argc, argv); 771 if (ret < 0) 772 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 773 774 argc -= ret; 775 argv += ret; 776 777 /* parse application arguments (after the EAL ones) */ 778 ret = parse_args(argc, argv); 779 if (ret < 0) 780 rte_exit(EXIT_FAILURE, "Invalid flow_classify parameters\n"); 781 782 /* Check that there is an even number of ports to send/receive on. */ 783 nb_ports = rte_eth_dev_count_avail(); 784 if (nb_ports < 2 || (nb_ports & 1)) 785 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n"); 786 787 /* Creates a new mempool in memory to hold the mbufs. */ 788 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports, 789 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 790 791 if (mbuf_pool == NULL) 792 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 793 794 /* Initialize all ports. */ 795 RTE_ETH_FOREACH_DEV(portid) 796 if (port_init(portid, mbuf_pool) != 0) 797 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n", 798 portid); 799 800 if (rte_lcore_count() > 1) 801 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n"); 802 803 socket_id = rte_eth_dev_socket_id(0); 804 805 /* Memory allocation */ 806 size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl)); 807 cls_app = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE); 808 if (cls_app == NULL) 809 rte_exit(EXIT_FAILURE, "Cannot allocate classifier memory\n"); 810 811 cls_params.name = "flow_classifier"; 812 cls_params.socket_id = socket_id; 813 814 cls_app->cls = rte_flow_classifier_create(&cls_params); 815 if (cls_app->cls == NULL) { 816 rte_free(cls_app); 817 rte_exit(EXIT_FAILURE, "Cannot create classifier\n"); 818 } 819 820 /* initialise ACL table params */ 821 table_acl_params.name = "table_acl_ipv4_5tuple"; 822 table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM; 823 table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs); 824 memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs)); 825 826 /* initialise table create params */ 827 cls_table_params.ops = &rte_table_acl_ops; 828 cls_table_params.arg_create = &table_acl_params; 829 cls_table_params.type = RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE; 830 831 ret = rte_flow_classify_table_create(cls_app->cls, &cls_table_params); 832 if (ret) { 833 rte_flow_classifier_free(cls_app->cls); 834 rte_free(cls_app); 835 rte_exit(EXIT_FAILURE, "Failed to create classifier table\n"); 836 } 837 838 /* read file of IPv4 5 tuple rules and initialize parameters 839 * for rte_flow_classify_validate and rte_flow_classify_table_entry_add 840 * API's. 841 */ 842 if (add_rules(parm_config.rule_ipv4_name, cls_app)) { 843 rte_flow_classifier_free(cls_app->cls); 844 rte_free(cls_app); 845 rte_exit(EXIT_FAILURE, "Failed to add rules\n"); 846 } 847 848 /* Call lcore_main on the master core only. */ 849 lcore_main(cls_app); 850 851 return 0; 852 } 853