netdev-dpdk: Move to DPDK 1.7.0
[cascardo/ovs.git] / tests / ovs_client / ovs_client.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
35 #include <getopt.h>
36
37 #include <config.h>
38 #include <rte_config.h>
39 #include <rte_mbuf.h>
40 #include <rte_ether.h>
41 #include <rte_string_fns.h>
42 #include <rte_ip.h>
43 #include <rte_byteorder.h>
44
45 /* Number of packets to attempt to read from queue. */
46 #define PKT_READ_SIZE  ((uint16_t)32)
47
48 /* Define common names for structures shared between ovs_dpdk and client. */
49 #define MP_CLIENT_RXQ_NAME "dpdkr%u_tx"
50 #define MP_CLIENT_TXQ_NAME "dpdkr%u_rx"
51
52 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
53
54 #define BASE_10 10
55
56 /* Our client id number - tells us which rx queue to read, and tx
57  * queue to write to.
58  */
59 static uint8_t client_id = 0;
60
61 int no_pkt;
62 int pkt;
63
64 /*
65  * Given the rx queue name template above, get the queue name.
66  */
67 static inline const char *
68 get_rx_queue_name(unsigned id)
69 {
70     /* Buffer for return value. Size calculated by %u being replaced
71      * by maximum 3 digits (plus an extra byte for safety).
72      */
73     static char buffer[sizeof(MP_CLIENT_RXQ_NAME) + 2];
74
75     snprintf(buffer, sizeof(buffer) - 1, MP_CLIENT_RXQ_NAME, id);
76     return buffer;
77 }
78
79 /*
80  * Given the tx queue name template above, get the queue name.
81  */
82 static inline const char *
83 get_tx_queue_name(unsigned id)
84 {
85     /* Buffer for return value. Size calculated by %u being replaced
86      * by maximum 3 digits (plus an extra byte for safety).
87      */
88     static char buffer[sizeof(MP_CLIENT_TXQ_NAME) + 2];
89
90     snprintf(buffer, sizeof(buffer) - 1, MP_CLIENT_TXQ_NAME, id);
91     return buffer;
92 }
93
94 /*
95  * Print a usage message.
96  */
97 static void
98 usage(const char *progname)
99 {
100     printf("\nUsage: %s [EAL args] -- -n <client_id>\n", progname);
101 }
102
103 /*
104  * Convert the client id number from a string to an int.
105  */
106 static int
107 parse_client_num(const char *client)
108 {
109     char *end = NULL;
110     unsigned long temp = 0;
111
112     if (client == NULL || *client == '\0') {
113         return -1;
114     }
115
116     temp = strtoul(client, &end, BASE_10);
117     /* If valid string argument is provided, terminating '/0' character
118      * is stored in 'end'. */
119     if (end == NULL || *end != '\0') {
120         return -1;
121     }
122
123     client_id = (uint8_t)temp;
124     return 0;
125 }
126
127 /*
128  * Parse the application arguments to the client app.
129  */
130 static int
131 parse_app_args(int argc, char *argv[])
132 {
133     int option_index = 0, opt = 0;
134     char **argvopt = argv;
135     const char *progname = NULL;
136     static struct option lgopts[] = {
137         {NULL, 0, 0, 0 }
138     };
139     progname = argv[0];
140
141     while ((opt = getopt_long(argc, argvopt, "n:", lgopts,
142         &option_index)) != EOF) {
143         switch (opt) {
144             case 'n':
145                 if (parse_client_num(optarg) != 0) {
146                     usage(progname);
147                     return -1;
148                 }
149                 break;
150             default:
151                 usage(progname);
152                 return -1;
153         }
154     }
155
156     return 0;
157 }
158
159 /*
160  * Application main function - loops through
161  * receiving and processing packets. Never returns
162  */
163 int
164 main(int argc, char *argv[])
165 {
166     struct rte_ring *rx_ring = NULL;
167     struct rte_ring *tx_ring = NULL;
168     int retval = 0;
169     void *pkts[PKT_READ_SIZE];
170     int rslt = 0;
171
172     if ((retval = rte_eal_init(argc, argv)) < 0) {
173         return -1;
174     }
175
176     argc -= retval;
177     argv += retval;
178
179     if (parse_app_args(argc, argv) < 0) {
180         rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
181     }
182
183     rx_ring = rte_ring_lookup(get_rx_queue_name(client_id));
184     if (rx_ring == NULL) {
185         rte_exit(EXIT_FAILURE,
186             "Cannot get RX ring - is server process running?\n");
187     }
188
189     tx_ring = rte_ring_lookup(get_tx_queue_name(client_id));
190     if (tx_ring == NULL) {
191         rte_exit(EXIT_FAILURE,
192             "Cannot get TX ring - is server process running?\n");
193     }
194
195     RTE_LOG(INFO, APP, "Finished Process Init.\n");
196
197     printf("\nClient process %d handling packets\n", client_id);
198     printf("[Press Ctrl-C to quit ...]\n");
199
200     for (;;) {
201         unsigned rx_pkts = PKT_READ_SIZE;
202
203         /* Try dequeuing max possible packets first, if that fails, get the
204          * most we can. Loop body should only execute once, maximum.
205          */
206         while (unlikely(rte_ring_dequeue_bulk(rx_ring, pkts, rx_pkts) != 0) &&
207             rx_pkts > 0) {
208             rx_pkts = (uint16_t)RTE_MIN(rte_ring_count(rx_ring), PKT_READ_SIZE);
209         }
210
211         if (rx_pkts > 0) {
212             pkt++;
213             /* blocking enqueue */
214             do {
215                 rslt = rte_ring_enqueue_bulk(tx_ring, pkts, rx_pkts);
216             } while (rslt == -ENOBUFS);
217         } else {
218                no_pkt++;
219         }
220
221         if (!(pkt %  100000)) {
222             printf("pkt %d %d\n", pkt, no_pkt);
223             pkt = no_pkt = 0;
224         }
225     }
226 }