New upstream version 17.11-rc3
[deb_dpdk.git] / examples / quota_watermark / qw / init.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/mman.h>
37
38 #include <rte_eal.h>
39
40 #include <rte_common.h>
41 #include <rte_errno.h>
42 #include <rte_ethdev.h>
43 #include <rte_memzone.h>
44 #include <rte_ring.h>
45 #include <rte_string_fns.h>
46
47 #include "args.h"
48 #include "init.h"
49 #include "main.h"
50 #include "../include/conf.h"
51
52
53 static const struct rte_eth_conf port_conf = {
54                 .rxmode = {
55                         .split_hdr_size = 0,
56                         .header_split   = 0, /**< Header Split disabled */
57                         .hw_ip_checksum = 0, /**< IP csum offload disabled */
58                         .hw_vlan_filter = 0, /**< VLAN filtering disabled */
59                         .jumbo_frame    = 0, /**< Jumbo Frame disabled */
60                         .hw_strip_crc   = 1, /**< CRC stripped by hardware */
61                 },
62                 .txmode = {
63                         .mq_mode = ETH_DCB_NONE,
64                 },
65 };
66
67 static struct rte_eth_fc_conf fc_conf = {
68                 .mode       = RTE_FC_TX_PAUSE,
69                 .high_water = 80 * 510 / 100,
70                 .low_water  = 60 * 510 / 100,
71                 .pause_time = 1337,
72                 .send_xon   = 0,
73 };
74
75
76 void configure_eth_port(uint16_t port_id)
77 {
78         int ret;
79         uint16_t nb_rxd = RX_DESC_PER_QUEUE;
80         uint16_t nb_txd = TX_DESC_PER_QUEUE;
81
82         rte_eth_dev_stop(port_id);
83
84         ret = rte_eth_dev_configure(port_id, 1, 1, &port_conf);
85         if (ret < 0)
86                 rte_exit(EXIT_FAILURE, "Cannot configure port %u (error %d)\n",
87                                 (unsigned int) port_id, ret);
88
89         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
90         if (ret < 0)
91                 rte_exit(EXIT_FAILURE,
92                                 "Cannot adjust number of descriptors for port %u (error %d)\n",
93                                 (unsigned int) port_id, ret);
94
95         /* Initialize the port's RX queue */
96         ret = rte_eth_rx_queue_setup(port_id, 0, nb_rxd,
97                         rte_eth_dev_socket_id(port_id),
98                         NULL,
99                         mbuf_pool);
100         if (ret < 0)
101                 rte_exit(EXIT_FAILURE,
102                                 "Failed to setup RX queue on port %u (error %d)\n",
103                                 (unsigned int) port_id, ret);
104
105         /* Initialize the port's TX queue */
106         ret = rte_eth_tx_queue_setup(port_id, 0, nb_txd,
107                         rte_eth_dev_socket_id(port_id),
108                         NULL);
109         if (ret < 0)
110                 rte_exit(EXIT_FAILURE,
111                                 "Failed to setup TX queue on port %u (error %d)\n",
112                                 (unsigned int) port_id, ret);
113
114         /* Initialize the port's flow control */
115         ret = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
116         if (ret < 0)
117                 rte_exit(EXIT_FAILURE,
118                                 "Failed to setup hardware flow control on port %u (error %d)\n",
119                                 (unsigned int) port_id, ret);
120
121         /* Start the port */
122         ret = rte_eth_dev_start(port_id);
123         if (ret < 0)
124                 rte_exit(EXIT_FAILURE, "Failed to start port %u (error %d)\n",
125                                 (unsigned int) port_id, ret);
126
127         /* Put it in promiscuous mode */
128         rte_eth_promiscuous_enable(port_id);
129 }
130
131 void
132 init_dpdk(void)
133 {
134         if (rte_eth_dev_count() < 2)
135                 rte_exit(EXIT_FAILURE, "Not enough ethernet port available\n");
136 }
137
138 void init_ring(int lcore_id, uint16_t port_id)
139 {
140         struct rte_ring *ring;
141         char ring_name[RTE_RING_NAMESIZE];
142
143         snprintf(ring_name, RTE_RING_NAMESIZE,
144                         "core%d_port%d", lcore_id, port_id);
145         ring = rte_ring_create(ring_name, RING_SIZE, rte_socket_id(),
146                         RING_F_SP_ENQ | RING_F_SC_DEQ);
147
148         if (ring == NULL)
149                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
150
151         *high_watermark = 80 * RING_SIZE / 100;
152
153         rings[lcore_id][port_id] = ring;
154 }
155
156 void
157 pair_ports(void)
158 {
159         uint16_t i, j;
160
161         /* Pair ports with their "closest neighbour" in the portmask */
162         for (i = 0; i < RTE_MAX_ETHPORTS; i++)
163                 if (is_bit_set(i, portmask))
164                         for (j = i + 1; j < RTE_MAX_ETHPORTS; j++)
165                                 if (is_bit_set(j, portmask)) {
166                                         port_pairs[i] = j;
167                                         port_pairs[j] = i;
168                                         i = j;
169                                         break;
170                                 }
171 }
172
173 void
174 setup_shared_variables(void)
175 {
176         const struct rte_memzone *qw_memzone;
177
178         qw_memzone = rte_memzone_reserve(QUOTA_WATERMARK_MEMZONE_NAME,
179                         3 * sizeof(int), rte_socket_id(), 0);
180         if (qw_memzone == NULL)
181                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
182
183         quota = qw_memzone->addr;
184         low_watermark = (unsigned int *) qw_memzone->addr + 1;
185         high_watermark = (unsigned int *) qw_memzone->addr + 2;
186 }