#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <inttypes.h> #include <sys/types.h> #include <sys/queue.h> #include <netinet/in.h> #include <setjmp.h> #include <stdarg.h> #include <ctype.h> #include <errno.h> #include <getopt.h> #include <signal.h> #include <stdbool.h> #include <rte_common.h> #include <rte_log.h> #include <rte_malloc.h> #include <rte_memory.h> #include <rte_memcpy.h> #include <rte_memzone.h> #include <rte_eal.h> #include <rte_per_lcore.h> #include <rte_launch.h> #include <rte_atomic.h> #include <rte_cycles.h> #include <rte_prefetch.h> #include <rte_lcore.h> #include <rte_per_lcore.h> #include <rte_branch_prediction.h> #include <rte_interrupts.h> #include <rte_pci.h> #include <rte_random.h> #include <rte_debug.h> #include <rte_ether.h> #include <rte_ethdev.h> #include <rte_mempool.h> #include <rte_mbuf.h> static volatile bool force_quit; /* MAC updating enabled by default */ static int mac_updating = 1; #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1 #define NB_MBUF 8192 #define MAX_PKT_BURST 32 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ #define MEMPOOL_CACHE_SIZE 256 /* * Configurable number of RX/TX ring descriptors */ #define RTE_TEST_RX_DESC_DEFAULT 128 #define RTE_TEST_TX_DESC_DEFAULT 512 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /* ethernet addresses of ports */ //端口的以太網地址 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS]; /* mask of enabled ports */ //啟用端口的mask static uint32_t l2fwd_enabled_port_mask = 0; /* list of enabled ports */ //啟用端口列表 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS]; static unsigned int l2fwd_rx_queue_per_lcore = 1; #define MAX_RX_QUEUE_PER_LCORE 16 #define MAX_TX_QUEUE_PER_PORT 16 struct lcore_queue_conf { unsigned n_rx_port; unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE]; } __rte_cache_aligned; struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS]; static const struct rte_eth_conf port_conf = { .rxmode = { .split_hdr_size = 0, .header_split = 0, /**< Header Split disabled */ //報頭分離 .hw_ip_checksum = 0, /**< IP checksum offload disabled */ //IP校驗和卸載 .hw_vlan_filter = 0, /**< VLAN filtering disabled */ //vlan過濾 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */ //巨星幀的支持 .hw_strip_crc = 0, /**< CRC stripped by hardware */ //使用硬件清除CRC }, .txmode = { .mq_mode = ETH_MQ_TX_NONE, }, }; struct rte_mempool * l2fwd_pktmbuf_pool = NULL; /* Per-port statistics struct */ struct l2fwd_port_statistics { uint64_t tx; uint64_t rx; uint64_t dropped; } __rte_cache_aligned; struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS]; #define MAX_TIMER_PERIOD 86400 /* 1 day max */ /* A tsc-based timer responsible for triggering statistics printout */ static uint64_t timer_period = 10; /* default period is 10 seconds */ /* Print out statistics on packets dropped */ //打印的屬性跳過。。 static void print_stats(void) { uint64_t total_packets_dropped, total_packets_tx, total_packets_rx; unsigned portid; total_packets_dropped = 0; total_packets_tx = 0; total_packets_rx = 0; const char clr[] = { 27, '[', '2', 'J', '\0' }; const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' }; /* Clear screen and move to top left */ printf("%s%s", clr, topLeft); printf("\nPort statistics ===================================="); for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { /* skip disabled ports */ if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) continue; printf("\nStatistics for port %u ------------------------------" "\nPackets sent: %24"PRIu64 "\nPackets received: %20"PRIu64 "\nPackets dropped: %21"PRIu64, portid, port_statistics[portid].tx, port_statistics[portid].rx, port_statistics[portid].dropped); total_packets_dropped += port_statistics[portid].dropped; total_packets_tx += port_statistics[portid].tx; total_packets_rx += port_statistics[portid].rx; } printf("\nAggregate statistics ===============================" "\nTotal packets sent: %18"PRIu64 "\nTotal packets received: %14"PRIu64 "\nTotal packets dropped: %15"PRIu64, total_packets_tx, total_packets_rx, total_packets_dropped); printf("\n====================================================\n"); } static void l2fwd_mac_updating(struct rte_mbuf *m, unsigned dest_portid) { struct ether_hdr *eth; void *tmp; eth = rte_pktmbuf_mtod(m, struct ether_hdr *); /* 02:00:00:00:00:xx */ tmp = ð->d_addr.addr_bytes[0]; *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40); /* src addr */ ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr); } //處理收到的數據包 static void l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid) { unsigned dst_port; int sent; struct rte_eth_dev_tx_buffer *buffer; //獲取目的端口ID dst_port = l2fwd_dst_ports[portid]; //更新MAC地址 if (mac_updating) l2fwd_mac_updating(m, dst_port); //獲取目的端口的 tx 緩存 buffer = tx_buffer[dst_port]; //發送數據包,到目的端口的tx緩存 sent = rte_eth_tx_buffer(dst_port, 0, buffer, m); if (sent) port_statistics[dst_port].tx += sent; //如果發包成功,發包計數+1 } /* main processing loop */ static void l2fwd_main_loop(void) { struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; struct rte_mbuf *m; int sent; unsigned lcore_id; uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc; unsigned i, j, portid, nb_rx; struct lcore_queue_conf *qconf; const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US; struct rte_eth_dev_tx_buffer *buffer; prev_tsc = 0; timer_tsc = 0; //獲取當前核心的ID lcore_id = rte_lcore_id(); //獲取當前核心的配置 qconf = &lcore_queue_conf[lcore_id]; //如果 rx_port的個數為0,則 記錄日志。(其實是出錯了) if (qconf->n_rx_port == 0) { RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id); return; } //記錄日志, RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id); //遍歷所有的port for (i = 0; i < qconf->n_rx_port; i++) { //獲取到portID portid = qconf->rx_port_list[i]; //記錄日志 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id, portid); } //如果沒超時(最開始設置的那個二層轉發的運行時間) while (!force_quit) { //獲取時間戳 cur_tsc = rte_rdtsc(); /* * TX burst queue drain */ //對比時間戳 diff_tsc = cur_tsc - prev_tsc; if (unlikely(diff_tsc > drain_tsc)) { for (i = 0; i < qconf->n_rx_port; i++) { //獲得portid和buffer portid = l2fwd_dst_ports[qconf->rx_port_list[i]]; buffer = tx_buffer[portid]; //把buffer中的數據發送到portid對應的port sent = rte_eth_tx_buffer_flush(portid, 0, buffer); if (sent) port_statistics[portid].tx += sent; //如果發包成功,發包計數+1 } /* if timer is enabled */ //如果計時器開啟 if (timer_period > 0) { /* advance the timer */ //調整計時器 timer_tsc += diff_tsc; /* if timer has reached its timeout */ //如果計時器超時 if (unlikely(timer_tsc >= timer_period)) { /* do this only on master core */ //主線程打印一些屬性,僅有主線程會執行print_stats if (lcore_id == rte_get_master_lcore()) { print_stats(); //打印屬性, /* reset the timer */ timer_tsc = 0; //設置計時器為0 } } } prev_tsc = cur_tsc; } /* * Read packet from RX queues //收包模塊 */ for (i = 0; i < qconf->n_rx_port; i++) { //獲取portID portid = qconf->rx_port_list[i]; //從portID對應的Port收到nb_rx個包 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0, pkts_burst, MAX_PKT_BURST); port_statistics[portid].rx += nb_rx; //收包計數+=nb_rx for (j = 0; j < nb_rx; j++) { //遍歷收到的包 m = pkts_burst[j]; rte_prefetch0(rte_pktmbuf_mtod(m, void *)); //預取到pktmbuf l2fwd_simple_forward(m, portid); //調用函數,處理收到的包 } } } } //__attribute__((unused))表示可能沒有這個參數,編譯器不會報錯 static int l2fwd_launch_one_lcore(__attribute__((unused)) void *dummy) { l2fwd_main_loop(); //二層轉發主要循環 return 0; } /* display usage */ static void l2fwd_usage(const char *prgname) { printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n" " -p PORTMASK: hexadecimal bitmask of ports to configure\n" " -q NQ: number of queue (=ports) per lcore (default is 1)\n" " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n" " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n" " When enabled:\n" " - The source MAC address is replaced by the TX port MAC address\n" " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n", prgname); } static int l2fwd_parse_portmask(const char *portmask) { char *end = NULL; unsigned long pm; /* parse hexadecimal string */ //解析十六進制字符串,將十六進制字符串轉換為unsigned long pm = strtoul(portmask, &end, 16); if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) return -1; if (pm == 0) return -1; return pm; } static unsigned int l2fwd_parse_nqueue(const char *q_arg) { char *end = NULL; unsigned long n; /* parse hexadecimal string */ n = strtoul(q_arg, &end, 10); if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) return 0; if (n == 0) return 0; if (n >= MAX_RX_QUEUE_PER_LCORE) return 0; return n; } static int l2fwd_parse_timer_period(const char *q_arg) { char *end = NULL; int n; /* parse number string */ n = strtol(q_arg, &end, 10); if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) return -1; if (n >= MAX_TIMER_PERIOD) return -1; return n; } //解析命令行參數 /* Parse the argument given in the command line of the application */ static int l2fwd_parse_args(int argc, char **argv) { int opt, ret, timer_secs; char **argvopt; int option_index; char *prgname = argv[0]; static struct option lgopts[] = { { "mac-updating", no_argument, &mac_updating, 1}, { "no-mac-updating", no_argument, &mac_updating, 0}, {NULL, 0, 0, 0} }; argvopt = argv; //復制argv指針 //解析命令行,getopt_long()可以解析命令行參數 while ((opt = getopt_long(argc, argvopt, "p:q:T:", lgopts, &option_index)) != EOF) { //命令行有三個參數, p:portmask,n:nqueue,T:timer period switch (opt) { /* portmask */ //port的個數,以十六進制表示的,如0x0f表示15 case 'p': //l2fwd_parse_portmask()函數為自定義函數 //把十六進制的mask字符串轉換成unsigned long。 //端口使能情況 l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg); if (l2fwd_enabled_port_mask == 0) { //mask為0表示出錯 printf("invalid portmask\n"); l2fwd_usage(prgname); //打印用戶選項,類似於 --help return -1; //參數傳遞錯誤,會返回-1,其結果是退出程序 } break; /* nqueue */ //隊列的個數,同樣也是用十六進制字符串表示的。 case 'q': //每個核心配置幾個 rx 隊列。 l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg); if (l2fwd_rx_queue_per_lcore == 0) { //lcore 0表示出錯 printf("invalid queue number\n"); l2fwd_usage(prgname); return -1; //返回-1,其結果是退出程序。 } break; /* timer period */ //定時器周期 case 'T': //配置定時器周期,單位為秒 //即l2fwd運行多少秒,使用十六進制表示的。 timer_secs = l2fwd_parse_timer_period(optarg); if (timer_secs < 0) { printf("invalid timer period\n"); l2fwd_usage(prgname); return -1; } //timer_period表示二層轉發測試時間,默認為10秒,可通過-T來調節時間 timer_period = timer_secs; break; /* long options */ //--打頭的選項不處理 case 0: break; default: //參數傳遞錯誤,打印--help l2fwd_usage(prgname); return -1; } } if (optind >= 0) argv[optind-1] = prgname; ret = optind-1; optind = 0; /* reset getopt lib */ return ret; } /* Check the link status of all ports in up to 9s, and print them finally */ static void check_all_ports_link_status(uint8_t port_num, uint32_t port_mask) { #define CHECK_INTERVAL 100 /* 100ms */ #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ uint8_t portid, count, all_ports_up, print_flag = 0; struct rte_eth_link link; printf("\nChecking link status"); fflush(stdout); for (count = 0; count <= MAX_CHECK_TIME; count++) { if (force_quit) return; all_ports_up = 1; for (portid = 0; portid < port_num; portid++) { if (force_quit) return; if ((port_mask & (1 << portid)) == 0) continue; memset(&link, 0, sizeof(link)); rte_eth_link_get_nowait(portid, &link); /* print link status if flag set */ if (print_flag == 1) { if (link.link_status) printf("Port %d Link Up - speed %u " "Mbps - %s\n", (uint8_t)portid, (unsigned)link.link_speed, (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? ("full-duplex") : ("half-duplex\n")); else printf("Port %d Link Down\n", (uint8_t)portid); continue; } /* clear all_ports_up flag if any link down */ if (link.link_status == ETH_LINK_DOWN) { all_ports_up = 0; break; } } /* after finally printing all link status, get out */ if (print_flag == 1) break; if (all_ports_up == 0) { printf("."); fflush(stdout); rte_delay_ms(CHECK_INTERVAL); } /* set the print_flag if all ports up or timeout */ if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { print_flag = 1; printf("done\n"); } } } static void signal_handler(int signum) { if (signum == SIGINT || signum == SIGTERM) { printf("\n\nSignal %d received, preparing to exit...\n", signum); force_quit = true; //強制退出按鈕為真 } } int main(int argc, char **argv) { /* //程序前面自定義的結構 // __rte_cache_aligned 是一個宏,表示內存對其 struct lcore_queue_conf { unsigned n_rx_port; //rx_port個數 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE]; //rx_port列表 } __rte_cache_aligned; struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; //一個計算機,包含多個核心,一個核心處理多個rx_port */ struct lcore_queue_conf *qconf; struct rte_eth_dev_info dev_info; //設備信息 int ret; //返回值 uint8_t nb_ports; //總port個數 uint8_t nb_ports_available; //可用port個數 uint8_t portid, last_port; //當前portid,前一個portid。 unsigned lcore_id, rx_lcore_id; //核心id,rx_lcore_id unsigned nb_ports_in_mask = 0; //?? /* init EAL */ //初始化環境 ret = rte_eal_init(argc, argv); if (ret < 0) rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n"); argc -= ret; //??未知,貌似沒用,不明所以 argv += ret; //??未知,貌似沒用,不明所以 force_quit = false; //強制推出按鈕為假 //當捕獲到SIGINT或SIGTERM信號時,強制推出按鈕為真,詳見signal_handler //signal_handler為自定義函數 signal(SIGINT, signal_handler); //信號捕獲處理1 signal(SIGTERM, signal_handler); //信號捕獲處理2 /* parse application arguments (after the EAL ones) */ //解析傳參(必須在eal_init之后),此為自定義函數,詳見函數定義 ret = l2fwd_parse_args(argc, argv); if (ret < 0) rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n"); printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled"); /* convert to number of cycles */ //獲取定時器的時鍾周期 timer_period *= rte_get_timer_hz(); /* create the mbuf pool */ //創建緩存池,用於存儲數據包。 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); if (l2fwd_pktmbuf_pool == NULL) rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n"); //獲取Eth口的個數 nb_ports = rte_eth_dev_count(); if (nb_ports == 0) rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n"); /* reset l2fwd_dst_ports */ //復位(清0)目的端口,此代碼塊作用是初始化l2fwd_dst_ports //個人認為寫的比較low for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) l2fwd_dst_ports[portid] = 0; last_port = 0; /* * Each logical core is assigned a dedicated TX queue on each port. */ //設置每個 port 接收的數據包要 轉發 到的目的port。 for (portid = 0; portid < nb_ports; portid++) { /* skip ports that are not enabled */ //跳過沒使能的端口 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) continue; //每兩個port為一對,互相作二層轉發 //單數發到單數+1,雙數發到雙數-1 if (nb_ports_in_mask % 2) { l2fwd_dst_ports[portid] = last_port; l2fwd_dst_ports[last_port] = portid; } else last_port = portid; //對last_port賦值。 nb_ports_in_mask++; //對標記 ++ rte_eth_dev_info_get(portid, &dev_info); //獲取對應portid的dev信息 } //如果剩下一個網口,則該網口自己轉發給自己 if (nb_ports_in_mask % 2) { printf("Notice: odd number of ports in portmask.\n"); l2fwd_dst_ports[last_port] = last_port; } rx_lcore_id = 0; qconf = NULL; //初始化每個核心的隊列配置 /* Initialize the port/queue configuration of each logical core */ //遍歷所有port for (portid = 0; portid < nb_ports; portid++) { /* skip ports that are not enabled */ //跳過沒使能的端口 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) continue; /* get the lcore_id for this port */ //port分配一個 lcore_id。 //rte_lcore_is_enabled(rx_lcore_id) 測試core是否使能 //lcore_queue_conf是一個自定義的結構,包含port個數和portid //意思是一個核心,對多個port的rx隊列 //在這里,一個核心只能匹配一個port while (rte_lcore_is_enabled(rx_lcore_id) == 0 || lcore_queue_conf[rx_lcore_id].n_rx_port == l2fwd_rx_queue_per_lcore) { rx_lcore_id++; if (rx_lcore_id >= RTE_MAX_LCORE) rte_exit(EXIT_FAILURE, "Not enough cores\n"); } //賦值qconf,qconf最開始為NULL if (qconf != &lcore_queue_conf[rx_lcore_id]) /* Assigned a new logical core in the loop above. */ qconf = &lcore_queue_conf[rx_lcore_id]; //當前lcore添加一個port //當前lcore管理的rx_port總數+1 qconf->rx_port_list[qconf->n_rx_port] = portid; qconf->n_rx_port++; printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned) portid); } //使能的port總數 nb_ports_available = nb_ports; /* Initialise each port */ //獲取可用的port個數 for (portid = 0; portid < nb_ports; portid++) { /* skip ports that are not enabled */ //不可用的會剪掉。 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) { printf("Skipping disabled port %u\n", (unsigned) portid); nb_ports_available--; continue; } /* init port */ //初始化port printf("Initializing port %u... ", (unsigned) portid); fflush(stdout); //配置port,其中port_conf是一個結構,包含關於port的配置 //vlan分離,crc硬件校驗,認為這兩個有用。 ret = rte_eth_dev_configure(portid, 1, 1, &port_conf); if (ret < 0) rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n", ret, (unsigned) portid); //獲取port的mac地址 //l2fwd_ports_eth_addr[] 的 類型為一個結構,是全局數組 rte_eth_macaddr_get(portid,&l2fwd_ports_eth_addr[portid]); /* init one RX queue */ //初始化RX隊列 fflush(stdout); //rx隊列會綁定到當前port上,l2fwd_pktmbuf_pool為內存池 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd, rte_eth_dev_socket_id(portid), NULL, l2fwd_pktmbuf_pool); if (ret < 0) rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n", ret, (unsigned) portid); /* init one TX queue on each port */ //初始化tx隊列 fflush(stdout); //tx隊列會綁定到port上 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd, rte_eth_dev_socket_id(portid), NULL); if (ret < 0) rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n", ret, (unsigned) portid); /* Initialize TX buffers */ //malloc tx緩存,tx_buffer為指針數組,每個port對應一個tx緩存 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer", RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0, rte_eth_dev_socket_id(portid)); if (tx_buffer[portid] == NULL) rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n", (unsigned) portid); //初始化tx緩存 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST); //tx緩存滿了的時候,設置回調函數 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid], rte_eth_tx_buffer_count_callback, &port_statistics[portid].dropped); if (ret < 0) rte_exit(EXIT_FAILURE, "Cannot set error callback for " "tx buffer on port %u\n", (unsigned) portid); /* Start device */ //啟動設備 ret = rte_eth_dev_start(portid); if (ret < 0) rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n", ret, (unsigned) portid); printf("done: \n"); //設置網卡為混雜模式 rte_eth_promiscuous_enable(portid); //打印port的MAC地址 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n", (unsigned) portid, l2fwd_ports_eth_addr[portid].addr_bytes[0], l2fwd_ports_eth_addr[portid].addr_bytes[1], l2fwd_ports_eth_addr[portid].addr_bytes[2], l2fwd_ports_eth_addr[portid].addr_bytes[3], l2fwd_ports_eth_addr[portid].addr_bytes[4], l2fwd_ports_eth_addr[portid].addr_bytes[5]); /* initialize port stats */ //初始化port屬性 memset(&port_statistics, 0, sizeof(port_statistics)); } //如果可以使能的網卡為0個,則報錯。 if (!nb_ports_available) { rte_exit(EXIT_FAILURE, "All available ports are disabled. Please set portmask.\n"); } //檢查所有port的link屬性 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask); ret = 0; /* launch per-lcore init on every lcore */ //啟動所有的lcore,回調l2fwd_launch_one_lcore函數,參數為NULL rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MASTER); //遍歷所有lcore_id RTE_LCORE_FOREACH_SLAVE(lcore_id) { //等所有線程結束 if (rte_eal_wait_lcore(lcore_id) < 0) { ret = -1; break; } } //停止並且關閉所有的port for (portid = 0; portid < nb_ports; portid++) { if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) continue; printf("Closing port %d...", portid); rte_eth_dev_stop(portid); rte_eth_dev_close(portid); printf(" Done\n"); } printf("Bye...\n"); return ret; }
