examples: take promiscuous mode switch result into account
[dpdk.git] / examples / quota_watermark / qw / init.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <sys/mman.h>
8
9 #include <rte_eal.h>
10
11 #include <rte_common.h>
12 #include <rte_errno.h>
13 #include <rte_ethdev.h>
14 #include <rte_memzone.h>
15 #include <rte_ring.h>
16 #include <rte_string_fns.h>
17
18 #include "args.h"
19 #include "init.h"
20 #include "main.h"
21 #include "../include/conf.h"
22
23
24 static struct rte_eth_conf port_conf = {
25                 .rxmode = {
26                         .split_hdr_size = 0,
27                 },
28                 .txmode = {
29                         .mq_mode = ETH_DCB_NONE,
30                 },
31 };
32
33 static struct rte_eth_fc_conf fc_conf = {
34                 .mode       = RTE_FC_TX_PAUSE,
35                 .high_water = 80 * 510 / 100,
36                 .low_water  = 60 * 510 / 100,
37                 .pause_time = 1337,
38                 .send_xon   = 0,
39 };
40
41
42 void configure_eth_port(uint16_t port_id)
43 {
44         int ret;
45         uint16_t nb_rxd = RX_DESC_PER_QUEUE;
46         uint16_t nb_txd = TX_DESC_PER_QUEUE;
47         struct rte_eth_rxconf rxq_conf;
48         struct rte_eth_txconf txq_conf;
49         struct rte_eth_dev_info dev_info;
50         struct rte_eth_conf local_port_conf = port_conf;
51
52         rte_eth_dev_stop(port_id);
53
54         rte_eth_dev_info_get(port_id, &dev_info);
55         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
56                 local_port_conf.txmode.offloads |=
57                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
58         ret = rte_eth_dev_configure(port_id, 1, 1, &local_port_conf);
59         if (ret < 0)
60                 rte_exit(EXIT_FAILURE, "Cannot configure port %u (error %d)\n",
61                                 (unsigned int) port_id, ret);
62
63         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
64         if (ret < 0)
65                 rte_exit(EXIT_FAILURE,
66                                 "Cannot adjust number of descriptors for port %u (error %d)\n",
67                                 (unsigned int) port_id, ret);
68
69         /* Initialize the port's RX queue */
70         rxq_conf = dev_info.default_rxconf;
71         rxq_conf.offloads = local_port_conf.rxmode.offloads;
72         ret = rte_eth_rx_queue_setup(port_id, 0, nb_rxd,
73                         rte_eth_dev_socket_id(port_id),
74                         &rxq_conf,
75                         mbuf_pool);
76         if (ret < 0)
77                 rte_exit(EXIT_FAILURE,
78                                 "Failed to setup RX queue on port %u (error %d)\n",
79                                 (unsigned int) port_id, ret);
80
81         /* Initialize the port's TX queue */
82         txq_conf = dev_info.default_txconf;
83         txq_conf.offloads = local_port_conf.txmode.offloads;
84         ret = rte_eth_tx_queue_setup(port_id, 0, nb_txd,
85                         rte_eth_dev_socket_id(port_id),
86                         &txq_conf);
87         if (ret < 0)
88                 rte_exit(EXIT_FAILURE,
89                                 "Failed to setup TX queue on port %u (error %d)\n",
90                                 (unsigned int) port_id, ret);
91
92         /* Initialize the port's flow control */
93         ret = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
94         if (ret < 0)
95                 rte_exit(EXIT_FAILURE,
96                                 "Failed to setup hardware flow control on port %u (error %d)\n",
97                                 (unsigned int) port_id, ret);
98
99         /* Start the port */
100         ret = rte_eth_dev_start(port_id);
101         if (ret < 0)
102                 rte_exit(EXIT_FAILURE, "Failed to start port %u (error %d)\n",
103                                 (unsigned int) port_id, ret);
104
105         /* Put it in promiscuous mode */
106         ret = rte_eth_promiscuous_enable(port_id);
107         if (ret != 0)
108                 rte_exit(EXIT_FAILURE,
109                         "Failed to enable promiscuous mode for port %u: %s\n",
110                         port_id, rte_strerror(-ret));
111 }
112
113 void
114 init_dpdk(void)
115 {
116         if (rte_eth_dev_count_avail() < 2)
117                 rte_exit(EXIT_FAILURE, "Not enough ethernet port available\n");
118 }
119
120 void init_ring(int lcore_id, uint16_t port_id)
121 {
122         struct rte_ring *ring;
123         char ring_name[RTE_RING_NAMESIZE];
124
125         snprintf(ring_name, RTE_RING_NAMESIZE,
126                         "core%d_port%d", lcore_id, port_id);
127         ring = rte_ring_create(ring_name, RING_SIZE, rte_socket_id(),
128                         RING_F_SP_ENQ | RING_F_SC_DEQ);
129
130         if (ring == NULL)
131                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
132
133         *high_watermark = 80 * RING_SIZE / 100;
134
135         rings[lcore_id][port_id] = ring;
136 }
137
138 void
139 pair_ports(void)
140 {
141         uint16_t i, j;
142
143         /* Pair ports with their "closest neighbour" in the portmask */
144         for (i = 0; i < RTE_MAX_ETHPORTS; i++)
145                 if (is_bit_set(i, portmask))
146                         for (j = i + 1; j < RTE_MAX_ETHPORTS; j++)
147                                 if (is_bit_set(j, portmask)) {
148                                         port_pairs[i] = j;
149                                         port_pairs[j] = i;
150                                         i = j;
151                                         break;
152                                 }
153 }
154
155 void
156 setup_shared_variables(void)
157 {
158         const struct rte_memzone *qw_memzone;
159
160         qw_memzone = rte_memzone_reserve(QUOTA_WATERMARK_MEMZONE_NAME,
161                         3 * sizeof(int), rte_socket_id(), 0);
162         if (qw_memzone == NULL)
163                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
164
165         quota = qw_memzone->addr;
166         low_watermark = (unsigned int *) qw_memzone->addr + 1;
167         high_watermark = (unsigned int *) qw_memzone->addr + 2;
168 }