1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
| diff --git a/config.mk b/config.mk index 91673dc93aff8fc47bcde6f275c439b44c5b8ef8..ff98d5c997a9207feb4e73e3e9ad899e9f28f090 100644 --- a/config.mk +++ b/config.mk @@ -10,7 +10,7 @@ export CONFIG_PDUMP=y export CONFIG_ICMP_REDIRECT_CORE=n
# debugging and logging -export CONFIG_DEBUG=n +export CONFIG_DEBUG=y export CONFIG_DPVS_NEIGH_DEBUG=n export CONFIG_RECORD_BIG_LOOP=n export CONFIG_DPVS_SAPOOL_DEBUG=n diff --git a/include/conf/cpu_fdir.h b/include/conf/cpu_fdir.h new file mode 100644 index 0000000000000000000000000000000000000000..2bd26dd5880aa740c1290b3f095b02bf6761f890 --- /dev/null +++ b/include/conf/cpu_fdir.h @@ -0,0 +1,22 @@ +#ifndef __CPU_FDIR_CONF_H__ +#define __CPU_FDIR_CONF_H__ + +#include "conf/sockopts.h" + +struct cpu_fdir_conf { + uint8_t proto; + uint8_t cid; + + uint32_t daddr; + uint8_t d_plen; + uint16_t dport_from; + uint16_t dport_to; +}__attribute__((__packed__)); + + +struct cpu_fdir_conf_array { + int nrule; + struct cpu_fdir_conf rules[0]; +} __attribute__((__packed__)); + +#endif \ No newline at end of file diff --git a/include/conf/sockopts.h b/include/conf/sockopts.h index 2e3cd75950c4939a3c1e3f143fcfd26306dcfcbb..9e0bf0d764c973341c24a04ac6f5858ce094efc8 100644 --- a/include/conf/sockopts.h +++ b/include/conf/sockopts.h @@ -149,6 +149,10 @@ DPVSMSG(SOCKOPT_SET_IFTRAF_ADD) \ DPVSMSG(SOCKOPT_SET_IFTRAF_DEL) \ DPVSMSG(SOCKOPT_GET_IFTRAF_SHOW)\ + \ + DPVSMSG(SOCKOPT_SET_FDIR_ADD) \ + DPVSMSG(SOCKOPT_SET_FDIR_DEL) \ + DPVSMSG(SOCKOPT_GET_FDIR_SHOW) \
typedef enum { DPVSMSG_SOCKOPT_ENUM(ENUM_ITEM) diff --git a/include/fdir/cpu_fdir.h b/include/fdir/cpu_fdir.h new file mode 100644 index 0000000000000000000000000000000000000000..71f780efe6910b5d91f43e5cb2fdb491dda4966d --- /dev/null +++ b/include/fdir/cpu_fdir.h @@ -0,0 +1,29 @@ +#ifndef __CPU_FDIR_H__ +#define __CPU_FDIR_H__ +#include "conf/common.h" +#include "netif.h" +#include "list.h" +#include "dpdk.h" + +struct cpu_fdir { + struct list_head list; + + uint8_t proto; + lcoreid_t cid; + + + rte_be32_t daddr; + rte_be32_t daddr_mask; + uint16_t dport_from; + uint16_t dport_to; +} __rte_cache_aligned; + +struct cpu_fdir *cpu_fdir_alloc(void); +struct cpu_fdir *cpu_fdir_get(uint8_t proto, rte_be32_t saddr, rte_be32_t daddr, + uint16_t sport, uint16_t dport); +int cpu_fdir_pkt(struct rte_mbuf *mbuf, lcoreid_t peer_cid); +void cpu_fdir_ring_proc(lcoreid_t cid); +int cpu_fdir_redirect(struct rte_mbuf *mbuf, int *redirected); +int cpu_fdir_init(void); +int cpu_fdir_term(void); +#endif \ No newline at end of file diff --git a/include/fdir/parser.h b/include/fdir/parser.h new file mode 100644 index 0000000000000000000000000000000000000000..f793d952293ee8ca5790c90c509d8464d8a8155a --- /dev/null +++ b/include/fdir/parser.h @@ -0,0 +1,97 @@ +#ifndef __FDIR_PARSER_H__ +#define __FDIR_PARSER_H__ +#include "conf/common.h" +#include <rte_mbuf.h> +#include "netif.h" +#include "dpdk.h" +#include "rte_geneve.h" +#include "rte_vxlan.h" + +enum { + TUNNEL_NONE, + TUNNEL_VXLAN, + TUNNEL_GENEVE, +}; + +#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +#define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8))) +#else +#define _htons(x) (x) +#endif + + +typedef struct sk_buff_info { + struct rte_mbuf* mbuf; +
+ union { + struct rte_ether_hdr *outer_eth; + void *outer_l2_hdr; + }; + + union { + struct rte_ipv4_hdr *outer_iph; + struct rte_arp_hdr *outer_arph; + void *outer_l3_hdr; + }; + + union { + struct rte_tcp_hdr *outer_tcph; + struct rte_udp_hdr *outer_udph; + struct rte_icmp_hdr *outer_icmph; + void *outer_l4_hdr; + }; + union { + struct rte_ether_hdr *eth; + void *l2_hdr; + }; + + union { + struct rte_ipv6_hdr *ip6h; + struct rte_ipv4_hdr *iph; + struct rte_arp_hdr *arph; + void *l3_hdr; + }; + + union { + struct rte_tcp_hdr *tcph; + struct rte_udp_hdr *udph; + struct rte_icmp_hdr *icmph; + void *l4_hdr; + }; + + union { + struct rte_vxlan_hdr *vxlanh; + struct rte_geneve_hdr *genh; + void *tunnel_hdr; + }; + + uint32_t recv_vni; + uint8_t vni_flag; + + uint16_t outer_l2_len; + uint16_t outer_l3_len; + uint16_t outer_l4_len; + + uint8_t tunnel_type; + uint8_t tunnel_flag; + uint16_t tunnel_len; + + uint16_t l2_len; + uint16_t l3_len; + uint16_t l4_len; + + uint8_t outer_ethertype; + uint8_t ethertype; + uint8_t outer_l4_proto; + uint8_t l4_proto; + uint8_t is_tunnel; +} pkt_info_t __rte_cache_aligned; + + +void pkt_info_init(pkt_info_t* pkt_info, struct rte_mbuf* mbuf); + +#endif \ No newline at end of file diff --git a/src/fdir/cpu_fdir.c b/src/fdir/cpu_fdir.c new file mode 100644 index 0000000000000000000000000000000000000000..3aef18a13dccb652210b8bad8de96b1c4e32c0c0 --- /dev/null +++ b/src/fdir/cpu_fdir.c @@ -0,0 +1,468 @@ +#include "fdir/cpu_fdir.h" +#include "fdir/parser.h" +#include "conf/cpu_fdir.h" +#include "ctrl.h" + + +#define CPU_FDIR_RING_SIZE 2048 + +#define CPU_FDIR_TBL_BITS 10 +#define CPU_FDIR_TBL_SIZE (1 << CPU_FDIR_TBL_BITS) +#define CPU_FDIR_TBL_MASK (CPU_FDIR_TBL_SIZE - 1) + +static struct list_head *g_cpu_fdir_tbl; +static rte_spinlock_t cpu_fdir_tbl_lock[DPVS_MAX_SOCKET]; +static struct rte_mempool *g_cpu_fdir_cache[DPVS_MAX_SOCKET]; +static rte_atomic32_t num_fdir_rules[DPVS_MAX_SOCKET]; +#define this_cpu_fdir_cache (g_cpu_fdir_cache[rte_socket_id()]) +#define this_num_fdir_rules (num_fdir_rules[rte_socket_id()]) + + +static struct rte_ring *cpu_fdir_ring[DPVS_MAX_LCORE][DPVS_MAX_LCORE]; +bool cpu_fdir_enable = true; + +#define CPU_FDIR +#define RTE_LOGTYPE_CPU_FDIR RTE_LOGTYPE_USER1 + + +#ifdef CONFIG_DPVS_IPVS_DEBUG +static inline void +cpu_fdir_show(struct cpu_fdir *r, const char *action) +{ + char sbuf[64], dbuf[64]; + char sbufm[64], dbufm[64]; + + RTE_LOG(DEBUG, CPU_FDIR, "[%d] redirect %s: [%d] %s %s/%s:%d-%d -> %s/%s:%d-%d\n", + rte_lcore_id(), action, r->cid, + inet_proto_name(r->proto), + inet_ntop(r->af, &r->saddr, sbuf, sizeof(sbuf)) ? sbuf : "::", + inet_ntop(r->af, &r->saddr_mask, sbufm, sizeof(sbufm)) ? sbufm : "::", + ntohs(r->sport_from), + ntohs(r->sport_to), + inet_ntop(r->af, &r->daddr, dbuf, sizeof(dbuf)) ? dbuf : "::", + inet_ntop(r->af, &r->daddr_mask, dbufm, sizeof(dbufm)) ? dbufm : "::", + ntohs(r->dport_from), + ntohs(r->dport_to)); +} +#endif + +
+struct cpu_fdir * +cpu_fdir_get(uint8_t proto, rte_be32_t saddr, rte_be32_t daddr, + uint16_t sport, uint16_t dport) +{ + struct cpu_fdir *r; + + if (!cpu_fdir_enable) { + return EDPVS_OK; + } + + + list_for_each_entry(r, &g_cpu_fdir_tbl[rte_socket_id()], list) { + if ( r->proto == proto + && (dport >= r->dport_from && dport <= r->dport_to) + && ((r->daddr_mask & daddr) == r->daddr)) { + goto found; + } + } + + return NULL; + +found: +#ifdef CONFIG_DPVS_IPVS_DEBUG + cpu_fdir_show(r, "get"); +#endif + return r; +} + +
+static int cpu_fdir_redirect_pkt(struct rte_mbuf *mbuf, lcoreid_t peer_cid) +{ + lcoreid_t cid = rte_lcore_id(); + int ret; + + ret = rte_ring_enqueue(cpu_fdir_ring[peer_cid][cid], mbuf); + if (ret < 0) { + RTE_LOG(ERR, CPU_FDIR, + "%s: [%d] failed to enqueue mbuf to cpu_fdir_ring[%d][%d]\n", + __func__, cid, peer_cid, cid); + return INET_DROP; + } + +#ifdef CONFIG_DPVS_IPVS_DEBUG + RTE_LOG(DEBUG, CPU_FDIR, + "%s: [%d] enqueued mbuf to cpu_fdir_ring[%d][%d]\n", + __func__, cid, peer_cid, cid); +#endif + + return INET_STOLEN; +} + + + +int cpu_fdir_redirect(struct rte_mbuf *mbuf, int *redirected) { + + pkt_info_t pkt_info; + pkt_info_init(&pkt_info, mbuf); + + + struct rte_ipv4_hdr *iph = pkt_info.iph; + if (!iph) { + return EDPVS_INVPKT; + } + + uint16_t sport, dport = 0; + + switch (pkt_info.l4_proto) + { + case IPPROTO_TCP: + sport = pkt_info.tcph->src_port; + dport = pkt_info.tcph->dst_port; + break; + case IPPROTO_UDP: + sport = pkt_info.udph->src_port; + dport = pkt_info.udph->dst_port; + break; + case IPPROTO_ICMP: + sport = 0; + dport = 0; + default: + return EDPVS_INVPKT; + } + + struct cpu_fdir *r = cpu_fdir_get(pkt_info.l4_proto, + iph->src_addr, iph->dst_addr, sport, dport); + if (r) { + cpu_fdir_redirect_pkt(mbuf, r->cid); + *redirected = 1; + } + + return EDPVS_OK; +} + + +static int cpu_fdir_rule_add(rte_be32_t dst, rte_be32_t d_mask, + rte_be16_t dport_from, rte_be16_t dport_to, + uint8_t proto, uint8_t cid) +{ + int i = 0; + struct cpu_fdir *r; + + for (i = 0; i < get_numa_nodes(); i++) { + if (unlikely(rte_mempool_get(g_cpu_fdir_cache[i], (void **)&r) != 0)) { + RTE_LOG(WARNING, CPU_FDIR, + "%s: no memory for cpu fdir add\n", __func__); + return EDPVS_NOMEM; + } + + memset(r, 0, sizeof(struct cpu_fdir)); + r->daddr = dst; + r->daddr_mask = d_mask; + r->dport_from = dport_from; + r->dport_to = dport_to; + r->proto = proto; + r->cid = cid; + + rte_spinlock_lock(&cpu_fdir_tbl_lock[i]); + list_add(&r->list, &g_cpu_fdir_tbl[i]); + rte_atomic32_inc(&num_fdir_rules[i]); + rte_spinlock_unlock(&cpu_fdir_tbl_lock[i]); + } + + return EDPVS_OK; +} + +static int cpu_fdir_rule_del(rte_be32_t dst, rte_be32_t d_mask, + rte_be16_t dport_from, rte_be16_t dport_to, + uint8_t proto) +{ + int i = 0; + struct cpu_fdir *r, *r_next; + + for (i = 0; i < get_numa_nodes(); i++) { + rte_spinlock_lock(&cpu_fdir_tbl_lock[i]); + + + list_for_each_entry_safe(r, r_next, &g_cpu_fdir_tbl[rte_socket_id()], list) { + if ( r->proto == proto + && (dport_from == r->dport_from) + && (dport_to == r->dport_to) + && (dst == r->daddr) + && (d_mask == r->daddr_mask)) { + list_del(&r->list); + rte_atomic32_dec(&num_fdir_rules[i]); + } + } + + rte_spinlock_unlock(&cpu_fdir_tbl_lock[i]); + } + return EDPVS_OK; +} + +void cpu_fdir_ring_proc(lcoreid_t cid) +{ + struct rte_mbuf *mbufs[NETIF_MAX_PKT_BURST]; + uint16_t nb_rb; + lcoreid_t peer_cid; + + if (!cpu_fdir_enable) { + return; + } + + cid = rte_lcore_id(); + + for (peer_cid = 0; peer_cid < DPVS_MAX_LCORE; peer_cid++) { + if (cpu_fdir_ring[cid][peer_cid]) { + nb_rb = rte_ring_dequeue_burst(cpu_fdir_ring[cid][peer_cid], + (void**)mbufs, + NETIF_MAX_PKT_BURST, NULL); + if (nb_rb > 0) { + lcore_process_packets(mbufs, cid, nb_rb, 1); + } + } + } +} + + +static int cpu_fdir_ring_create(void) +{ + char name_buf[RTE_RING_NAMESIZE]; + int socket_id; + lcoreid_t cid, peer_cid; + + socket_id = rte_socket_id(); + + for (cid = 0; cid < DPVS_MAX_LCORE; cid++) { + if (!netif_lcore_is_fwd_worker(cid)) { + continue; + } + + for (peer_cid = 0; peer_cid < DPVS_MAX_LCORE; peer_cid++) { + if (!netif_lcore_is_fwd_worker(peer_cid) + || cid == peer_cid) { + continue; + } + + snprintf(name_buf, RTE_RING_NAMESIZE, + "cpu_fdir_ring[%d[%d]", cid, peer_cid); + + cpu_fdir_ring[cid][peer_cid] = + rte_ring_create(name_buf, CPU_FDIR_RING_SIZE, socket_id, + RING_F_SP_ENQ | RING_F_SC_DEQ); + + if (!cpu_fdir_ring[cid][peer_cid]) { + RTE_LOG(ERR, CPU_FDIR, + "%s: failed to create cpu_fdir_ring[%d][%d]\n", + __func__, cid, peer_cid); + return EDPVS_NOMEM; + } + } + } + + return EDPVS_OK; +} + +static void cpu_fdir_ring_free(void) +{ + lcoreid_t cid, peer_cid; + + for (cid = 0; cid < DPVS_MAX_LCORE; cid++) { + for (peer_cid = 0; peer_cid < DPVS_MAX_LCORE; peer_cid++) { + rte_ring_free(cpu_fdir_ring[cid][peer_cid]); + } + } +} + + +static int cpu_fdir_table_cache_alloc(void) +{ + int i; + char pool_name[32]; + + for (i = 0; i < get_numa_nodes(); i++) { + snprintf(pool_name, sizeof(pool_name), "cpu_fdir_%d", i); + g_cpu_fdir_cache[i] = rte_mempool_create(pool_name, + CPU_FDIR_TBL_SIZE, + sizeof(struct cpu_fdir), + 256, 0, NULL, NULL, NULL, NULL, + i, 0); + + if (!g_cpu_fdir_cache[i]) { + return EDPVS_NOMEM; + } + } + + return EDPVS_OK; +} + +static void cpu_fdir_table_cache_free(void) +{ + int i; + + for (i = 0; i < get_numa_nodes(); i++) { + rte_mempool_free(g_cpu_fdir_cache[i]); + } +} + +static int cpu_fdir_table_create(void) +{ + int i; + + if (cpu_fdir_table_cache_alloc() != EDPVS_OK) { + goto cache_free; + } + + + g_cpu_fdir_tbl = + rte_malloc(NULL, sizeof(struct list_head ), + RTE_CACHE_LINE_SIZE); + if (!g_cpu_fdir_tbl) { + goto cache_free; + } + + + for (i = 0; i < get_numa_nodes(); i++) { + INIT_LIST_HEAD(&g_cpu_fdir_tbl[i]); + rte_spinlock_init(&cpu_fdir_tbl_lock[i]); + } + + return EDPVS_OK; + +cache_free: + cpu_fdir_table_cache_free(); + return EDPVS_NOMEM; +} + +static void cpu_fdir_table_free(void) +{ + cpu_fdir_table_cache_free(); + + if (g_cpu_fdir_tbl) { + rte_free(g_cpu_fdir_tbl); + } +} + + +static int fdir_sockopt_set(sockoptid_t opt, const void *cf, size_t size) +{ + if (!cpu_fdir_enable) { + return EDPVS_NOTSUPP; + } + + struct cpu_fdir_conf *conf = (struct cpu_fdir_conf*)cf; + + if (!cf || size != sizeof(*conf)) + return EDPVS_INVAL; + + + uint32_t daddr_mask = htonl(0xFFFFFFFF << (32 - conf->d_plen)); + + switch (opt) { + case SOCKOPT_SET_FDIR_ADD: + return cpu_fdir_rule_add(conf->daddr, daddr_mask, + htons(conf->dport_from), htons(conf->dport_to), + conf->proto, conf->cid); + case SOCKOPT_SET_FDIR_DEL: + return cpu_fdir_rule_del(conf->daddr, daddr_mask, + htons(conf->dport_from), htons(conf->dport_to), + conf->proto); + default: + return EDPVS_NOTSUPP; + } + + return EDPVS_NOTSUPP; +} + +static int fdir_sockopt_get(sockoptid_t opt, const void *conf, size_t size, + void **out, size_t *outsize) +{ + if (!cpu_fdir_enable) { + return EDPVS_NOTSUPP; + } + + struct cpu_fdir_conf_array *array; + size_t nrule; + struct cpu_fdir *r; + int off = 0; + + nrule = rte_atomic32_read(&this_num_fdir_rules); + *outsize = sizeof(struct cpu_fdir_conf_array) + nrule * sizeof(struct cpu_fdir_conf); + *out = rte_calloc(NULL, 1, *outsize, 0); + if (!(*out)) + return EDPVS_NOMEM; + array = (struct cpu_fdir_conf_array*)*out; + + list_for_each_entry(r, &g_cpu_fdir_tbl[rte_socket_id()], list) { + if (off >= nrule) { + break; + } + memset(&array->rules[off], 0, sizeof(struct cpu_fdir_conf)); + array->rules[off].proto = r->proto; + array->rules[off].cid = r->cid; + array->rules[off].daddr = r->daddr; + array->rules[off].d_plen = 32 - __builtin_clz(r->daddr_mask); + array->rules[off].dport_from = r->dport_from; + array->rules[off].dport_to = r->dport_to; + off++; + } + array->nrule = off; + return EDPVS_OK; +} + + +static struct dpvs_sockopts cpu_fdir_sockopts = { + .version = SOCKOPT_VERSION, + .set_opt_min = SOCKOPT_SET_FDIR_ADD, + .set_opt_max = SOCKOPT_SET_FDIR_DEL, + .set = fdir_sockopt_set, + .get_opt_min = SOCKOPT_GET_FDIR_SHOW, + .get_opt_max = SOCKOPT_GET_FDIR_SHOW, + .get = fdir_sockopt_get, +}; + +int cpu_fdir_init(void) +{ + int err; + + if (!cpu_fdir_enable) { + return EDPVS_OK; + } + + err = cpu_fdir_ring_create(); + if (err != EDPVS_OK) { + return err; + } + + if ((err = sockopt_register(&cpu_fdir_sockopts)) != EDPVS_OK) { + return err; + } + + + return cpu_fdir_table_create(); +} + +int cpu_fdir_term(void) +{ + int err; + if (!cpu_fdir_enable) { + return EDPVS_OK; + } + + if ((err = sockopt_unregister(&cpu_fdir_sockopts)) != EDPVS_OK) { + return err; + } + + cpu_fdir_ring_free(); + cpu_fdir_table_free(); + + return EDPVS_OK; +} + diff --git a/src/fdir/parser.c b/src/fdir/parser.c new file mode 100644 index 0000000000000000000000000000000000000000..8dc136ed2c5fed27d4aba1010acc0f79703ad92a --- /dev/null +++ b/src/fdir/parser.c @@ -0,0 +1,156 @@ +#include "fdir/parser.h" + + +static void +parse_ipv4(struct sk_buff_info* this_sk_info) +{ + struct rte_ipv4_hdr *ipv4_hdr = this_sk_info->iph; + this_sk_info->l3_len = rte_ipv4_hdr_len(ipv4_hdr); + this_sk_info->l4_proto = ipv4_hdr->next_proto_id; + if (this_sk_info->l4_proto == IPPROTO_TCP) { + this_sk_info->tcph = (struct rte_tcp_hdr *) + ((char *)ipv4_hdr + this_sk_info->l3_len); + this_sk_info->l4_len = (this_sk_info->tcph->data_off & 0xf0) >> 2; + } else if (this_sk_info->l4_proto == IPPROTO_UDP) { + this_sk_info->udph = (struct rte_udp_hdr *) + ((char *)ipv4_hdr + this_sk_info->l3_len); + this_sk_info->l4_len = sizeof(struct rte_udp_hdr); + } else if (this_sk_info->l4_proto == IPPROTO_ICMP) { + this_sk_info->icmph = (struct rte_icmp_hdr*) + ((char *)ipv4_hdr + this_sk_info->l3_len); + } else + this_sk_info->l4_len = 0; +} + +static void +update_tunnel_outer(struct sk_buff_info* this_sk_info) +{ + this_sk_info->is_tunnel = 1; + this_sk_info->outer_ethertype = this_sk_info->ethertype; + this_sk_info->outer_l2_len = this_sk_info->l2_len; + this_sk_info->outer_l3_len = this_sk_info->l3_len; + this_sk_info->outer_l4_proto = this_sk_info->l4_proto; + this_sk_info->outer_l2_hdr = this_sk_info->l2_hdr; + this_sk_info->outer_l3_hdr = this_sk_info->l3_hdr; + this_sk_info->outer_l4_hdr = this_sk_info->l4_hdr; +} + +
+static void +parse_ethernet(struct sk_buff_info* this_sk_info) +{ + struct rte_ether_hdr *eth_hdr = this_sk_info->eth; + struct rte_vlan_hdr *vlan_hdr; + this_sk_info->l2_len = sizeof(struct rte_ether_hdr); + this_sk_info->ethertype = this_sk_info->eth->ether_type; + while (this_sk_info->ethertype == _htons(RTE_ETHER_TYPE_VLAN) || + this_sk_info->ethertype == _htons(RTE_ETHER_TYPE_QINQ)) { + vlan_hdr = (struct rte_vlan_hdr *) + ((char *)eth_hdr + this_sk_info->l2_len); + this_sk_info->l2_len += sizeof(struct rte_vlan_hdr); + this_sk_info->ethertype = vlan_hdr->eth_proto; + } + + switch (this_sk_info->ethertype) { + case _htons(RTE_ETHER_TYPE_IPV4): + this_sk_info->iph = (struct rte_ipv4_hdr *) + ((char *)this_sk_info->eth + this_sk_info->l2_len); + parse_ipv4(this_sk_info); + break; + default: + this_sk_info->l4_len = 0; + this_sk_info->l3_len = 0; + this_sk_info->l4_proto = 0; + break; + } +} + + +static void +parse_geneve(struct sk_buff_info* this_sk_info) +{ + struct rte_geneve_hdr *genh; + if (this_sk_info->udph->dst_port != _htons(RTE_GENEVE_DEFAULT_PORT)) { + return; + } + + genh = (struct rte_geneve_hdr *)((char *)this_sk_info->udph + + sizeof(struct rte_udp_hdr)); + this_sk_info->genh = genh; + this_sk_info->tunnel_type = TUNNEL_GENEVE; + this_sk_info->tunnel_len = sizeof(struct rte_geneve_hdr) + this_sk_info->genh->opt_len * 4; + if (!this_sk_info->genh->proto || this_sk_info->genh->proto == + _htons(RTE_ETHER_TYPE_IPV4)) { + update_tunnel_outer(this_sk_info); + this_sk_info->iph = (struct rte_ipv4_hdr *)((char *)this_sk_info->genh + + this_sk_info->tunnel_len); + parse_ipv4(this_sk_info); + this_sk_info->ethertype = _htons(RTE_ETHER_TYPE_IPV4); + this_sk_info->l2_len = 0; + } else if (this_sk_info->genh->proto == _htons(RTE_GENEVE_TYPE_ETH)) { + update_tunnel_outer(this_sk_info); + this_sk_info->eth = (struct rte_ether_hdr *)((char *)this_sk_info->genh + + this_sk_info->tunnel_len); + parse_ethernet(this_sk_info); + } else { + return; + } + + this_sk_info->recv_vni = genh->vni[0]<<16 | genh->vni[1]<<8 | genh->vni[2]; + this_sk_info->vni_flag = genh->reserved1 & 0x3f; + + this_sk_info->l2_len += this_sk_info->tunnel_len + sizeof(struct rte_udp_hdr); +} + + +static void +parse_vxlan(struct sk_buff_info* this_sk_info) +{ +
+ if (this_sk_info->udph->dst_port != _htons(RTE_VXLAN_DEFAULT_PORT) && + RTE_ETH_IS_TUNNEL_PKT(this_sk_info->mbuf->packet_type) == 0) { + return; + } + + update_tunnel_outer(this_sk_info); + this_sk_info->tunnel_type = TUNNEL_VXLAN; + this_sk_info->tunnel_len = RTE_ETHER_VXLAN_HLEN; + this_sk_info->vxlanh = (struct rte_vxlan_hdr *)((char *)this_sk_info->udph + + sizeof(struct rte_udp_hdr)); + this_sk_info->eth = (struct rte_ether_hdr *)((char *)this_sk_info->vxlanh + + sizeof(struct rte_vxlan_hdr)); + parse_ethernet(this_sk_info); + this_sk_info->recv_vni = rte_be_to_cpu_32(this_sk_info->vxlanh->vx_vni); + this_sk_info->vni_flag = this_sk_info->vxlanh->vx_flags; + this_sk_info->l2_len += this_sk_info->tunnel_len; +} + +void pkt_info_init(pkt_info_t* pkt_info, struct rte_mbuf* mbuf) +{ + memset(pkt_info, 0, sizeof(struct sk_buff_info)); + pkt_info->mbuf = mbuf; + pkt_info->l2_hdr = rte_pktmbuf_mtod(pkt_info->mbuf, struct rte_ether_hdr*); + parse_ethernet(pkt_info); + + + if (pkt_info->l4_proto == IPPROTO_UDP) { + parse_vxlan(pkt_info); + if (pkt_info->is_tunnel) { + goto tunnel_ok; + } + parse_geneve(pkt_info); + if (pkt_info->is_tunnel) { + goto tunnel_ok; + } + } + + tunnel_ok: + return; +} diff --git a/src/main.c b/src/main.c index 00ca8965593eaa41818d3158b3c670412505ff4e..93f49638213b7eb5341092c522d8a5cf2dc88daa 100644 --- a/src/main.c +++ b/src/main.c @@ -49,6 +49,7 @@ #include "eal_mem.h" #include "scheduler.h" #include "pdump.h" +#include "fdir/cpu_fdir.h"
#define DPVS "dpvs" #define RTE_LOGTYPE_DPVS RTE_LOGTYPE_USER1 @@ -107,6 +108,8 @@ static void inline dpdk_version_check(void) iftraf_init, iftraf_term), \ DPVS_MODULE(MODULE_EAL_MEM, "eal_mem", \ eal_mem_init, eal_mem_term), \ + DPVS_MODULE(MODULE_CPU_FDIR, "cpu_fdir", \ + cpu_fdir_init, cpu_fdir_term), \ DPVS_MODULE(MODULE_LAST, "last", \ NULL, NULL) \ } diff --git a/src/netif.c b/src/netif.c index d2ae6cf91da116ee7ccb3981eb1d40122da33d6b..85b0a0f44e6f636dd4bc3444ecc5aa42e5fd3078 100644 --- a/src/netif.c +++ b/src/netif.c @@ -44,6 +44,7 @@ #include <netinet/in.h> #include <arpa/inet.h> #include <ipvs/redirect.h> +#include <fdir/cpu_fdir.h> #ifdef CONFIG_ICMP_REDIRECT_CORE #include "icmp.h" #endif @@ -2373,6 +2374,12 @@ static int netif_deliver_mbuf(struct netif_port *dev, lcoreid_t cid, mbuf->packet_type = eth_type_parse(eth_hdr, dev);
+ int redirected = 0; + ret = cpu_fdir_redirect(mbuf, &redirected); + if (redirected) { + return ret; + } +
|