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