Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at:
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #include <config.h>
18 : : #include <arpa/inet.h>
19 : : #include <errno.h>
20 : : #include <inttypes.h>
21 : : #include <sys/socket.h>
22 : : #include <net/if.h>
23 : : #include <netinet/in.h>
24 : : #include <stdarg.h>
25 : : #include <stdlib.h>
26 : : #include <string.h>
27 : : #include <unistd.h>
28 : :
29 : : #include "command-line.h"
30 : : #include "compiler.h"
31 : : #include "ct-dpif.h"
32 : : #include "dirs.h"
33 : : #include "dpctl.h"
34 : : #include "dpif.h"
35 : : #include "openvswitch/dynamic-string.h"
36 : : #include "flow.h"
37 : : #include "openvswitch/match.h"
38 : : #include "netdev.h"
39 : : #include "netdev-dpdk.h"
40 : : #include "netlink.h"
41 : : #include "odp-util.h"
42 : : #include "openvswitch/ofpbuf.h"
43 : : #include "ovs-numa.h"
44 : : #include "packets.h"
45 : : #include "openvswitch/shash.h"
46 : : #include "simap.h"
47 : : #include "smap.h"
48 : : #include "sset.h"
49 : : #include "timeval.h"
50 : : #include "unixctl.h"
51 : : #include "util.h"
52 : : #include "openvswitch/ofp-parse.h"
53 : :
54 : : typedef int dpctl_command_handler(int argc, const char *argv[],
55 : : struct dpctl_params *);
56 : : struct dpctl_command {
57 : : const char *name;
58 : : const char *usage;
59 : : int min_args;
60 : : int max_args;
61 : : dpctl_command_handler *handler;
62 : : enum { DP_RO, DP_RW} mode;
63 : : };
64 : : static const struct dpctl_command *get_all_dpctl_commands(void);
65 : : static void dpctl_print(struct dpctl_params *dpctl_p, const char *fmt, ...)
66 : : OVS_PRINTF_FORMAT(2, 3);
67 : : static void dpctl_error(struct dpctl_params* dpctl_p, int err_no,
68 : : const char *fmt, ...)
69 : : OVS_PRINTF_FORMAT(3, 4);
70 : :
71 : : static void
72 : 1480 : dpctl_puts(struct dpctl_params *dpctl_p, bool error, const char *string)
73 : : {
74 : 1480 : dpctl_p->output(dpctl_p->aux, error, string);
75 : 1480 : }
76 : :
77 : : static void
78 : 1213 : dpctl_print(struct dpctl_params *dpctl_p, const char *fmt, ...)
79 : : {
80 : : char *string;
81 : : va_list args;
82 : :
83 : 1213 : va_start(args, fmt);
84 : 1213 : string = xvasprintf(fmt, args);
85 : 1213 : va_end(args);
86 : :
87 : 1213 : dpctl_puts(dpctl_p, false, string);
88 : 1213 : free(string);
89 : 1213 : }
90 : :
91 : : static void
92 : 9 : dpctl_error(struct dpctl_params* dpctl_p, int err_no, const char *fmt, ...)
93 : : {
94 : 9 : const char *subprogram_name = get_subprogram_name();
95 : 9 : struct ds ds = DS_EMPTY_INITIALIZER;
96 : 9 : int save_errno = errno;
97 : : va_list args;
98 : :
99 : :
100 [ - + ]: 9 : if (subprogram_name[0]) {
101 : 0 : ds_put_format(&ds, "%s(%s): ", program_name,subprogram_name);
102 : : } else {
103 : 9 : ds_put_format(&ds, "%s: ", program_name);
104 : : }
105 : :
106 : 9 : va_start(args, fmt);
107 : 9 : ds_put_format_valist(&ds, fmt, args);
108 : 9 : va_end(args);
109 : :
110 [ + + ]: 9 : if (err_no != 0) {
111 : 5 : ds_put_format(&ds, " (%s)", ovs_retval_to_string(err_no));
112 : : }
113 : 9 : ds_put_cstr(&ds, "\n");
114 : :
115 : 9 : dpctl_puts(dpctl_p, true, ds_cstr(&ds));
116 : :
117 : 9 : ds_destroy(&ds);
118 : :
119 : 9 : errno = save_errno;
120 : 9 : }
121 : :
122 : : static int dpctl_add_if(int argc, const char *argv[], struct dpctl_params *);
123 : :
124 : : static int
125 : 1 : if_up(struct netdev *netdev)
126 : : {
127 : 1 : return netdev_turn_flags_on(netdev, NETDEV_UP, NULL);
128 : : }
129 : :
130 : : /* Retrieve the name of the datapath if exactly one exists. The caller
131 : : * is responsible for freeing the returned string. If there is not one
132 : : * datapath, aborts with an error message. */
133 : : static char *
134 : 90 : get_one_dp(struct dpctl_params *dpctl_p)
135 : : {
136 : : struct sset types;
137 : : const char *type;
138 : 90 : char *dp_name = NULL;
139 : 90 : size_t count = 0;
140 : :
141 : 90 : sset_init(&types);
142 : 90 : dp_enumerate_types(&types);
143 [ + - ][ + + ]: 252 : SSET_FOR_EACH (type, &types) {
[ + + ]
144 : : struct sset names;
145 : :
146 : 162 : sset_init(&names);
147 [ + - ]: 162 : if (!dp_enumerate_names(type, &names)) {
148 : 162 : count += sset_count(&names);
149 [ + + ][ + + ]: 162 : if (!dp_name && count == 1) {
150 [ + - ]: 90 : dp_name = xasprintf("%s@%s", type, SSET_FIRST(&names));
151 : : }
152 : : }
153 : 162 : sset_destroy(&names);
154 : : }
155 : 90 : sset_destroy(&types);
156 : :
157 [ - + ]: 90 : if (!count) {
158 : 0 : dpctl_error(dpctl_p, 0, "no datapaths exist");
159 [ - + ]: 90 : } else if (count > 1) {
160 : 0 : dpctl_error(dpctl_p, 0, "multiple datapaths, specify one");
161 : 0 : free(dp_name);
162 : 0 : dp_name = NULL;
163 : : }
164 : :
165 : 90 : return dp_name;
166 : : }
167 : :
168 : : static int
169 : 176 : parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
170 : : {
171 : : int result;
172 : : char *name, *type;
173 : :
174 : 176 : dp_parse_name(arg_, &name, &type);
175 : :
176 [ + + ]: 176 : if (create) {
177 : 3 : result = dpif_create(name, type, dpifp);
178 : : } else {
179 : 173 : result = dpif_open(name, type, dpifp);
180 : : }
181 : :
182 : 176 : free(name);
183 : 176 : free(type);
184 : 176 : return result;
185 : : }
186 : :
187 : : static int
188 : 3 : dpctl_add_dp(int argc, const char *argv[],
189 : : struct dpctl_params *dpctl_p)
190 : : {
191 : : struct dpif *dpif;
192 : : int error;
193 : :
194 : 3 : error = parsed_dpif_open(argv[1], true, &dpif);
195 [ + + ]: 3 : if (error) {
196 : 1 : dpctl_error(dpctl_p, error, "add_dp");
197 : 1 : return error;
198 : : }
199 : 2 : dpif_close(dpif);
200 [ - + ]: 2 : if (argc > 2) {
201 : 0 : error = dpctl_add_if(argc, argv, dpctl_p);
202 : : }
203 : 3 : return error;
204 : : }
205 : :
206 : : static int
207 : 67 : dpctl_del_dp(int argc OVS_UNUSED, const char *argv[],
208 : : struct dpctl_params *dpctl_p)
209 : : {
210 : : struct dpif *dpif;
211 : : int error;
212 : :
213 : 67 : error = parsed_dpif_open(argv[1], false, &dpif);
214 [ + + ]: 67 : if (error) {
215 : 1 : dpctl_error(dpctl_p, error, "opening datapath");
216 : 1 : return error;
217 : : }
218 : 66 : error = dpif_delete(dpif);
219 [ - + ]: 66 : if (error) {
220 : 0 : dpctl_error(dpctl_p, error, "del_dp");
221 : : }
222 : :
223 : 66 : dpif_close(dpif);
224 : 67 : return error;
225 : : }
226 : :
227 : : static int
228 : 2 : dpctl_add_if(int argc OVS_UNUSED, const char *argv[],
229 : : struct dpctl_params *dpctl_p)
230 : : {
231 : : struct dpif *dpif;
232 : 2 : int i, error, lasterror = 0;
233 : :
234 : 2 : error = parsed_dpif_open(argv[1], false, &dpif);
235 [ - + ]: 2 : if (error) {
236 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
237 : 0 : return error;
238 : : }
239 [ + + ]: 4 : for (i = 2; i < argc; i++) {
240 : : const char *name, *type;
241 : 2 : char *save_ptr = NULL, *argcopy;
242 : 2 : struct netdev *netdev = NULL;
243 : : struct smap args;
244 : 2 : odp_port_t port_no = ODPP_NONE;
245 : : char *option;
246 : :
247 : 2 : argcopy = xstrdup(argv[i]);
248 : 2 : name = strtok_r(argcopy, ",", &save_ptr);
249 : 2 : type = "system";
250 : :
251 [ - + ]: 2 : if (!name) {
252 : 0 : dpctl_error(dpctl_p, 0, "%s is not a valid network device name",
253 : 0 : argv[i]);
254 : 0 : error = EINVAL;
255 : 0 : goto next;
256 : : }
257 : :
258 : 2 : smap_init(&args);
259 [ + + ]: 5 : while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
260 : 3 : char *save_ptr_2 = NULL;
261 : : char *key, *value;
262 : :
263 : 3 : key = strtok_r(option, "=", &save_ptr_2);
264 : 3 : value = strtok_r(NULL, "", &save_ptr_2);
265 [ - + ]: 3 : if (!value) {
266 : 0 : value = "";
267 : : }
268 : :
269 [ + + ]: 3 : if (!strcmp(key, "type")) {
270 : 2 : type = value;
271 [ + - ]: 1 : } else if (!strcmp(key, "port_no")) {
272 : 1 : port_no = u32_to_odp(atoi(value));
273 [ # # ]: 0 : } else if (!smap_add_once(&args, key, value)) {
274 : 3 : dpctl_error(dpctl_p, 0, "duplicate \"%s\" option", key);
275 : : }
276 : : }
277 : :
278 : 2 : error = netdev_open(name, type, &netdev);
279 [ - + ]: 2 : if (error) {
280 : 0 : dpctl_error(dpctl_p, error, "%s: failed to open network device",
281 : : name);
282 : 0 : goto next_destroy_args;
283 : : }
284 : :
285 : 2 : error = netdev_set_config(netdev, &args, NULL);
286 [ - + ]: 2 : if (error) {
287 : 0 : goto next_destroy_args;
288 : : }
289 : :
290 : 2 : error = dpif_port_add(dpif, netdev, &port_no);
291 [ + + ]: 2 : if (error) {
292 : 1 : dpctl_error(dpctl_p, error, "adding %s to %s failed", name,
293 : 1 : argv[1]);
294 : 1 : goto next_destroy_args;
295 : : }
296 : :
297 : 1 : error = if_up(netdev);
298 [ - + ]: 1 : if (error) {
299 : 0 : dpctl_error(dpctl_p, error, "%s: failed bringing interface up",
300 : : name);
301 : : }
302 : :
303 : : next_destroy_args:
304 : 2 : netdev_close(netdev);
305 : 2 : smap_destroy(&args);
306 : : next:
307 : 2 : free(argcopy);
308 [ + + ]: 2 : if (error) {
309 : 1 : lasterror = error;
310 : : }
311 : : }
312 : 2 : dpif_close(dpif);
313 : :
314 : 2 : return lasterror;
315 : : }
316 : :
317 : : static int
318 : 4 : dpctl_set_if(int argc, const char *argv[], struct dpctl_params *dpctl_p)
319 : : {
320 : : struct dpif *dpif;
321 : 4 : int i, error, lasterror = 0;
322 : :
323 : 4 : error = parsed_dpif_open(argv[1], false, &dpif);
324 [ - + ]: 4 : if (error) {
325 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
326 : 0 : return error;
327 : : }
328 [ + + ]: 8 : for (i = 2; i < argc; i++) {
329 : 4 : struct netdev *netdev = NULL;
330 : : struct dpif_port dpif_port;
331 : 4 : char *save_ptr = NULL;
332 : 4 : char *type = NULL;
333 : : char *argcopy;
334 : : const char *name;
335 : : struct smap args;
336 : : odp_port_t port_no;
337 : : char *option;
338 : 4 : int error = 0;
339 : :
340 : 4 : argcopy = xstrdup(argv[i]);
341 : 4 : name = strtok_r(argcopy, ",", &save_ptr);
342 [ - + ]: 4 : if (!name) {
343 : 0 : dpctl_error(dpctl_p, 0, "%s is not a valid network device name",
344 : 0 : argv[i]);
345 : 0 : goto next;
346 : : }
347 : :
348 : : /* Get the port's type from the datapath. */
349 : 4 : error = dpif_port_query_by_name(dpif, name, &dpif_port);
350 [ - + ]: 4 : if (error) {
351 : 0 : dpctl_error(dpctl_p, error, "%s: failed to query port in %s", name,
352 : 0 : argv[1]);
353 : 0 : goto next;
354 : : }
355 : 4 : type = xstrdup(dpif_port.type);
356 : 4 : port_no = dpif_port.port_no;
357 : 4 : dpif_port_destroy(&dpif_port);
358 : :
359 : : /* Retrieve its existing configuration. */
360 : 4 : error = netdev_open(name, type, &netdev);
361 [ - + ]: 4 : if (error) {
362 : 0 : dpctl_error(dpctl_p, error, "%s: failed to open network device",
363 : : name);
364 : 0 : goto next;
365 : : }
366 : :
367 : 4 : smap_init(&args);
368 : 4 : error = netdev_get_config(netdev, &args);
369 [ - + ]: 4 : if (error) {
370 : 0 : dpctl_error(dpctl_p, error, "%s: failed to fetch configuration",
371 : : name);
372 : 0 : goto next_destroy_args;
373 : : }
374 : :
375 : : /* Parse changes to configuration. */
376 [ + + ]: 6 : while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
377 : 4 : char *save_ptr_2 = NULL;
378 : : char *key, *value;
379 : :
380 : 4 : key = strtok_r(option, "=", &save_ptr_2);
381 : 4 : value = strtok_r(NULL, "", &save_ptr_2);
382 [ - + ]: 4 : if (!value) {
383 : 0 : value = "";
384 : : }
385 : :
386 [ + + ]: 4 : if (!strcmp(key, "type")) {
387 [ + + ]: 3 : if (strcmp(value, type)) {
388 : 2 : dpctl_error(dpctl_p, 0,
389 : : "%s: can't change type from %s to %s",
390 : : name, type, value);
391 : 2 : error = EINVAL;
392 : 2 : goto next_destroy_args;
393 : : }
394 [ + - ]: 1 : } else if (!strcmp(key, "port_no")) {
395 [ - + ]: 1 : if (port_no != u32_to_odp(atoi(value))) {
396 : 0 : dpctl_error(dpctl_p, 0, "%s: can't change port number from"
397 : : " %"PRIu32" to %d", name, port_no, atoi(value));
398 : 0 : error = EINVAL;
399 : 0 : goto next_destroy_args;
400 : : }
401 [ # # ]: 0 : } else if (value[0] == '\0') {
402 : 0 : smap_remove(&args, key);
403 : : } else {
404 : 2 : smap_replace(&args, key, value);
405 : : }
406 : : }
407 : :
408 : : /* Update configuration. */
409 : 2 : char *err_s = NULL;
410 : 2 : error = netdev_set_config(netdev, &args, &err_s);
411 [ + - ][ - + ]: 2 : if (err_s || error) {
412 [ # # ]: 0 : dpctl_error(dpctl_p, error, "%s",
413 : 0 : err_s ? err_s : "Error updating configuration");
414 : 0 : free(err_s);
415 : : }
416 [ - + ]: 2 : if (error) {
417 : 0 : goto next_destroy_args;
418 : : }
419 : :
420 : : next_destroy_args:
421 : 4 : smap_destroy(&args);
422 : : next:
423 : 4 : netdev_close(netdev);
424 : 4 : free(type);
425 : 4 : free(argcopy);
426 [ + + ]: 4 : if (error) {
427 : 2 : lasterror = error;
428 : : }
429 : : }
430 : 4 : dpif_close(dpif);
431 : :
432 : 4 : return lasterror;
433 : : }
434 : :
435 : : static bool
436 : 5 : get_port_number(struct dpif *dpif, const char *name, odp_port_t *port,
437 : : struct dpctl_params *dpctl_p)
438 : : {
439 : : struct dpif_port dpif_port;
440 : :
441 [ + + ]: 5 : if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
442 : 3 : *port = dpif_port.port_no;
443 : 3 : dpif_port_destroy(&dpif_port);
444 : 3 : return true;
445 : : } else {
446 : 2 : dpctl_error(dpctl_p, 0, "no port named %s", name);
447 : 5 : return false;
448 : : }
449 : : }
450 : :
451 : : static int
452 : 6 : dpctl_del_if(int argc, const char *argv[], struct dpctl_params *dpctl_p)
453 : : {
454 : : struct dpif *dpif;
455 : 6 : int i, error, lasterror = 0;
456 : :
457 : 6 : error = parsed_dpif_open(argv[1], false, &dpif);
458 [ + + ]: 6 : if (error) {
459 : 1 : dpctl_error(dpctl_p, error, "opening datapath");
460 : 1 : return error;
461 : : }
462 [ + + ]: 10 : for (i = 2; i < argc; i++) {
463 : 5 : const char *name = argv[i];
464 : : odp_port_t port;
465 : :
466 [ - + ]: 5 : if (!name[strspn(name, "0123456789")]) {
467 : 0 : port = u32_to_odp(atoi(name));
468 [ + + ]: 5 : } else if (!get_port_number(dpif, name, &port, dpctl_p)) {
469 : 2 : lasterror = ENOENT;
470 : 2 : continue;
471 : : }
472 : :
473 : 3 : error = dpif_port_del(dpif, port);
474 [ + + ]: 3 : if (error) {
475 : 1 : dpctl_error(dpctl_p, error, "deleting port %s from %s failed",
476 : 1 : name, argv[1]);
477 : 3 : lasterror = error;
478 : : }
479 : : }
480 : 5 : dpif_close(dpif);
481 : 6 : return lasterror;
482 : : }
483 : :
484 : : static void
485 : 0 : print_stat(struct dpctl_params *dpctl_p, const char *leader, uint64_t value)
486 : : {
487 : 0 : dpctl_print(dpctl_p, "%s", leader);
488 [ # # ]: 0 : if (value != UINT64_MAX) {
489 : 0 : dpctl_print(dpctl_p, "%"PRIu64, value);
490 : : } else {
491 : 0 : dpctl_print(dpctl_p, "?");
492 : : }
493 : 0 : }
494 : :
495 : : static void
496 : 0 : print_human_size(struct dpctl_params *dpctl_p, uint64_t value)
497 : : {
498 [ # # ]: 0 : if (value == UINT64_MAX) {
499 : : /* Nothing to do. */
500 [ # # ]: 0 : } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
501 : 0 : dpctl_print(dpctl_p, " (%.1f TiB)",
502 : : value / (1024.0 * 1024 * 1024 * 1024));
503 [ # # ]: 0 : } else if (value >= 1024ULL * 1024 * 1024) {
504 : 0 : dpctl_print(dpctl_p, " (%.1f GiB)", value / (1024.0 * 1024 * 1024));
505 [ # # ]: 0 : } else if (value >= 1024ULL * 1024) {
506 : 0 : dpctl_print(dpctl_p, " (%.1f MiB)", value / (1024.0 * 1024));
507 [ # # ]: 0 : } else if (value >= 1024) {
508 : 0 : dpctl_print(dpctl_p, " (%.1f KiB)", value / 1024.0);
509 : : }
510 : 0 : }
511 : :
512 : : /* qsort comparison function. */
513 : : static int
514 : 1 : compare_port_nos(const void *a_, const void *b_)
515 : : {
516 : 1 : const odp_port_t *ap = a_;
517 : 1 : const odp_port_t *bp = b_;
518 : 1 : uint32_t a = odp_to_u32(*ap);
519 : 1 : uint32_t b = odp_to_u32(*bp);
520 : :
521 [ - + ]: 1 : return a < b ? -1 : a > b;
522 : : }
523 : :
524 : : static void
525 : 4 : show_dpif(struct dpif *dpif, struct dpctl_params *dpctl_p)
526 : : {
527 : : struct dpif_port_dump dump;
528 : : struct dpif_port dpif_port;
529 : : struct dpif_dp_stats stats;
530 : : struct netdev *netdev;
531 : :
532 : 4 : dpctl_print(dpctl_p, "%s:\n", dpif_name(dpif));
533 [ + - ]: 4 : if (!dpif_get_dp_stats(dpif, &stats)) {
534 : 4 : dpctl_print(dpctl_p, "\tlookups: hit:%"PRIu64" missed:%"PRIu64
535 : : " lost:%"PRIu64"\n\tflows: %"PRIu64"\n",
536 : : stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
537 [ - + ]: 4 : if (stats.n_masks != UINT32_MAX) {
538 : 0 : uint64_t n_pkts = stats.n_hit + stats.n_missed;
539 [ # # ]: 0 : double avg = n_pkts ? (double) stats.n_mask_hit / n_pkts : 0.0;
540 : :
541 : 0 : dpctl_print(dpctl_p, "\tmasks: hit:%"PRIu64" total:%"PRIu32
542 : : " hit/pkt:%.2f\n",
543 : : stats.n_mask_hit, stats.n_masks, avg);
544 : : }
545 : : }
546 : :
547 : 4 : odp_port_t *port_nos = NULL;
548 : 4 : size_t allocated_port_nos = 0, n_port_nos = 0;
549 [ + + ][ + + ]: 9 : DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
550 [ + - ]: 5 : if (n_port_nos >= allocated_port_nos) {
551 : 5 : port_nos = x2nrealloc(port_nos, &allocated_port_nos,
552 : : sizeof *port_nos);
553 : : }
554 : :
555 : 5 : port_nos[n_port_nos] = dpif_port.port_no;
556 : 5 : n_port_nos++;
557 : : }
558 : :
559 : 4 : qsort(port_nos, n_port_nos, sizeof *port_nos, compare_port_nos);
560 : :
561 [ + + ]: 9 : for (int i = 0; i < n_port_nos; i++) {
562 [ - + ]: 5 : if (dpif_port_query_by_number(dpif, port_nos[i], &dpif_port)) {
563 : 0 : continue;
564 : : }
565 : :
566 : 5 : dpctl_print(dpctl_p, "\tport %u: %s",
567 : : dpif_port.port_no, dpif_port.name);
568 : :
569 [ + - ]: 5 : if (strcmp(dpif_port.type, "system")) {
570 : : int error;
571 : :
572 : 5 : dpctl_print(dpctl_p, " (%s", dpif_port.type);
573 : :
574 : 5 : error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
575 [ + - ]: 5 : if (!error) {
576 : : struct smap config;
577 : :
578 : 5 : smap_init(&config);
579 : 5 : error = netdev_get_config(netdev, &config);
580 [ + - ]: 5 : if (!error) {
581 : 5 : const struct smap_node **nodes = smap_sort(&config);
582 [ - + ]: 5 : for (size_t j = 0; j < smap_count(&config); j++) {
583 : 0 : const struct smap_node *node = nodes[j];
584 [ # # ]: 0 : dpctl_print(dpctl_p, "%c %s=%s", j ? ',' : ':',
585 : : node->key, node->value);
586 : : }
587 : 5 : free(nodes);
588 : : } else {
589 : 0 : dpctl_print(dpctl_p, ", could not retrieve configuration "
590 : : "(%s)", ovs_strerror(error));
591 : : }
592 : 5 : smap_destroy(&config);
593 : :
594 : 5 : netdev_close(netdev);
595 : : } else {
596 : 0 : dpctl_print(dpctl_p, ": open failed (%s)",
597 : : ovs_strerror(error));
598 : : }
599 : 5 : dpctl_print(dpctl_p, ")");
600 : : }
601 : 5 : dpctl_print(dpctl_p, "\n");
602 : :
603 [ - + ]: 5 : if (dpctl_p->print_statistics) {
604 : : struct netdev_stats s;
605 : : int error;
606 : :
607 : 0 : error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
608 [ # # ]: 0 : if (error) {
609 : 0 : dpctl_print(dpctl_p, ", open failed (%s)",
610 : : ovs_strerror(error));
611 : 0 : dpif_port_destroy(&dpif_port);
612 : 0 : continue;
613 : : }
614 : 0 : error = netdev_get_stats(netdev, &s);
615 [ # # ]: 0 : if (!error) {
616 : 0 : netdev_close(netdev);
617 : 0 : print_stat(dpctl_p, "\t\tRX packets:", s.rx_packets);
618 : 0 : print_stat(dpctl_p, " errors:", s.rx_errors);
619 : 0 : print_stat(dpctl_p, " dropped:", s.rx_dropped);
620 : 0 : print_stat(dpctl_p, " overruns:", s.rx_over_errors);
621 : 0 : print_stat(dpctl_p, " frame:", s.rx_frame_errors);
622 : 0 : dpctl_print(dpctl_p, "\n");
623 : :
624 : 0 : print_stat(dpctl_p, "\t\tTX packets:", s.tx_packets);
625 : 0 : print_stat(dpctl_p, " errors:", s.tx_errors);
626 : 0 : print_stat(dpctl_p, " dropped:", s.tx_dropped);
627 : 0 : print_stat(dpctl_p, " aborted:", s.tx_aborted_errors);
628 : 0 : print_stat(dpctl_p, " carrier:", s.tx_carrier_errors);
629 : 0 : dpctl_print(dpctl_p, "\n");
630 : :
631 : 0 : print_stat(dpctl_p, "\t\tcollisions:", s.collisions);
632 : 0 : dpctl_print(dpctl_p, "\n");
633 : :
634 : 0 : print_stat(dpctl_p, "\t\tRX bytes:", s.rx_bytes);
635 : 0 : print_human_size(dpctl_p, s.rx_bytes);
636 : 0 : print_stat(dpctl_p, " TX bytes:", s.tx_bytes);
637 : 0 : print_human_size(dpctl_p, s.tx_bytes);
638 : 0 : dpctl_print(dpctl_p, "\n");
639 : : } else {
640 : 0 : dpctl_print(dpctl_p, ", could not retrieve stats (%s)",
641 : : ovs_strerror(error));
642 : : }
643 : : }
644 : 5 : dpif_port_destroy(&dpif_port);
645 : : }
646 : :
647 : 4 : free(port_nos);
648 : 4 : }
649 : :
650 : : typedef void (*dps_for_each_cb)(struct dpif *, struct dpctl_params *);
651 : :
652 : : static int
653 : 5 : dps_for_each(struct dpctl_params *dpctl_p, dps_for_each_cb cb)
654 : : {
655 : 5 : struct sset dpif_names = SSET_INITIALIZER(&dpif_names),
656 : 5 : dpif_types = SSET_INITIALIZER(&dpif_types);
657 : 5 : int error, openerror = 0, enumerror = 0;
658 : : const char *type, *name;
659 : 5 : bool at_least_one = false;
660 : :
661 : 5 : dp_enumerate_types(&dpif_types);
662 : :
663 [ + - ][ + + ]: 15 : SSET_FOR_EACH (type, &dpif_types) {
[ + + ]
664 : 10 : error = dp_enumerate_names(type, &dpif_names);
665 [ - + ]: 10 : if (error) {
666 : 0 : enumerror = error;
667 : : }
668 : :
669 [ + + ][ - + ]: 15 : SSET_FOR_EACH (name, &dpif_names) {
[ + + ]
670 : : struct dpif *dpif;
671 : :
672 : 5 : at_least_one = true;
673 : 5 : error = dpif_open(name, type, &dpif);
674 [ + - ]: 5 : if (!error) {
675 : 5 : cb(dpif, dpctl_p);
676 : 5 : dpif_close(dpif);
677 : : } else {
678 : 0 : openerror = error;
679 : 0 : dpctl_error(dpctl_p, error, "opening datapath %s failed",
680 : : name);
681 : : }
682 : : }
683 : : }
684 : :
685 : 5 : sset_destroy(&dpif_names);
686 : 5 : sset_destroy(&dpif_types);
687 : :
688 : : /* If there has been an error while opening a datapath it should be
689 : : * reported. Otherwise, we want to ignore the errors generated by
690 : : * dp_enumerate_names() if at least one datapath has been discovered,
691 : : * because they're not interesting for the user. This happens, for
692 : : * example, if OVS is using a userspace datapath and the kernel module
693 : : * is not loaded. */
694 [ - + ]: 5 : if (openerror) {
695 : 0 : return openerror;
696 : : } else {
697 [ + - ]: 5 : return at_least_one ? 0 : enumerror;
698 : : }
699 : : }
700 : :
701 : : static int
702 : 4 : dpctl_show(int argc, const char *argv[], struct dpctl_params *dpctl_p)
703 : : {
704 : 4 : int error, lasterror = 0;
705 [ + - ]: 4 : if (argc > 1) {
706 : : int i;
707 [ + + ]: 8 : for (i = 1; i < argc; i++) {
708 : 4 : const char *name = argv[i];
709 : : struct dpif *dpif;
710 : :
711 : 4 : error = parsed_dpif_open(name, false, &dpif);
712 [ + - ]: 4 : if (!error) {
713 : 4 : show_dpif(dpif, dpctl_p);
714 : 4 : dpif_close(dpif);
715 : : } else {
716 : 0 : dpctl_error(dpctl_p, error, "opening datapath %s failed",
717 : : name);
718 : 0 : lasterror = error;
719 : : }
720 : : }
721 : : } else {
722 : 0 : lasterror = dps_for_each(dpctl_p, show_dpif);
723 : : }
724 : :
725 : 4 : return lasterror;
726 : : }
727 : :
728 : : static void
729 : 5 : dump_cb(struct dpif *dpif, struct dpctl_params *dpctl_p)
730 : : {
731 : 5 : dpctl_print(dpctl_p, "%s\n", dpif_name(dpif));
732 : 5 : }
733 : :
734 : : static int
735 : 5 : dpctl_dump_dps(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
736 : : struct dpctl_params *dpctl_p)
737 : : {
738 : 5 : return dps_for_each(dpctl_p, dump_cb);
739 : : }
740 : :
741 : : static void
742 : 26 : format_dpif_flow(struct ds *ds, const struct dpif_flow *f, struct hmap *ports,
743 : : struct dpctl_params *dpctl_p)
744 : : {
745 [ - + ][ # # ]: 26 : if (dpctl_p->verbosity && f->ufid_present) {
746 : 0 : odp_format_ufid(&f->ufid, ds);
747 : 0 : ds_put_cstr(ds, ", ");
748 : : }
749 : 26 : odp_flow_format(f->key, f->key_len, f->mask, f->mask_len, ports, ds,
750 : 26 : dpctl_p->verbosity);
751 : 26 : ds_put_cstr(ds, ", ");
752 : :
753 : 26 : dpif_flow_stats_format(&f->stats, ds);
754 : 26 : ds_put_cstr(ds, ", actions:");
755 : 26 : format_odp_actions(ds, f->actions, f->actions_len);
756 : 26 : }
757 : :
758 : : static int
759 : 11 : dpctl_dump_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p)
760 : : {
761 : : struct dpif *dpif;
762 : : struct ds ds;
763 : : char *name;
764 : :
765 : 11 : char *filter = NULL;
766 : : struct flow flow_filter;
767 : : struct flow_wildcards wc_filter;
768 : :
769 : : struct dpif_port_dump port_dump;
770 : : struct dpif_port dpif_port;
771 : : struct hmap portno_names;
772 : : struct simap names_portno;
773 : :
774 : : struct dpif_flow_dump_thread *flow_dump_thread;
775 : : struct dpif_flow_dump *flow_dump;
776 : : struct dpif_flow f;
777 : 11 : int pmd_id = PMD_ID_NULL;
778 : : int error;
779 : :
780 [ - + ][ # # ]: 11 : if (argc > 1 && !strncmp(argv[argc - 1], "filter=", 7)) {
781 : 0 : filter = xstrdup(argv[--argc] + 7);
782 : : }
783 [ - + ]: 11 : name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
784 [ - + ]: 11 : if (!name) {
785 : 0 : error = EINVAL;
786 : 0 : goto out_freefilter;
787 : : }
788 : :
789 : 11 : error = parsed_dpif_open(name, false, &dpif);
790 : 11 : free(name);
791 [ - + ]: 11 : if (error) {
792 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
793 : 0 : goto out_freefilter;
794 : : }
795 : :
796 : :
797 : 11 : hmap_init(&portno_names);
798 : 11 : simap_init(&names_portno);
799 [ + + ][ + + ]: 84 : DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
800 : 73 : odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
801 : 73 : simap_put(&names_portno, dpif_port.name,
802 : : odp_to_u32(dpif_port.port_no));
803 : : }
804 : :
805 [ - + ]: 11 : if (filter) {
806 : 0 : char *err = parse_ofp_exact_flow(&flow_filter, &wc_filter, filter,
807 : : &names_portno);
808 [ # # ]: 0 : if (err) {
809 : 0 : dpctl_error(dpctl_p, 0, "Failed to parse filter (%s)", err);
810 : 0 : error = EINVAL;
811 : 0 : goto out_dpifclose;
812 : : }
813 : : }
814 : :
815 : : /* Make sure that these values are different. PMD_ID_NULL means that the
816 : : * pmd is unspecified (e.g. because the datapath doesn't have different
817 : : * pmd threads), while NON_PMD_CORE_ID refers to every non pmd threads
818 : : * in the userspace datapath */
819 : : BUILD_ASSERT(PMD_ID_NULL != NON_PMD_CORE_ID);
820 : :
821 : 11 : ds_init(&ds);
822 : 11 : flow_dump = dpif_flow_dump_create(dpif, false);
823 : 11 : flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
824 [ + + ]: 35 : while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
825 [ - + ]: 24 : if (filter) {
826 : : struct flow flow;
827 : : struct flow_wildcards wc;
828 : : struct match match, match_filter;
829 : : struct minimatch minimatch;
830 : :
831 : 0 : odp_flow_key_to_flow(f.key, f.key_len, &flow);
832 : 0 : odp_flow_key_to_mask(f.mask, f.mask_len, f.key, f.key_len,
833 : : &wc, &flow);
834 : 0 : match_init(&match, &flow, &wc);
835 : :
836 : 0 : match_init(&match_filter, &flow_filter, &wc);
837 : 0 : match_init(&match_filter, &match_filter.flow, &wc_filter);
838 : 0 : minimatch_init(&minimatch, &match_filter);
839 : :
840 [ # # ]: 0 : if (!minimatch_matches_flow(&minimatch, &match.flow)) {
841 : 0 : minimatch_destroy(&minimatch);
842 : 0 : continue;
843 : : }
844 : 0 : minimatch_destroy(&minimatch);
845 : : }
846 : 24 : ds_clear(&ds);
847 : : /* If 'pmd_id' is specified, overlapping flows could be dumped from
848 : : * different pmd threads. So, separates dumps from different pmds
849 : : * by printing a title line. */
850 [ + + ]: 24 : if (pmd_id != f.pmd_id) {
851 [ + + ]: 12 : if (f.pmd_id == NON_PMD_CORE_ID) {
852 : 10 : ds_put_format(&ds, "flow-dump from non-dpdk interfaces:\n");
853 : : } else {
854 : 2 : ds_put_format(&ds, "flow-dump from pmd on cpu core: %d\n",
855 : : f.pmd_id);
856 : : }
857 : 12 : pmd_id = f.pmd_id;
858 : : }
859 : 24 : format_dpif_flow(&ds, &f, &portno_names, dpctl_p);
860 : 24 : dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds));
861 : : }
862 : 11 : dpif_flow_dump_thread_destroy(flow_dump_thread);
863 : 11 : error = dpif_flow_dump_destroy(flow_dump);
864 : :
865 [ - + ]: 11 : if (error) {
866 : 0 : dpctl_error(dpctl_p, error, "Failed to dump flows from datapath");
867 : : }
868 : 11 : ds_destroy(&ds);
869 : :
870 : : out_dpifclose:
871 : 11 : odp_portno_names_destroy(&portno_names);
872 : 11 : simap_destroy(&names_portno);
873 : 11 : hmap_destroy(&portno_names);
874 : 11 : dpif_close(dpif);
875 : : out_freefilter:
876 : 11 : free(filter);
877 : 11 : return error;
878 : : }
879 : :
880 : : /* Extracts the in_port from the parsed keys, and returns the reference
881 : : * to the 'struct netdev *' of the dpif port. On error, returns NULL.
882 : : * Users must call 'netdev_close()' after finish using the returned
883 : : * reference. */
884 : : static struct netdev *
885 : 0 : get_in_port_netdev_from_key(struct dpif *dpif, const struct ofpbuf *key)
886 : : {
887 : : const struct nlattr *in_port_nla;
888 : 0 : struct netdev *dev = NULL;
889 : :
890 : 0 : in_port_nla = nl_attr_find(key, 0, OVS_KEY_ATTR_IN_PORT);
891 [ # # ]: 0 : if (in_port_nla) {
892 : : struct dpif_port dpif_port;
893 : : odp_port_t port_no;
894 : : int error;
895 : :
896 : 0 : port_no = ODP_PORT_C(nl_attr_get_u32(in_port_nla));
897 : 0 : error = dpif_port_query_by_number(dpif, port_no, &dpif_port);
898 [ # # ]: 0 : if (error) {
899 : 0 : goto out;
900 : : }
901 : :
902 : 0 : netdev_open(dpif_port.name, dpif_port.type, &dev);
903 : 0 : dpif_port_destroy(&dpif_port);
904 : : }
905 : :
906 : : out:
907 : 0 : return dev;
908 : : }
909 : :
910 : : static int
911 : 0 : dpctl_put_flow(int argc, const char *argv[], enum dpif_flow_put_flags flags,
912 : : struct dpctl_params *dpctl_p)
913 : : {
914 : 0 : const char *key_s = argv[argc - 2];
915 : 0 : const char *actions_s = argv[argc - 1];
916 : 0 : struct netdev *in_port_netdev = NULL;
917 : : struct dpif_flow_stats stats;
918 : : struct dpif_port dpif_port;
919 : : struct dpif_port_dump port_dump;
920 : : struct ofpbuf actions;
921 : : struct ofpbuf key;
922 : : struct ofpbuf mask;
923 : : struct dpif *dpif;
924 : : ovs_u128 ufid;
925 : : bool ufid_present;
926 : : char *dp_name;
927 : : struct simap port_names;
928 : : int n, error;
929 : :
930 [ # # ]: 0 : dp_name = argc == 4 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
931 [ # # ]: 0 : if (!dp_name) {
932 : 0 : return EINVAL;
933 : : }
934 : 0 : error = parsed_dpif_open(dp_name, false, &dpif);
935 : 0 : free(dp_name);
936 [ # # ]: 0 : if (error) {
937 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
938 : 0 : return error;
939 : : }
940 : :
941 : 0 : ufid_present = false;
942 : 0 : n = odp_ufid_from_string(key_s, &ufid);
943 [ # # ]: 0 : if (n < 0) {
944 : 0 : dpctl_error(dpctl_p, -n, "parsing flow ufid");
945 : 0 : return -n;
946 [ # # ]: 0 : } else if (n) {
947 : 0 : key_s += n;
948 : 0 : ufid_present = true;
949 : : }
950 : :
951 : 0 : simap_init(&port_names);
952 [ # # ][ # # ]: 0 : DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
953 : 0 : simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
954 : : }
955 : :
956 : 0 : ofpbuf_init(&key, 0);
957 : 0 : ofpbuf_init(&mask, 0);
958 : 0 : error = odp_flow_from_string(key_s, &port_names, &key, &mask);
959 : 0 : simap_destroy(&port_names);
960 [ # # ]: 0 : if (error) {
961 : 0 : dpctl_error(dpctl_p, error, "parsing flow key");
962 : 0 : goto out_freekeymask;
963 : : }
964 : :
965 : 0 : ofpbuf_init(&actions, 0);
966 : 0 : error = odp_actions_from_string(actions_s, NULL, &actions);
967 [ # # ]: 0 : if (error) {
968 : 0 : dpctl_error(dpctl_p, error, "parsing actions");
969 : 0 : goto out_freeactions;
970 : : }
971 : :
972 : : /* For DPDK interface, applies the operation to all pmd threads
973 : : * on the same numa node. */
974 : 0 : in_port_netdev = get_in_port_netdev_from_key(dpif, &key);
975 [ # # ][ # # ]: 0 : if (in_port_netdev && netdev_is_pmd(in_port_netdev)) {
976 : : int numa_id;
977 : :
978 : 0 : numa_id = netdev_get_numa_id(in_port_netdev);
979 [ # # ]: 0 : if (ovs_numa_numa_id_is_valid(numa_id)) {
980 : 0 : struct ovs_numa_dump *dump = ovs_numa_dump_cores_on_numa(numa_id);
981 : : struct ovs_numa_info *iter;
982 : :
983 [ # # ]: 0 : FOR_EACH_CORE_ON_NUMA (iter, dump) {
984 [ # # ]: 0 : if (ovs_numa_core_is_pinned(iter->core_id)) {
985 [ # # ][ # # ]: 0 : error = dpif_flow_put(dpif, flags,
[ # # ]
986 : 0 : key.data, key.size,
987 : 0 : mask.size == 0 ? NULL : mask.data,
988 : 0 : mask.size, actions.data,
989 : 0 : actions.size, ufid_present ? &ufid : NULL,
990 : 0 : iter->core_id, dpctl_p->print_statistics ? &stats : NULL);
991 : : }
992 : : }
993 : 0 : ovs_numa_dump_destroy(dump);
994 : : } else {
995 : 0 : error = EINVAL;
996 : : }
997 : : } else {
998 [ # # ][ # # ]: 0 : error = dpif_flow_put(dpif, flags,
[ # # ]
999 : 0 : key.data, key.size,
1000 : 0 : mask.size == 0 ? NULL : mask.data,
1001 : 0 : mask.size, actions.data,
1002 : 0 : actions.size, ufid_present ? &ufid : NULL,
1003 : 0 : PMD_ID_NULL, dpctl_p->print_statistics ? &stats : NULL);
1004 : : }
1005 [ # # ]: 0 : if (error) {
1006 : 0 : dpctl_error(dpctl_p, error, "updating flow table");
1007 : 0 : goto out_freeactions;
1008 : : }
1009 : :
1010 [ # # ]: 0 : if (dpctl_p->print_statistics) {
1011 : : struct ds s;
1012 : :
1013 : 0 : ds_init(&s);
1014 : 0 : dpif_flow_stats_format(&stats, &s);
1015 : 0 : dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1016 : 0 : ds_destroy(&s);
1017 : : }
1018 : :
1019 : : out_freeactions:
1020 : 0 : ofpbuf_uninit(&actions);
1021 : : out_freekeymask:
1022 : 0 : ofpbuf_uninit(&mask);
1023 : 0 : ofpbuf_uninit(&key);
1024 : 0 : dpif_close(dpif);
1025 : 0 : netdev_close(in_port_netdev);
1026 : 0 : return error;
1027 : : }
1028 : :
1029 : : static int
1030 : 0 : dpctl_add_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1031 : : {
1032 : 0 : return dpctl_put_flow(argc, argv, DPIF_FP_CREATE, dpctl_p);
1033 : : }
1034 : :
1035 : : static int
1036 : 0 : dpctl_mod_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1037 : : {
1038 : : enum dpif_flow_put_flags flags;
1039 : :
1040 : 0 : flags = DPIF_FP_MODIFY;
1041 [ # # ]: 0 : if (dpctl_p->may_create) {
1042 : 0 : flags |= DPIF_FP_CREATE;
1043 : : }
1044 [ # # ]: 0 : if (dpctl_p->zero_statistics) {
1045 : 0 : flags |= DPIF_FP_ZERO_STATS;
1046 : : }
1047 : :
1048 : 0 : return dpctl_put_flow(argc, argv, flags, dpctl_p);
1049 : : }
1050 : :
1051 : : static int
1052 : 2 : dpctl_get_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1053 : : {
1054 : 2 : const char *key_s = argv[argc - 1];
1055 : : struct dpif_flow flow;
1056 : : struct dpif_port dpif_port;
1057 : : struct dpif_port_dump port_dump;
1058 : : struct dpif *dpif;
1059 : : char *dp_name;
1060 : : struct hmap portno_names;
1061 : : ovs_u128 ufid;
1062 : : struct ofpbuf buf;
1063 : : uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
1064 : : struct ds ds;
1065 : : int n, error;
1066 : :
1067 [ - + ]: 2 : dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1068 [ - + ]: 2 : if (!dp_name) {
1069 : 0 : return EINVAL;
1070 : : }
1071 : 2 : error = parsed_dpif_open(dp_name, false, &dpif);
1072 : 2 : free(dp_name);
1073 [ - + ]: 2 : if (error) {
1074 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
1075 : 0 : return error;
1076 : : }
1077 : :
1078 : 2 : ofpbuf_use_stub(&buf, &stub, sizeof stub);
1079 : 2 : hmap_init(&portno_names);
1080 [ + + ][ + + ]: 12 : DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
1081 : 10 : odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
1082 : : }
1083 : :
1084 : 2 : n = odp_ufid_from_string(key_s, &ufid);
1085 [ - + ]: 2 : if (n <= 0) {
1086 : 0 : dpctl_error(dpctl_p, -n, "parsing flow ufid");
1087 : 0 : goto out;
1088 : : }
1089 : :
1090 : : /* In case of PMD will be returned flow from first PMD thread with match. */
1091 : 2 : error = dpif_flow_get(dpif, NULL, 0, &ufid, PMD_ID_NULL, &buf, &flow);
1092 [ - + ]: 2 : if (error) {
1093 : 0 : dpctl_error(dpctl_p, error, "getting flow");
1094 : 0 : goto out;
1095 : : }
1096 : :
1097 : 2 : ds_init(&ds);
1098 : 2 : format_dpif_flow(&ds, &flow, &portno_names, dpctl_p);
1099 : 2 : dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds));
1100 : 2 : ds_destroy(&ds);
1101 : :
1102 : : out:
1103 : 2 : odp_portno_names_destroy(&portno_names);
1104 : 2 : hmap_destroy(&portno_names);
1105 : 2 : ofpbuf_uninit(&buf);
1106 : 2 : dpif_close(dpif);
1107 : 2 : return error;
1108 : : }
1109 : :
1110 : : static int
1111 : 0 : dpctl_del_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1112 : : {
1113 : 0 : const char *key_s = argv[argc - 1];
1114 : 0 : struct netdev *in_port_netdev = NULL;
1115 : : struct dpif_flow_stats stats;
1116 : : struct dpif_port dpif_port;
1117 : : struct dpif_port_dump port_dump;
1118 : : struct ofpbuf key;
1119 : : struct ofpbuf mask; /* To be ignored. */
1120 : : struct dpif *dpif;
1121 : : ovs_u128 ufid;
1122 : : bool ufid_present;
1123 : : char *dp_name;
1124 : : struct simap port_names;
1125 : : int n, error;
1126 : :
1127 [ # # ]: 0 : dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1128 [ # # ]: 0 : if (!dp_name) {
1129 : 0 : return EINVAL;
1130 : : }
1131 : 0 : error = parsed_dpif_open(dp_name, false, &dpif);
1132 : 0 : free(dp_name);
1133 [ # # ]: 0 : if (error) {
1134 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
1135 : 0 : return error;
1136 : : }
1137 : :
1138 : 0 : ufid_present = false;
1139 : 0 : n = odp_ufid_from_string(key_s, &ufid);
1140 [ # # ]: 0 : if (n < 0) {
1141 : 0 : dpctl_error(dpctl_p, -n, "parsing flow ufid");
1142 : 0 : return -n;
1143 [ # # ]: 0 : } else if (n) {
1144 : 0 : key_s += n;
1145 : 0 : ufid_present = true;
1146 : : }
1147 : :
1148 : 0 : simap_init(&port_names);
1149 [ # # ][ # # ]: 0 : DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
1150 : 0 : simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
1151 : : }
1152 : :
1153 : 0 : ofpbuf_init(&key, 0);
1154 : 0 : ofpbuf_init(&mask, 0);
1155 : :
1156 : 0 : error = odp_flow_from_string(key_s, &port_names, &key, &mask);
1157 [ # # ]: 0 : if (error) {
1158 : 0 : dpctl_error(dpctl_p, error, "parsing flow key");
1159 : 0 : goto out;
1160 : : }
1161 : :
1162 : : /* For DPDK interface, applies the operation to all pmd threads
1163 : : * on the same numa node. */
1164 : 0 : in_port_netdev = get_in_port_netdev_from_key(dpif, &key);
1165 [ # # ][ # # ]: 0 : if (in_port_netdev && netdev_is_pmd(in_port_netdev)) {
1166 : : int numa_id;
1167 : :
1168 : 0 : numa_id = netdev_get_numa_id(in_port_netdev);
1169 [ # # ]: 0 : if (ovs_numa_numa_id_is_valid(numa_id)) {
1170 : 0 : struct ovs_numa_dump *dump = ovs_numa_dump_cores_on_numa(numa_id);
1171 : : struct ovs_numa_info *iter;
1172 : :
1173 [ # # ]: 0 : FOR_EACH_CORE_ON_NUMA (iter, dump) {
1174 [ # # ]: 0 : if (ovs_numa_core_is_pinned(iter->core_id)) {
1175 [ # # ][ # # ]: 0 : error = dpif_flow_del(dpif, key.data,
1176 : 0 : key.size, ufid_present ? &ufid : NULL,
1177 : 0 : iter->core_id, dpctl_p->print_statistics ? &stats : NULL);
1178 : : }
1179 : : }
1180 : 0 : ovs_numa_dump_destroy(dump);
1181 : : } else {
1182 : 0 : error = EINVAL;
1183 : : }
1184 : : } else {
1185 [ # # ][ # # ]: 0 : error = dpif_flow_del(dpif, key.data, key.size,
1186 : : ufid_present ? &ufid : NULL, PMD_ID_NULL,
1187 : 0 : dpctl_p->print_statistics ? &stats : NULL);
1188 : : }
1189 [ # # ]: 0 : if (error) {
1190 : 0 : dpctl_error(dpctl_p, error, "deleting flow");
1191 [ # # ][ # # ]: 0 : if (error == ENOENT && !ufid_present) {
1192 : : struct ds s;
1193 : :
1194 : 0 : ds_init(&s);
1195 : 0 : ds_put_format(&s, "Perhaps you need to specify a UFID?");
1196 : 0 : dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1197 : 0 : ds_destroy(&s);
1198 : : }
1199 : 0 : goto out;
1200 : : }
1201 : :
1202 [ # # ]: 0 : if (dpctl_p->print_statistics) {
1203 : : struct ds s;
1204 : :
1205 : 0 : ds_init(&s);
1206 : 0 : dpif_flow_stats_format(&stats, &s);
1207 : 0 : dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1208 : 0 : ds_destroy(&s);
1209 : : }
1210 : :
1211 : : out:
1212 : 0 : ofpbuf_uninit(&mask);
1213 : 0 : ofpbuf_uninit(&key);
1214 : 0 : simap_destroy(&port_names);
1215 : 0 : dpif_close(dpif);
1216 : 0 : netdev_close(in_port_netdev);
1217 : 0 : return error;
1218 : : }
1219 : :
1220 : : static int
1221 : 0 : dpctl_del_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1222 : : {
1223 : : struct dpif *dpif;
1224 : : char *name;
1225 : : int error;
1226 : :
1227 [ # # ]: 0 : name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1228 [ # # ]: 0 : if (!name) {
1229 : 0 : return EINVAL;
1230 : : }
1231 : 0 : error = parsed_dpif_open(name, false, &dpif);
1232 : 0 : free(name);
1233 [ # # ]: 0 : if (error) {
1234 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
1235 : 0 : return error;
1236 : : }
1237 : :
1238 : 0 : error = dpif_flow_flush(dpif);
1239 [ # # ]: 0 : if (error) {
1240 : 0 : dpctl_error(dpctl_p, error, "deleting all flows");
1241 : : }
1242 : 0 : dpif_close(dpif);
1243 : 0 : return error;
1244 : : }
1245 : :
1246 : : static int
1247 : 0 : dpctl_help(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1248 : : struct dpctl_params *dpctl_p)
1249 : : {
1250 [ # # ]: 0 : if (dpctl_p->usage) {
1251 : 0 : dpctl_p->usage(dpctl_p->aux);
1252 : : }
1253 : :
1254 : 0 : return 0;
1255 : : }
1256 : :
1257 : : static int
1258 : 2 : dpctl_list_commands(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1259 : : struct dpctl_params *dpctl_p)
1260 : : {
1261 : 2 : struct ds ds = DS_EMPTY_INITIALIZER;
1262 : 2 : const struct dpctl_command *commands = get_all_dpctl_commands();
1263 : :
1264 : 2 : ds_put_cstr(&ds, "The available commands are:\n");
1265 [ + + ]: 40 : for (; commands->name; commands++) {
1266 : 38 : const struct dpctl_command *c = commands;
1267 : :
1268 [ - + ]: 38 : ds_put_format(&ds, " %s%-23s %s\n", dpctl_p->is_appctl ? "dpctl/" : "",
1269 : : c->name, c->usage);
1270 : : }
1271 : 2 : dpctl_puts(dpctl_p, false, ds.string);
1272 : 2 : ds_destroy(&ds);
1273 : :
1274 : 2 : return 0;
1275 : : }
1276 : :
1277 : : static int
1278 : 69 : dpctl_dump_conntrack(int argc, const char *argv[],
1279 : : struct dpctl_params *dpctl_p)
1280 : : {
1281 : : struct ct_dpif_dump_state *dump;
1282 : : struct ct_dpif_entry cte;
1283 : 69 : uint16_t zone, *pzone = NULL;
1284 : : struct dpif *dpif;
1285 : : char *name;
1286 : : int error;
1287 : :
1288 [ - + ][ # # ]: 69 : if (argc > 1 && ovs_scan(argv[argc - 1], "zone=%"SCNu16, &zone)) {
1289 : 0 : pzone = &zone;
1290 : 0 : argc--;
1291 : : }
1292 [ - + ]: 69 : name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1293 [ - + ]: 69 : if (!name) {
1294 : 0 : return EINVAL;
1295 : : }
1296 : 69 : error = parsed_dpif_open(name, false, &dpif);
1297 : 69 : free(name);
1298 [ - + ]: 69 : if (error) {
1299 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
1300 : 0 : return error;
1301 : : }
1302 : :
1303 : 69 : error = ct_dpif_dump_start(dpif, &dump, pzone);
1304 [ - + ]: 69 : if (error) {
1305 : 0 : dpctl_error(dpctl_p, error, "starting conntrack dump");
1306 : 0 : dpif_close(dpif);
1307 : 0 : return error;
1308 : : }
1309 : :
1310 [ + + ]: 503 : while (!ct_dpif_dump_next(dump, &cte)) {
1311 : 434 : struct ds s = DS_EMPTY_INITIALIZER;
1312 : :
1313 : 434 : ct_dpif_format_entry(&cte, &s, dpctl_p->verbosity,
1314 : 434 : dpctl_p->print_statistics);
1315 : 434 : ct_dpif_entry_uninit(&cte);
1316 : :
1317 : 434 : dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1318 : 434 : ds_destroy(&s);
1319 : : }
1320 : 69 : ct_dpif_dump_done(dump);
1321 : 69 : dpif_close(dpif);
1322 : 69 : return error;
1323 : : }
1324 : :
1325 : : static int
1326 : 8 : dpctl_flush_conntrack(int argc, const char *argv[],
1327 : : struct dpctl_params *dpctl_p)
1328 : : {
1329 : : struct dpif *dpif;
1330 : 8 : uint16_t zone, *pzone = NULL;
1331 : : char *name;
1332 : : int error;
1333 : :
1334 [ - + ][ # # ]: 8 : if (argc > 1 && ovs_scan(argv[argc - 1], "zone=%"SCNu16, &zone)) {
1335 : 0 : pzone = &zone;
1336 : 0 : argc--;
1337 : : }
1338 [ - + ]: 8 : name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1339 [ - + ]: 8 : if (!name) {
1340 : 0 : return EINVAL;
1341 : : }
1342 : 8 : error = parsed_dpif_open(name, false, &dpif);
1343 : 8 : free(name);
1344 [ - + ]: 8 : if (error) {
1345 : 0 : dpctl_error(dpctl_p, error, "opening datapath");
1346 : 0 : return error;
1347 : : }
1348 : :
1349 : 8 : error = ct_dpif_flush(dpif, pzone);
1350 : :
1351 : 8 : dpif_close(dpif);
1352 : 8 : return error;
1353 : : }
1354 : :
1355 : : /* Undocumented commands for unit testing. */
1356 : :
1357 : : static int
1358 : 0 : dpctl_parse_actions(int argc, const char *argv[], struct dpctl_params* dpctl_p)
1359 : : {
1360 : 0 : int i, error = 0;
1361 : :
1362 [ # # ]: 0 : for (i = 1; i < argc; i++) {
1363 : : struct ofpbuf actions;
1364 : : struct ds s;
1365 : :
1366 : 0 : ofpbuf_init(&actions, 0);
1367 : 0 : error = odp_actions_from_string(argv[i], NULL, &actions);
1368 : :
1369 [ # # ]: 0 : if (error) {
1370 : 0 : ofpbuf_uninit(&actions);
1371 : 0 : dpctl_error(dpctl_p, error, "odp_actions_from_string");
1372 : 0 : return error;
1373 : : }
1374 : :
1375 : 0 : ds_init(&s);
1376 : 0 : format_odp_actions(&s, actions.data, actions.size);
1377 : 0 : dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1378 : 0 : ds_destroy(&s);
1379 : :
1380 : 0 : ofpbuf_uninit(&actions);
1381 : : }
1382 : :
1383 : 0 : return error;
1384 : : }
1385 : :
1386 : : struct actions_for_flow {
1387 : : struct hmap_node hmap_node;
1388 : : struct flow flow;
1389 : : struct ofpbuf actions;
1390 : : };
1391 : :
1392 : : static struct actions_for_flow *
1393 : 746 : get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
1394 : : {
1395 : 746 : uint32_t hash = flow_hash(flow, 0);
1396 : : struct actions_for_flow *af;
1397 : :
1398 [ + + ][ - + ]: 746 : HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
1399 [ + - ]: 490 : if (flow_equal(&af->flow, flow)) {
1400 : 490 : return af;
1401 : : }
1402 : : }
1403 : :
1404 : 256 : af = xmalloc(sizeof *af);
1405 : 256 : af->flow = *flow;
1406 : 256 : ofpbuf_init(&af->actions, 0);
1407 : 256 : hmap_insert(actions_per_flow, &af->hmap_node, hash);
1408 : 256 : return af;
1409 : : }
1410 : :
1411 : : static int
1412 : 121 : compare_actions_for_flow(const void *a_, const void *b_)
1413 : : {
1414 : 121 : struct actions_for_flow *const *a = a_;
1415 : 121 : struct actions_for_flow *const *b = b_;
1416 : :
1417 : 121 : return flow_compare_3way(&(*a)->flow, &(*b)->flow);
1418 : : }
1419 : :
1420 : : static int
1421 : 674 : compare_output_actions(const void *a_, const void *b_)
1422 : : {
1423 : 674 : const struct nlattr *a = a_;
1424 : 674 : const struct nlattr *b = b_;
1425 : 674 : uint32_t a_port = nl_attr_get_u32(a);
1426 : 674 : uint32_t b_port = nl_attr_get_u32(b);
1427 : :
1428 [ + + ]: 674 : return a_port < b_port ? -1 : a_port > b_port;
1429 : : }
1430 : :
1431 : : static void
1432 : 256 : sort_output_actions__(struct nlattr *first, struct nlattr *end)
1433 : : {
1434 : 256 : size_t bytes = (uint8_t *) end - (uint8_t *) first;
1435 : 256 : size_t n = bytes / NL_A_U32_SIZE;
1436 : :
1437 [ - + ]: 256 : ovs_assert(bytes % NL_A_U32_SIZE == 0);
1438 : 256 : qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
1439 : 256 : }
1440 : :
1441 : : static void
1442 : 256 : sort_output_actions(struct nlattr *actions, size_t length)
1443 : : {
1444 : 256 : struct nlattr *first_output = NULL;
1445 : : struct nlattr *a;
1446 : : int left;
1447 : :
1448 [ + + ]: 1002 : NL_ATTR_FOR_EACH (a, left, actions, length) {
1449 [ + - ]: 746 : if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
1450 [ + + ]: 746 : if (!first_output) {
1451 : 746 : first_output = a;
1452 : : }
1453 : : } else {
1454 [ # # ]: 0 : if (first_output) {
1455 : 0 : sort_output_actions__(first_output, a);
1456 : 0 : first_output = NULL;
1457 : : }
1458 : : }
1459 : : }
1460 [ + - ]: 256 : if (first_output) {
1461 : 256 : uint8_t *end = (uint8_t *) actions + length;
1462 : 256 : sort_output_actions__(first_output,
1463 : : ALIGNED_CAST(struct nlattr *, end));
1464 : : }
1465 : 256 : }
1466 : :
1467 : : /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1468 : : * have the syntax used by "ovs-dpctl dump-flows".
1469 : : *
1470 : : * This command prints ACTIONS in a format that shows what happens for each
1471 : : * VLAN, independent of the order of the ACTIONS. For example, there is more
1472 : : * than one way to output a packet on VLANs 9 and 11, but this command will
1473 : : * print the same output for any form.
1474 : : *
1475 : : * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1476 : : * so far the implementation only covers VLANs. */
1477 : : static int
1478 : 208 : dpctl_normalize_actions(int argc, const char *argv[],
1479 : : struct dpctl_params *dpctl_p)
1480 : : {
1481 : : struct simap port_names;
1482 : : struct ofpbuf keybuf;
1483 : : struct flow flow;
1484 : : struct ofpbuf odp_actions;
1485 : : struct hmap actions_per_flow;
1486 : : struct actions_for_flow **afs;
1487 : : struct actions_for_flow *af;
1488 : : struct nlattr *a;
1489 : : size_t n_afs;
1490 : : struct ds s;
1491 : : int left;
1492 : : int i, error;
1493 : :
1494 : 208 : ds_init(&s);
1495 : :
1496 : 208 : simap_init(&port_names);
1497 [ - + ]: 208 : for (i = 3; i < argc; i++) {
1498 : : char name[16];
1499 : : int number;
1500 : :
1501 [ # # ]: 0 : if (ovs_scan(argv[i], "%15[^=]=%d", name, &number)) {
1502 : 0 : uintptr_t n = number;
1503 : 0 : simap_put(&port_names, name, n);
1504 : : } else {
1505 : 0 : dpctl_error(dpctl_p, 0, "%s: expected NAME=NUMBER", argv[i]);
1506 : 0 : error = EINVAL;
1507 : 0 : goto out;
1508 : : }
1509 : : }
1510 : :
1511 : : /* Parse flow key. */
1512 : 208 : ofpbuf_init(&keybuf, 0);
1513 : 208 : error = odp_flow_from_string(argv[1], &port_names, &keybuf, NULL);
1514 [ - + ]: 208 : if (error) {
1515 : 0 : dpctl_error(dpctl_p, error, "odp_flow_key_from_string");
1516 : 0 : goto out_freekeybuf;
1517 : : }
1518 : :
1519 : 208 : ds_clear(&s);
1520 : 208 : odp_flow_format(keybuf.data, keybuf.size, NULL, 0, NULL,
1521 : 208 : &s, dpctl_p->verbosity);
1522 : 208 : dpctl_print(dpctl_p, "input flow: %s\n", ds_cstr(&s));
1523 : :
1524 : 208 : error = odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow);
1525 [ - + ]: 208 : if (error) {
1526 : 0 : dpctl_error(dpctl_p, error, "odp_flow_key_to_flow");
1527 : 0 : goto out_freekeybuf;
1528 : : }
1529 : :
1530 : : /* Parse actions. */
1531 : 208 : ofpbuf_init(&odp_actions, 0);
1532 : 208 : error = odp_actions_from_string(argv[2], &port_names, &odp_actions);
1533 [ - + ]: 208 : if (error) {
1534 : 0 : dpctl_error(dpctl_p, error, "odp_actions_from_string");
1535 : 0 : goto out_freeactions;
1536 : : }
1537 : :
1538 [ - + ]: 208 : if (dpctl_p->verbosity) {
1539 : 0 : ds_clear(&s);
1540 : 0 : format_odp_actions(&s, odp_actions.data, odp_actions.size);
1541 : 0 : dpctl_print(dpctl_p, "input actions: %s\n", ds_cstr(&s));
1542 : : }
1543 : :
1544 : 208 : hmap_init(&actions_per_flow);
1545 [ + + ]: 1222 : NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
1546 : : const struct ovs_action_push_vlan *push;
1547 [ + + + ]: 1014 : switch(nl_attr_type(a)) {
1548 : : case OVS_ACTION_ATTR_POP_VLAN:
1549 : 145 : flow.vlan_tci = htons(0);
1550 : 145 : continue;
1551 : :
1552 : : case OVS_ACTION_ATTR_PUSH_VLAN:
1553 : 123 : push = nl_attr_get_unspec(a, sizeof *push);
1554 : 123 : flow.vlan_tci = push->vlan_tci;
1555 : 123 : continue;
1556 : : }
1557 : :
1558 : 746 : af = get_actions_for_flow(&actions_per_flow, &flow);
1559 : 746 : nl_msg_put_unspec(&af->actions, nl_attr_type(a),
1560 : : nl_attr_get(a), nl_attr_get_size(a));
1561 : : }
1562 : :
1563 : 208 : n_afs = hmap_count(&actions_per_flow);
1564 : 208 : afs = xmalloc(n_afs * sizeof *afs);
1565 : 208 : i = 0;
1566 [ + + ][ - + ]: 464 : HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
1567 : 256 : afs[i++] = af;
1568 : : }
1569 : :
1570 [ - + ]: 208 : ovs_assert(i == n_afs);
1571 : 208 : hmap_destroy(&actions_per_flow);
1572 : :
1573 : 208 : qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
1574 : :
1575 [ + + ]: 464 : for (i = 0; i < n_afs; i++) {
1576 : 256 : struct actions_for_flow *af = afs[i];
1577 : :
1578 : 256 : sort_output_actions(af->actions.data, af->actions.size);
1579 : :
1580 [ + + ]: 256 : if (af->flow.vlan_tci != htons(0)) {
1581 : 256 : dpctl_print(dpctl_p, "vlan(vid=%"PRIu16",pcp=%d): ",
1582 : 128 : vlan_tci_to_vid(af->flow.vlan_tci),
1583 : 128 : vlan_tci_to_pcp(af->flow.vlan_tci));
1584 : : } else {
1585 : 128 : dpctl_print(dpctl_p, "no vlan: ");
1586 : : }
1587 : :
1588 [ - + ]: 256 : if (eth_type_mpls(af->flow.dl_type)) {
1589 : 0 : dpctl_print(dpctl_p, "mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
1590 : : mpls_lse_to_label(af->flow.mpls_lse[0]),
1591 : 0 : mpls_lse_to_tc(af->flow.mpls_lse[0]),
1592 : 0 : mpls_lse_to_ttl(af->flow.mpls_lse[0]));
1593 : : } else {
1594 : 256 : dpctl_print(dpctl_p, "no mpls: ");
1595 : : }
1596 : :
1597 : 256 : ds_clear(&s);
1598 : 256 : format_odp_actions(&s, af->actions.data, af->actions.size);
1599 : 256 : dpctl_puts(dpctl_p, false, ds_cstr(&s));
1600 : :
1601 : 256 : ofpbuf_uninit(&af->actions);
1602 : 256 : free(af);
1603 : : }
1604 : 208 : free(afs);
1605 : :
1606 : :
1607 : : out_freeactions:
1608 : 208 : ofpbuf_uninit(&odp_actions);
1609 : : out_freekeybuf:
1610 : 208 : ofpbuf_uninit(&keybuf);
1611 : : out:
1612 : 208 : simap_destroy(&port_names);
1613 : 208 : ds_destroy(&s);
1614 : :
1615 : 208 : return error;
1616 : : }
1617 : :
1618 : : static const struct dpctl_command all_commands[] = {
1619 : : { "add-dp", "add-dp dp [iface...]", 1, INT_MAX, dpctl_add_dp, DP_RW },
1620 : : { "del-dp", "del-dp dp", 1, 1, dpctl_del_dp, DP_RW },
1621 : : { "add-if", "add-if dp iface...", 2, INT_MAX, dpctl_add_if, DP_RW },
1622 : : { "del-if", "del-if dp iface...", 2, INT_MAX, dpctl_del_if, DP_RW },
1623 : : { "set-if", "set-if dp iface...", 2, INT_MAX, dpctl_set_if, DP_RW },
1624 : : { "dump-dps", "", 0, 0, dpctl_dump_dps, DP_RO },
1625 : : { "show", "[dp...]", 0, INT_MAX, dpctl_show, DP_RO },
1626 : : { "dump-flows", "[dp]", 0, 2, dpctl_dump_flows, DP_RO },
1627 : : { "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow, DP_RW },
1628 : : { "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow, DP_RW },
1629 : : { "get-flow", "get-flow [dp] ufid", 1, 2, dpctl_get_flow, DP_RO },
1630 : : { "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow, DP_RW },
1631 : : { "del-flows", "[dp]", 0, 1, dpctl_del_flows, DP_RW },
1632 : : { "dump-conntrack", "[dp] [zone=N]", 0, 2, dpctl_dump_conntrack, DP_RO },
1633 : : { "flush-conntrack", "[dp] [zone=N]", 0, 2, dpctl_flush_conntrack, DP_RW },
1634 : : { "help", "", 0, INT_MAX, dpctl_help, DP_RO },
1635 : : { "list-commands", "", 0, INT_MAX, dpctl_list_commands, DP_RO },
1636 : :
1637 : : /* Undocumented commands for testing. */
1638 : : { "parse-actions", "actions", 1, INT_MAX, dpctl_parse_actions, DP_RO },
1639 : : { "normalize-actions", "actions", 2, INT_MAX, dpctl_normalize_actions, DP_RO },
1640 : :
1641 : : { NULL, NULL, 0, 0, NULL, DP_RO },
1642 : : };
1643 : :
1644 : 2 : static const struct dpctl_command *get_all_dpctl_commands(void)
1645 : : {
1646 : 2 : return all_commands;
1647 : : }
1648 : :
1649 : : /* Runs the command designated by argv[0] within the command table specified by
1650 : : * 'commands', which must be terminated by a command whose 'name' member is a
1651 : : * null pointer. */
1652 : : int
1653 : 274 : dpctl_run_command(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1654 : : {
1655 : : const struct dpctl_command *p;
1656 : :
1657 [ - + ]: 274 : if (argc < 1) {
1658 : 0 : dpctl_error(dpctl_p, 0, "missing command name; use --help for help");
1659 : 0 : return EINVAL;
1660 : : }
1661 : :
1662 [ + - ]: 4114 : for (p = all_commands; p->name != NULL; p++) {
1663 [ + + ]: 4114 : if (!strcmp(p->name, argv[0])) {
1664 : 274 : int n_arg = argc - 1;
1665 [ - + ]: 274 : if (n_arg < p->min_args) {
1666 : 0 : dpctl_error(dpctl_p, 0,
1667 : : "'%s' command requires at least %d arguments",
1668 : : p->name, p->min_args);
1669 : 0 : return EINVAL;
1670 [ - + ]: 274 : } else if (n_arg > p->max_args) {
1671 : 0 : dpctl_error(dpctl_p, 0,
1672 : : "'%s' command takes at most %d arguments",
1673 : : p->name, p->max_args);
1674 : 0 : return EINVAL;
1675 : : } else {
1676 [ + + ][ - + ]: 274 : if (p->mode == DP_RW && dpctl_p->read_only) {
1677 : 0 : dpctl_error(dpctl_p, 0,
1678 : : "'%s' command does not work in read only mode",
1679 : : p->name);
1680 : 0 : return EINVAL;
1681 : : }
1682 : 274 : return p->handler(argc, argv, dpctl_p);
1683 : : }
1684 : : }
1685 : : }
1686 : :
1687 : 0 : dpctl_error(dpctl_p, 0, "unknown command '%s'; use --help for help",
1688 : : argv[0]);
1689 : 0 : return EINVAL;
1690 : : }
1691 : :
1692 : : static void
1693 : 502 : dpctl_unixctl_print(void *userdata, bool error OVS_UNUSED, const char *msg)
1694 : : {
1695 : 502 : struct ds *ds = userdata;
1696 : 502 : ds_put_cstr(ds, msg);
1697 : 502 : }
1698 : :
1699 : : static void
1700 : 117 : dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
1701 : : void *aux)
1702 : : {
1703 : 117 : struct ds ds = DS_EMPTY_INITIALIZER;
1704 : 117 : bool error = false;
1705 : :
1706 : 117 : struct dpctl_params dpctl_p = {
1707 : : .is_appctl = true,
1708 : : .output = dpctl_unixctl_print,
1709 : : .aux = &ds,
1710 : : };
1711 : :
1712 : : /* Parse options (like getopt). Unfortunately it does
1713 : : * not seem a good idea to call getopt_long() here, since it uses global
1714 : : * variables */
1715 [ + + ][ + - ]: 117 : while (argc > 1 && !error) {
1716 : 24 : const char *arg = argv[1];
1717 [ - + ]: 24 : if (!strncmp(arg, "--", 2)) {
1718 : : /* Long option */
1719 [ # # ]: 0 : if (!strcmp(arg, "--statistics")) {
1720 : 0 : dpctl_p.print_statistics = true;
1721 [ # # ]: 0 : } else if (!strcmp(arg, "--clear")) {
1722 : 0 : dpctl_p.zero_statistics = true;
1723 [ # # ]: 0 : } else if (!strcmp(arg, "--may-create")) {
1724 : 0 : dpctl_p.may_create = true;
1725 [ # # ]: 0 : } else if (!strcmp(arg, "--more")) {
1726 : 0 : dpctl_p.verbosity++;
1727 : : } else {
1728 : 0 : ds_put_format(&ds, "Unrecognized option %s", argv[1]);
1729 : 0 : error = true;
1730 : : }
1731 [ - + ][ # # ]: 24 : } else if (arg[0] == '-' && arg[1] != '\0') {
1732 : : /* Short option[s] */
1733 : 0 : const char *opt = &arg[1];
1734 : :
1735 [ # # ][ # # ]: 0 : while (*opt && !error) {
1736 [ # # # ]: 0 : switch (*opt) {
1737 : : case 'm':
1738 : 0 : dpctl_p.verbosity++;
1739 : 0 : break;
1740 : : case 's':
1741 : 0 : dpctl_p.print_statistics = true;
1742 : 0 : break;
1743 : : default:
1744 : 0 : ds_put_format(&ds, "Unrecognized option -%c", *opt);
1745 : 0 : error = true;
1746 : 0 : break;
1747 : : }
1748 : 0 : opt++;
1749 : : }
1750 : : } else {
1751 : : /* Doesn't start with -, not an option */
1752 : : break;
1753 : : }
1754 : :
1755 [ # # ]: 0 : if (error) {
1756 : 0 : break;
1757 : : }
1758 : 0 : argv++;
1759 : 0 : argc--;
1760 : : }
1761 : :
1762 [ + - ]: 117 : if (!error) {
1763 : 117 : dpctl_command_handler *handler = (dpctl_command_handler *) aux;
1764 : 117 : error = handler(argc, argv, &dpctl_p) != 0;
1765 : : }
1766 : :
1767 [ + + ]: 117 : if (error) {
1768 : 9 : unixctl_command_reply_error(conn, ds_cstr(&ds));
1769 : : } else {
1770 : 108 : unixctl_command_reply(conn, ds_cstr(&ds));
1771 : : }
1772 : :
1773 : 117 : ds_destroy(&ds);
1774 : 117 : }
1775 : :
1776 : : void
1777 : 684 : dpctl_unixctl_register(void)
1778 : : {
1779 : : const struct dpctl_command *p;
1780 : :
1781 [ + + ]: 13680 : for (p = all_commands; p->name != NULL; p++) {
1782 [ + + ]: 12996 : if (strcmp(p->name, "help")) {
1783 : 12312 : char *cmd_name = xasprintf("dpctl/%s", p->name);
1784 : 12312 : unixctl_command_register(cmd_name, "", p->min_args, p->max_args,
1785 : 12312 : dpctl_unixctl_handler, p->handler);
1786 : 12312 : free(cmd_name);
1787 : : }
1788 : : }
1789 : 684 : }
|