94d8ea990ac9803f82460e696dd4428743378c9f
[dpdk.git] / examples / quota_watermark / qw / init.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/mman.h>
38
39 #include <rte_eal.h>
40
41 #include <rte_common.h>
42 #include <rte_errno.h>
43 #include <rte_ethdev.h>
44 #include <rte_memzone.h>
45 #include <rte_ring.h>
46 #include <rte_string_fns.h>
47
48 #include "args.h"
49 #include "init.h"
50 #include "main.h"
51 #include "../include/conf.h"
52
53
54 static const struct rte_eth_conf port_conf = {
55         .rxmode = {
56                 .split_hdr_size = 0,
57                 .header_split   = 0, /**< Header Split disabled */
58                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
59                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
60                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
61                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
62         },
63         .txmode = {
64                 .mq_mode = ETH_DCB_NONE,
65         },
66 };
67
68 static const struct rte_eth_rxconf rx_conf = {
69     .rx_thresh = {
70         .pthresh = 8,
71         .hthresh = 8,
72         .wthresh = 4,
73     },
74 };
75
76 static const struct rte_eth_txconf tx_conf = {
77     .tx_thresh = {
78         .pthresh = 36,
79         .hthresh = 0,
80         .wthresh = 0,
81     },
82     .tx_free_thresh = 0,
83     .tx_rs_thresh = 0,
84 };
85
86 static struct rte_eth_fc_conf fc_conf = {
87     .mode       = RTE_FC_TX_PAUSE,
88     .high_water = 80 * 510 / 100,
89     .low_water  = 60 * 510 / 100,
90     .pause_time = 1337,
91     .send_xon   = 0,
92 };
93
94
95 void configure_eth_port(uint8_t port_id)
96 {
97     int ret;
98
99     rte_eth_dev_stop(port_id);
100
101     ret = rte_eth_dev_configure(port_id, 1, 1, &port_conf);
102     if (ret < 0)
103         rte_exit(EXIT_FAILURE, "Cannot configure port %u (error %d)\n",
104                                (unsigned) port_id, ret);
105
106     /* Initialize the port's RX queue */
107     ret = rte_eth_rx_queue_setup(port_id, 0, RX_DESC_PER_QUEUE,
108                                  rte_eth_dev_socket_id(port_id), &rx_conf,
109                                  mbuf_pool);
110     if (ret < 0)
111         rte_exit(EXIT_FAILURE, "Failed to setup RX queue on "
112                                "port %u (error %d)\n", (unsigned) port_id, ret);
113
114     /* Initialize the port's TX queue */
115     ret = rte_eth_tx_queue_setup(port_id, 0, TX_DESC_PER_QUEUE,
116                                  rte_eth_dev_socket_id(port_id), &tx_conf);
117     if (ret < 0)
118         rte_exit(EXIT_FAILURE, "Failed to setup TX queue on "
119                                "port %u (error %d)\n", (unsigned) port_id, ret);
120
121     /* Initialize the port's flow control */
122     ret = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
123     if (ret < 0)
124         rte_exit(EXIT_FAILURE, "Failed to setup hardware flow control on "
125                                "port %u (error %d)\n", (unsigned) port_id, ret);
126
127     /* Start the port */
128     ret = rte_eth_dev_start(port_id);
129     if (ret < 0)
130         rte_exit(EXIT_FAILURE, "Failed to start port %u (error %d)\n",
131                                (unsigned) port_id, ret);
132
133     /* Put it in promiscuous mode */
134     rte_eth_promiscuous_enable(port_id);
135 }
136
137 void
138 init_dpdk(void)
139 {
140     int ret;
141
142     /* Initialize the PMD */
143     ret = rte_pmd_init_all();
144     if (ret < 0)
145         rte_exit(EXIT_FAILURE, "Failed to initialize poll mode drivers (error %d)\n", ret);
146
147     /* Bind the drivers to usable devices */
148     ret = rte_eal_pci_probe();
149     if (ret < 0)
150         rte_exit(EXIT_FAILURE, "rte_eal_pci_probe(): error %d\n", ret);
151
152     if (rte_eth_dev_count() < 2)
153         rte_exit(EXIT_FAILURE, "Not enough ethernet port available\n");
154 }
155
156 void init_ring(int lcore_id, uint8_t port_id)
157 {
158     struct rte_ring *ring;
159     char ring_name[RTE_RING_NAMESIZE];
160
161     rte_snprintf(ring_name, RTE_RING_NAMESIZE,
162                 "core%d_port%d", lcore_id, port_id);
163     ring = rte_ring_create(ring_name, RING_SIZE, rte_socket_id(),
164                            RING_F_SP_ENQ | RING_F_SC_DEQ);
165
166     if (ring == NULL)
167         rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
168
169     rte_ring_set_water_mark(ring, 80 * RING_SIZE / 100);
170
171     rings[lcore_id][port_id] = ring;
172 }
173
174 void
175 pair_ports(void)
176 {
177     uint8_t i, j;
178
179     /* Pair ports with their "closest neighbour" in the portmask */
180     for (i = 0; i < RTE_MAX_ETHPORTS; i++)
181         if (is_bit_set(i, portmask))
182             for (j = (uint8_t) (i + 1); j < RTE_MAX_ETHPORTS; j++)
183                 if (is_bit_set(j, portmask)) {
184                     port_pairs[i] = j;
185                     port_pairs[j] = i;
186                     i = j;
187                     break;
188                 }
189 }
190
191 void
192 setup_shared_variables(void)
193 {
194     const struct rte_memzone *qw_memzone;
195
196     qw_memzone = rte_memzone_reserve(QUOTA_WATERMARK_MEMZONE_NAME, 2 * sizeof(int),
197                                      rte_socket_id(), RTE_MEMZONE_2MB);
198     if (qw_memzone == NULL)
199         rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
200
201     quota = qw_memzone->addr;
202     low_watermark = (unsigned int *) qw_memzone->addr + sizeof(int);
203 }