0c069ec350aafce9377c515288f0c112f937f7d4
[dpdk.git] / examples / l2fwd-event / l2fwd_common.c
1 #include "l2fwd_common.h"
2
3 int
4 l2fwd_event_init_ports(struct l2fwd_resources *rsrc)
5 {
6         uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
7         uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
8         struct rte_eth_conf port_conf = {
9                 .rxmode = {
10                         .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
11                         .split_hdr_size = 0,
12                 },
13                 .txmode = {
14                         .mq_mode = ETH_MQ_TX_NONE,
15                 },
16         };
17         uint16_t nb_ports_available = 0;
18         uint16_t port_id;
19         int ret;
20
21         if (rsrc->event_mode) {
22                 port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
23                 port_conf.rx_adv_conf.rss_conf.rss_key = NULL;
24                 port_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IP;
25         }
26
27         /* Initialise each port */
28         RTE_ETH_FOREACH_DEV(port_id) {
29                 struct rte_eth_conf local_port_conf = port_conf;
30                 struct rte_eth_dev_info dev_info;
31                 struct rte_eth_rxconf rxq_conf;
32                 struct rte_eth_txconf txq_conf;
33
34                 /* skip ports that are not enabled */
35                 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0) {
36                         printf("Skipping disabled port %u\n", port_id);
37                         continue;
38                 }
39                 nb_ports_available++;
40
41                 /* init port */
42                 printf("Initializing port %u... ", port_id);
43                 fflush(stdout);
44
45                 ret = rte_eth_dev_info_get(port_id, &dev_info);
46                 if (ret != 0)
47                         rte_panic("Error during getting device (port %u) info: %s\n",
48                                   port_id, strerror(-ret));
49                 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
50                         local_port_conf.txmode.offloads |=
51                                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
52                 ret = rte_eth_dev_configure(port_id, 1, 1, &local_port_conf);
53                 if (ret < 0)
54                         rte_panic("Cannot configure device: err=%d, port=%u\n",
55                                   ret, port_id);
56
57                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd,
58                                                        &nb_txd);
59                 if (ret < 0)
60                         rte_panic("Cannot adjust number of descriptors: err=%d, port=%u\n",
61                                   ret, port_id);
62
63                 rte_eth_macaddr_get(port_id, &rsrc->eth_addr[port_id]);
64
65                 /* init one RX queue */
66                 fflush(stdout);
67                 rxq_conf = dev_info.default_rxconf;
68                 rxq_conf.offloads = local_port_conf.rxmode.offloads;
69                 ret = rte_eth_rx_queue_setup(port_id, 0, nb_rxd,
70                                              rte_eth_dev_socket_id(port_id),
71                                              &rxq_conf,
72                                              rsrc->pktmbuf_pool);
73                 if (ret < 0)
74                         rte_panic("rte_eth_rx_queue_setup:err=%d, port=%u\n",
75                                   ret, port_id);
76
77                 /* init one TX queue on each port */
78                 fflush(stdout);
79                 txq_conf = dev_info.default_txconf;
80                 txq_conf.offloads = local_port_conf.txmode.offloads;
81                 ret = rte_eth_tx_queue_setup(port_id, 0, nb_txd,
82                                 rte_eth_dev_socket_id(port_id),
83                                 &txq_conf);
84                 if (ret < 0)
85                         rte_panic("rte_eth_tx_queue_setup:err=%d, port=%u\n",
86                                   ret, port_id);
87
88                 rte_eth_promiscuous_enable(port_id);
89
90                 printf("Port %u,MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
91                         port_id,
92                         rsrc->eth_addr[port_id].addr_bytes[0],
93                         rsrc->eth_addr[port_id].addr_bytes[1],
94                         rsrc->eth_addr[port_id].addr_bytes[2],
95                         rsrc->eth_addr[port_id].addr_bytes[3],
96                         rsrc->eth_addr[port_id].addr_bytes[4],
97                         rsrc->eth_addr[port_id].addr_bytes[5]);
98         }
99
100         return nb_ports_available;
101 }