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