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