apps: use helper to create mbuf pools
[dpdk.git] / examples / qos_sched / 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 <stdint.h>
35 #include <memory.h>
36
37 #include <rte_log.h>
38 #include <rte_mbuf.h>
39 #include <rte_debug.h>
40 #include <rte_ethdev.h>
41 #include <rte_mempool.h>
42 #include <rte_sched.h>
43 #include <rte_cycles.h>
44 #include <rte_string_fns.h>
45
46 #include "main.h"
47 #include "cfg_file.h"
48
49 uint32_t app_numa_mask = 0;
50 static uint32_t app_inited_port_mask = 0;
51
52 int app_pipe_to_profile[MAX_SCHED_SUBPORTS][MAX_SCHED_PIPES];
53
54 #define MAX_NAME_LEN 32
55
56 struct ring_conf ring_conf = {
57         .rx_size   = APP_RX_DESC_DEFAULT,
58         .ring_size = APP_RING_SIZE,
59         .tx_size   = APP_TX_DESC_DEFAULT,
60 };
61
62 struct burst_conf burst_conf = {
63         .rx_burst    = MAX_PKT_RX_BURST,
64         .ring_burst  = PKT_ENQUEUE,
65         .qos_dequeue = PKT_DEQUEUE,
66         .tx_burst    = MAX_PKT_TX_BURST,
67 };
68
69 struct ring_thresh rx_thresh = {
70         .pthresh = RX_PTHRESH,
71         .hthresh = RX_HTHRESH,
72         .wthresh = RX_WTHRESH,
73 };
74
75 struct ring_thresh tx_thresh = {
76         .pthresh = TX_PTHRESH,
77         .hthresh = TX_HTHRESH,
78         .wthresh = TX_WTHRESH,
79 };
80
81 uint32_t nb_pfc;
82 const char *cfg_profile = NULL;
83 int mp_size = NB_MBUF;
84 struct flow_conf qos_conf[MAX_DATA_STREAMS];
85
86 static const struct rte_eth_conf port_conf = {
87         .rxmode = {
88                 .max_rx_pkt_len = ETHER_MAX_LEN,
89                 .split_hdr_size = 0,
90                 .header_split   = 0, /**< Header Split disabled */
91                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
92                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
93                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
94                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
95         },
96         .txmode = {
97                 .mq_mode = ETH_DCB_NONE,
98         },
99 };
100
101 static int
102 app_init_port(uint8_t portid, struct rte_mempool *mp)
103 {
104         int ret;
105         struct rte_eth_link link;
106         struct rte_eth_rxconf rx_conf;
107         struct rte_eth_txconf tx_conf;
108
109         /* check if port already initialized (multistream configuration) */
110         if (app_inited_port_mask & (1u << portid))
111                 return 0;
112
113         rx_conf.rx_thresh.pthresh = rx_thresh.pthresh;
114         rx_conf.rx_thresh.hthresh = rx_thresh.hthresh;
115         rx_conf.rx_thresh.wthresh = rx_thresh.wthresh;
116         rx_conf.rx_free_thresh = 32;
117         rx_conf.rx_drop_en = 0;
118
119         tx_conf.tx_thresh.pthresh = tx_thresh.pthresh;
120         tx_conf.tx_thresh.hthresh = tx_thresh.hthresh;
121         tx_conf.tx_thresh.wthresh = tx_thresh.wthresh;
122         tx_conf.tx_free_thresh = 0;
123         tx_conf.tx_rs_thresh = 0;
124         tx_conf.txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS | ETH_TXQ_FLAGS_NOOFFLOADS;
125
126         /* init port */
127         RTE_LOG(INFO, APP, "Initializing port %"PRIu8"... ", portid);
128         fflush(stdout);
129         ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
130         if (ret < 0)
131                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
132                                 "err=%d, port=%"PRIu8"\n", ret, portid);
133
134         /* init one RX queue */
135         fflush(stdout);
136         ret = rte_eth_rx_queue_setup(portid, 0, (uint16_t)ring_conf.rx_size,
137                 rte_eth_dev_socket_id(portid), &rx_conf, mp);
138         if (ret < 0)
139                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
140                                 "err=%d, port=%"PRIu8"\n", ret, portid);
141
142         /* init one TX queue */
143         fflush(stdout);
144         ret = rte_eth_tx_queue_setup(portid, 0,
145                 (uint16_t)ring_conf.tx_size, rte_eth_dev_socket_id(portid), &tx_conf);
146         if (ret < 0)
147                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
148                                 "port=%"PRIu8" queue=%d\n", ret, portid, 0);
149
150         /* Start device */
151         ret = rte_eth_dev_start(portid);
152         if (ret < 0)
153                 rte_exit(EXIT_FAILURE, "rte_pmd_port_start: "
154                                 "err=%d, port=%"PRIu8"\n", ret, portid);
155
156         printf("done: ");
157
158         /* get link status */
159         rte_eth_link_get(portid, &link);
160         if (link.link_status) {
161                 printf(" Link Up - speed %u Mbps - %s\n",
162                         (uint32_t) link.link_speed,
163                         (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
164                         ("full-duplex") : ("half-duplex\n"));
165         } else {
166                 printf(" Link Down\n");
167         }
168         rte_eth_promiscuous_enable(portid);
169
170         /* mark port as initialized */
171         app_inited_port_mask |= 1u << portid;
172
173         return 0;
174 }
175
176 static struct rte_sched_subport_params subport_params[MAX_SCHED_SUBPORTS] = {
177         {
178                 .tb_rate = 1250000000,
179                 .tb_size = 1000000,
180
181                 .tc_rate = {1250000000, 1250000000, 1250000000, 1250000000},
182                 .tc_period = 10,
183         },
184 };
185
186 static struct rte_sched_pipe_params pipe_profiles[RTE_SCHED_PIPE_PROFILES_PER_PORT] = {
187         { /* Profile #0 */
188                 .tb_rate = 305175,
189                 .tb_size = 1000000,
190
191                 .tc_rate = {305175, 305175, 305175, 305175},
192                 .tc_period = 40,
193 #ifdef RTE_SCHED_SUBPORT_TC_OV
194                 .tc_ov_weight = 1,
195 #endif
196
197                 .wrr_weights = {1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1},
198         },
199 };
200
201 struct rte_sched_port_params port_params = {
202         .name = "port_scheduler_0",
203         .socket = 0, /* computed */
204         .rate = 0, /* computed */
205         .mtu = 6 + 6 + 4 + 4 + 2 + 1500,
206         .frame_overhead = RTE_SCHED_FRAME_OVERHEAD_DEFAULT,
207         .n_subports_per_port = 1,
208         .n_pipes_per_subport = 4096,
209         .qsize = {64, 64, 64, 64},
210         .pipe_profiles = pipe_profiles,
211         .n_pipe_profiles = sizeof(pipe_profiles) / sizeof(struct rte_sched_pipe_params),
212
213 #ifdef RTE_SCHED_RED
214         .red_params = {
215                 /* Traffic Class 0 Colors Green / Yellow / Red */
216                 [0][0] = {.min_th = 48, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
217                 [0][1] = {.min_th = 40, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
218                 [0][2] = {.min_th = 32, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
219
220                 /* Traffic Class 1 - Colors Green / Yellow / Red */
221                 [1][0] = {.min_th = 48, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
222                 [1][1] = {.min_th = 40, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
223                 [1][2] = {.min_th = 32, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
224
225                 /* Traffic Class 2 - Colors Green / Yellow / Red */
226                 [2][0] = {.min_th = 48, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
227                 [2][1] = {.min_th = 40, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
228                 [2][2] = {.min_th = 32, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
229
230                 /* Traffic Class 3 - Colors Green / Yellow / Red */
231                 [3][0] = {.min_th = 48, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
232                 [3][1] = {.min_th = 40, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9},
233                 [3][2] = {.min_th = 32, .max_th = 64, .maxp_inv = 10, .wq_log2 = 9}
234         }
235 #endif /* RTE_SCHED_RED */
236 };
237
238 static struct rte_sched_port *
239 app_init_sched_port(uint32_t portid, uint32_t socketid)
240 {
241         static char port_name[32]; /* static as referenced from global port_params*/
242         struct rte_eth_link link;
243         struct rte_sched_port *port = NULL;
244         uint32_t pipe, subport;
245         int err;
246
247         rte_eth_link_get((uint8_t)portid, &link);
248
249         port_params.socket = socketid;
250         port_params.rate = (uint64_t) link.link_speed * 1000 * 1000 / 8;
251         snprintf(port_name, sizeof(port_name), "port_%d", portid);
252         port_params.name = port_name;
253
254         port = rte_sched_port_config(&port_params);
255         if (port == NULL){
256                 rte_exit(EXIT_FAILURE, "Unable to config sched port\n");
257         }
258
259         for (subport = 0; subport < port_params.n_subports_per_port; subport ++) {
260                 err = rte_sched_subport_config(port, subport, &subport_params[subport]);
261                 if (err) {
262                         rte_exit(EXIT_FAILURE, "Unable to config sched subport %u, err=%d\n",
263                                         subport, err);
264                 }
265
266                 for (pipe = 0; pipe < port_params.n_pipes_per_subport; pipe ++) {
267                         if (app_pipe_to_profile[subport][pipe] != -1) {
268                                 err = rte_sched_pipe_config(port, subport, pipe,
269                                                 app_pipe_to_profile[subport][pipe]);
270                                 if (err) {
271                                         rte_exit(EXIT_FAILURE, "Unable to config sched pipe %u "
272                                                         "for profile %d, err=%d\n", pipe,
273                                                         app_pipe_to_profile[subport][pipe], err);
274                                 }
275                         }
276                 }
277         }
278
279         return port;
280 }
281
282 static int
283 app_load_cfg_profile(const char *profile)
284 {
285         if (profile == NULL)
286                 return 0;
287
288         struct cfg_file *cfg_file = cfg_load(profile, 0);
289         if (cfg_file == NULL)
290                 rte_exit(EXIT_FAILURE, "Cannot load configuration profile %s\n", profile);
291
292         cfg_load_port(cfg_file, &port_params);
293         cfg_load_subport(cfg_file, subport_params);
294         cfg_load_pipe(cfg_file, pipe_profiles);
295
296         cfg_close(cfg_file);
297
298         return 0;
299 }
300
301 int app_init(void)
302 {
303         uint32_t i;
304         char ring_name[MAX_NAME_LEN];
305         char pool_name[MAX_NAME_LEN];
306
307         if (rte_eth_dev_count() == 0)
308                 rte_exit(EXIT_FAILURE, "No Ethernet port - bye\n");
309
310         /* load configuration profile */
311         if (app_load_cfg_profile(cfg_profile) != 0)
312                 rte_exit(EXIT_FAILURE, "Invalid configuration profile\n");
313
314         /* Initialize each active flow */
315         for(i = 0; i < nb_pfc; i++) {
316                 uint32_t socket = rte_lcore_to_socket_id(qos_conf[i].rx_core);
317                 struct rte_ring *ring;
318
319                 snprintf(ring_name, MAX_NAME_LEN, "ring-%u-%u", i, qos_conf[i].rx_core);
320                 ring = rte_ring_lookup(ring_name);
321                 if (ring == NULL)
322                         qos_conf[i].rx_ring = rte_ring_create(ring_name, ring_conf.ring_size,
323                                 socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
324                 else
325                         qos_conf[i].rx_ring = ring;
326
327                 snprintf(ring_name, MAX_NAME_LEN, "ring-%u-%u", i, qos_conf[i].tx_core);
328                 ring = rte_ring_lookup(ring_name);
329                 if (ring == NULL)
330                         qos_conf[i].tx_ring = rte_ring_create(ring_name, ring_conf.ring_size,
331                                 socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
332                 else
333                         qos_conf[i].tx_ring = ring;
334
335
336                 /* create the mbuf pools for each RX Port */
337                 snprintf(pool_name, MAX_NAME_LEN, "mbuf_pool%u", i);
338                 qos_conf[i].mbuf_pool = rte_pktmbuf_pool_create(pool_name,
339                         mp_size, burst_conf.rx_burst * 4, 0, MBUF_DATA_SIZE,
340                         rte_eth_dev_socket_id(qos_conf[i].rx_port));
341                 if (qos_conf[i].mbuf_pool == NULL)
342                         rte_exit(EXIT_FAILURE, "Cannot init mbuf pool for socket %u\n", i);
343
344                 app_init_port(qos_conf[i].rx_port, qos_conf[i].mbuf_pool);
345                 app_init_port(qos_conf[i].tx_port, qos_conf[i].mbuf_pool);
346
347                 qos_conf[i].sched_port = app_init_sched_port(qos_conf[i].tx_port, socket);
348         }
349
350         RTE_LOG(INFO, APP, "time stamp clock running at %" PRIu64 " Hz\n",
351                          rte_get_timer_hz());
352
353         RTE_LOG(INFO, APP, "Ring sizes: NIC RX = %u, Mempool = %d SW queue = %u,"
354                          "NIC TX = %u\n", ring_conf.rx_size, mp_size, ring_conf.ring_size,
355                          ring_conf.tx_size);
356
357         RTE_LOG(INFO, APP, "Burst sizes: RX read = %hu, RX write = %hu,\n"
358                                                   "             Worker read/QoS enqueue = %hu,\n"
359                                                   "             QoS dequeue = %hu, Worker write = %hu\n",
360                 burst_conf.rx_burst, burst_conf.ring_burst, burst_conf.ring_burst,
361                 burst_conf.qos_dequeue, burst_conf.tx_burst);
362
363         RTE_LOG(INFO, APP, "NIC thresholds RX (p = %hhu, h = %hhu, w = %hhu),"
364                                  "TX (p = %hhu, h = %hhu, w = %hhu)\n",
365                 rx_thresh.pthresh, rx_thresh.hthresh, rx_thresh.wthresh,
366                 tx_thresh.pthresh, tx_thresh.hthresh, tx_thresh.wthresh);
367
368         return 0;
369 }