New upstream version 18.02
[deb_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                         .ignore_offload_bitfield = 1,
28                         .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
29                 },
30                 .txmode = {
31                         .mq_mode = ETH_DCB_NONE,
32                 },
33 };
34
35 static struct rte_eth_fc_conf fc_conf = {
36                 .mode       = RTE_FC_TX_PAUSE,
37                 .high_water = 80 * 510 / 100,
38                 .low_water  = 60 * 510 / 100,
39                 .pause_time = 1337,
40                 .send_xon   = 0,
41 };
42
43
44 void configure_eth_port(uint16_t port_id)
45 {
46         int ret;
47         uint16_t nb_rxd = RX_DESC_PER_QUEUE;
48         uint16_t nb_txd = TX_DESC_PER_QUEUE;
49         struct rte_eth_rxconf rxq_conf;
50         struct rte_eth_txconf txq_conf;
51         struct rte_eth_dev_info dev_info;
52         struct rte_eth_conf local_port_conf = port_conf;
53
54         rte_eth_dev_stop(port_id);
55
56         rte_eth_dev_info_get(port_id, &dev_info);
57         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
58                 local_port_conf.txmode.offloads |=
59                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
60         ret = rte_eth_dev_configure(port_id, 1, 1, &local_port_conf);
61         if (ret < 0)
62                 rte_exit(EXIT_FAILURE, "Cannot configure port %u (error %d)\n",
63                                 (unsigned int) port_id, ret);
64
65         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
66         if (ret < 0)
67                 rte_exit(EXIT_FAILURE,
68                                 "Cannot adjust number of descriptors for port %u (error %d)\n",
69                                 (unsigned int) port_id, ret);
70
71         /* Initialize the port's RX queue */
72         rxq_conf = dev_info.default_rxconf;
73         rxq_conf.offloads = local_port_conf.rxmode.offloads;
74         ret = rte_eth_rx_queue_setup(port_id, 0, nb_rxd,
75                         rte_eth_dev_socket_id(port_id),
76                         &rxq_conf,
77                         mbuf_pool);
78         if (ret < 0)
79                 rte_exit(EXIT_FAILURE,
80                                 "Failed to setup RX queue on port %u (error %d)\n",
81                                 (unsigned int) port_id, ret);
82
83         /* Initialize the port's TX queue */
84         txq_conf = dev_info.default_txconf;
85         txq_conf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
86         txq_conf.offloads = local_port_conf.txmode.offloads;
87         ret = rte_eth_tx_queue_setup(port_id, 0, nb_txd,
88                         rte_eth_dev_socket_id(port_id),
89                         &txq_conf);
90         if (ret < 0)
91                 rte_exit(EXIT_FAILURE,
92                                 "Failed to setup TX queue on port %u (error %d)\n",
93                                 (unsigned int) port_id, ret);
94
95         /* Initialize the port's flow control */
96         ret = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
97         if (ret < 0)
98                 rte_exit(EXIT_FAILURE,
99                                 "Failed to setup hardware flow control on port %u (error %d)\n",
100                                 (unsigned int) port_id, ret);
101
102         /* Start the port */
103         ret = rte_eth_dev_start(port_id);
104         if (ret < 0)
105                 rte_exit(EXIT_FAILURE, "Failed to start port %u (error %d)\n",
106                                 (unsigned int) port_id, ret);
107
108         /* Put it in promiscuous mode */
109         rte_eth_promiscuous_enable(port_id);
110 }
111
112 void
113 init_dpdk(void)
114 {
115         if (rte_eth_dev_count() < 2)
116                 rte_exit(EXIT_FAILURE, "Not enough ethernet port available\n");
117 }
118
119 void init_ring(int lcore_id, uint16_t port_id)
120 {
121         struct rte_ring *ring;
122         char ring_name[RTE_RING_NAMESIZE];
123
124         snprintf(ring_name, RTE_RING_NAMESIZE,
125                         "core%d_port%d", lcore_id, port_id);
126         ring = rte_ring_create(ring_name, RING_SIZE, rte_socket_id(),
127                         RING_F_SP_ENQ | RING_F_SC_DEQ);
128
129         if (ring == NULL)
130                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
131
132         *high_watermark = 80 * RING_SIZE / 100;
133
134         rings[lcore_id][port_id] = ring;
135 }
136
137 void
138 pair_ports(void)
139 {
140         uint16_t i, j;
141
142         /* Pair ports with their "closest neighbour" in the portmask */
143         for (i = 0; i < RTE_MAX_ETHPORTS; i++)
144                 if (is_bit_set(i, portmask))
145                         for (j = i + 1; j < RTE_MAX_ETHPORTS; j++)
146                                 if (is_bit_set(j, portmask)) {
147                                         port_pairs[i] = j;
148                                         port_pairs[j] = i;
149                                         i = j;
150                                         break;
151                                 }
152 }
153
154 void
155 setup_shared_variables(void)
156 {
157         const struct rte_memzone *qw_memzone;
158
159         qw_memzone = rte_memzone_reserve(QUOTA_WATERMARK_MEMZONE_NAME,
160                         3 * sizeof(int), rte_socket_id(), 0);
161         if (qw_memzone == NULL)
162                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
163
164         quota = qw_memzone->addr;
165         low_watermark = (unsigned int *) qw_memzone->addr + 1;
166         high_watermark = (unsigned int *) qw_memzone->addr + 2;
167 }