]> git.proxmox.com Git - mirror_ovs.git/blob - lib/dpif.c
dpif: Add support to set user features
[mirror_ovs.git] / lib / dpif.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 "dpif-provider.h"
19
20 #include <ctype.h>
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "coverage.h"
27 #include "dpctl.h"
28 #include "dp-packet.h"
29 #include "dpif-netdev.h"
30 #include "openvswitch/dynamic-string.h"
31 #include "flow.h"
32 #include "netdev.h"
33 #include "netlink.h"
34 #include "odp-execute.h"
35 #include "odp-util.h"
36 #include "openvswitch/ofp-print.h"
37 #include "openvswitch/ofpbuf.h"
38 #include "packets.h"
39 #include "openvswitch/poll-loop.h"
40 #include "route-table.h"
41 #include "seq.h"
42 #include "openvswitch/shash.h"
43 #include "sset.h"
44 #include "timeval.h"
45 #include "tnl-neigh-cache.h"
46 #include "tnl-ports.h"
47 #include "util.h"
48 #include "uuid.h"
49 #include "valgrind.h"
50 #include "openvswitch/ofp-errors.h"
51 #include "openvswitch/vlog.h"
52 #include "lib/netdev-provider.h"
53
54 VLOG_DEFINE_THIS_MODULE(dpif);
55
56 COVERAGE_DEFINE(dpif_destroy);
57 COVERAGE_DEFINE(dpif_port_add);
58 COVERAGE_DEFINE(dpif_port_del);
59 COVERAGE_DEFINE(dpif_flow_flush);
60 COVERAGE_DEFINE(dpif_flow_get);
61 COVERAGE_DEFINE(dpif_flow_put);
62 COVERAGE_DEFINE(dpif_flow_del);
63 COVERAGE_DEFINE(dpif_execute);
64 COVERAGE_DEFINE(dpif_purge);
65 COVERAGE_DEFINE(dpif_execute_with_help);
66 COVERAGE_DEFINE(dpif_meter_set);
67 COVERAGE_DEFINE(dpif_meter_get);
68 COVERAGE_DEFINE(dpif_meter_del);
69
70 static const struct dpif_class *base_dpif_classes[] = {
71 #if defined(__linux__) || defined(_WIN32)
72 &dpif_netlink_class,
73 #endif
74 &dpif_netdev_class,
75 };
76
77 struct registered_dpif_class {
78 const struct dpif_class *dpif_class;
79 int refcount;
80 };
81 static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes);
82 static struct sset dpif_blacklist = SSET_INITIALIZER(&dpif_blacklist);
83
84 /* Protects 'dpif_classes', including the refcount, and 'dpif_blacklist'. */
85 static struct ovs_mutex dpif_mutex = OVS_MUTEX_INITIALIZER;
86
87 /* Rate limit for individual messages going to or from the datapath, output at
88 * DBG level. This is very high because, if these are enabled, it is because
89 * we really need to see them. */
90 static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
91
92 /* Not really much point in logging many dpif errors. */
93 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
94
95 static void log_operation(const struct dpif *, const char *operation,
96 int error);
97 static bool should_log_flow_message(const struct vlog_module *module,
98 int error);
99
100 /* Incremented whenever tnl route, arp, etc changes. */
101 struct seq *tnl_conf_seq;
102
103 static bool
104 dpif_is_tap_port(const char *type)
105 {
106 return !strcmp(type, "tap");
107 }
108
109 static void
110 dp_initialize(void)
111 {
112 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
113
114 if (ovsthread_once_start(&once)) {
115 int i;
116
117 tnl_conf_seq = seq_create();
118 dpctl_unixctl_register();
119 tnl_port_map_init();
120 tnl_neigh_cache_init();
121 route_table_init();
122
123 for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) {
124 dp_register_provider(base_dpif_classes[i]);
125 }
126
127 ovsthread_once_done(&once);
128 }
129 }
130
131 static int
132 dp_register_provider__(const struct dpif_class *new_class)
133 {
134 struct registered_dpif_class *registered_class;
135 int error;
136
137 if (sset_contains(&dpif_blacklist, new_class->type)) {
138 VLOG_DBG("attempted to register blacklisted provider: %s",
139 new_class->type);
140 return EINVAL;
141 }
142
143 if (shash_find(&dpif_classes, new_class->type)) {
144 VLOG_WARN("attempted to register duplicate datapath provider: %s",
145 new_class->type);
146 return EEXIST;
147 }
148
149 error = new_class->init ? new_class->init() : 0;
150 if (error) {
151 VLOG_WARN("failed to initialize %s datapath class: %s",
152 new_class->type, ovs_strerror(error));
153 return error;
154 }
155
156 registered_class = xmalloc(sizeof *registered_class);
157 registered_class->dpif_class = new_class;
158 registered_class->refcount = 0;
159
160 shash_add(&dpif_classes, new_class->type, registered_class);
161
162 return 0;
163 }
164
165 /* Registers a new datapath provider. After successful registration, new
166 * datapaths of that type can be opened using dpif_open(). */
167 int
168 dp_register_provider(const struct dpif_class *new_class)
169 {
170 int error;
171
172 ovs_mutex_lock(&dpif_mutex);
173 error = dp_register_provider__(new_class);
174 ovs_mutex_unlock(&dpif_mutex);
175
176 return error;
177 }
178
179 /* Unregisters a datapath provider. 'type' must have been previously
180 * registered and not currently be in use by any dpifs. After unregistration
181 * new datapaths of that type cannot be opened using dpif_open(). */
182 static int
183 dp_unregister_provider__(const char *type)
184 {
185 struct shash_node *node;
186 struct registered_dpif_class *registered_class;
187
188 node = shash_find(&dpif_classes, type);
189 if (!node) {
190 return EAFNOSUPPORT;
191 }
192
193 registered_class = node->data;
194 if (registered_class->refcount) {
195 VLOG_WARN("attempted to unregister in use datapath provider: %s", type);
196 return EBUSY;
197 }
198
199 shash_delete(&dpif_classes, node);
200 free(registered_class);
201
202 return 0;
203 }
204
205 /* Unregisters a datapath provider. 'type' must have been previously
206 * registered and not currently be in use by any dpifs. After unregistration
207 * new datapaths of that type cannot be opened using dpif_open(). */
208 int
209 dp_unregister_provider(const char *type)
210 {
211 int error;
212
213 dp_initialize();
214
215 ovs_mutex_lock(&dpif_mutex);
216 error = dp_unregister_provider__(type);
217 ovs_mutex_unlock(&dpif_mutex);
218
219 return error;
220 }
221
222 /* Blacklists a provider. Causes future calls of dp_register_provider() with
223 * a dpif_class which implements 'type' to fail. */
224 void
225 dp_blacklist_provider(const char *type)
226 {
227 ovs_mutex_lock(&dpif_mutex);
228 sset_add(&dpif_blacklist, type);
229 ovs_mutex_unlock(&dpif_mutex);
230 }
231
232 /* Adds the types of all currently registered datapath providers to 'types'.
233 * The caller must first initialize the sset. */
234 void
235 dp_enumerate_types(struct sset *types)
236 {
237 struct shash_node *node;
238
239 dp_initialize();
240
241 ovs_mutex_lock(&dpif_mutex);
242 SHASH_FOR_EACH(node, &dpif_classes) {
243 const struct registered_dpif_class *registered_class = node->data;
244 sset_add(types, registered_class->dpif_class->type);
245 }
246 ovs_mutex_unlock(&dpif_mutex);
247 }
248
249 static void
250 dp_class_unref(struct registered_dpif_class *rc)
251 {
252 ovs_mutex_lock(&dpif_mutex);
253 ovs_assert(rc->refcount);
254 rc->refcount--;
255 ovs_mutex_unlock(&dpif_mutex);
256 }
257
258 static struct registered_dpif_class *
259 dp_class_lookup(const char *type)
260 {
261 struct registered_dpif_class *rc;
262
263 ovs_mutex_lock(&dpif_mutex);
264 rc = shash_find_data(&dpif_classes, type);
265 if (rc) {
266 rc->refcount++;
267 }
268 ovs_mutex_unlock(&dpif_mutex);
269
270 return rc;
271 }
272
273 /* Clears 'names' and enumerates the names of all known created datapaths with
274 * the given 'type'. The caller must first initialize the sset. Returns 0 if
275 * successful, otherwise a positive errno value.
276 *
277 * Some kinds of datapaths might not be practically enumerable. This is not
278 * considered an error. */
279 int
280 dp_enumerate_names(const char *type, struct sset *names)
281 {
282 struct registered_dpif_class *registered_class;
283 const struct dpif_class *dpif_class;
284 int error;
285
286 dp_initialize();
287 sset_clear(names);
288
289 registered_class = dp_class_lookup(type);
290 if (!registered_class) {
291 VLOG_WARN("could not enumerate unknown type: %s", type);
292 return EAFNOSUPPORT;
293 }
294
295 dpif_class = registered_class->dpif_class;
296 error = (dpif_class->enumerate
297 ? dpif_class->enumerate(names, dpif_class)
298 : 0);
299 if (error) {
300 VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
301 ovs_strerror(error));
302 }
303 dp_class_unref(registered_class);
304
305 return error;
306 }
307
308 /* Parses 'datapath_name_', which is of the form [type@]name into its
309 * component pieces. 'name' and 'type' must be freed by the caller.
310 *
311 * The returned 'type' is normalized, as if by dpif_normalize_type(). */
312 void
313 dp_parse_name(const char *datapath_name_, char **name, char **type)
314 {
315 char *datapath_name = xstrdup(datapath_name_);
316 char *separator;
317
318 separator = strchr(datapath_name, '@');
319 if (separator) {
320 *separator = '\0';
321 *type = datapath_name;
322 *name = xstrdup(dpif_normalize_type(separator + 1));
323 } else {
324 *name = datapath_name;
325 *type = xstrdup(dpif_normalize_type(NULL));
326 }
327 }
328
329 static int
330 do_open(const char *name, const char *type, bool create, struct dpif **dpifp)
331 {
332 struct dpif *dpif = NULL;
333 int error;
334 struct registered_dpif_class *registered_class;
335
336 dp_initialize();
337
338 type = dpif_normalize_type(type);
339 registered_class = dp_class_lookup(type);
340 if (!registered_class) {
341 VLOG_WARN("could not create datapath %s of unknown type %s", name,
342 type);
343 error = EAFNOSUPPORT;
344 goto exit;
345 }
346
347 error = registered_class->dpif_class->open(registered_class->dpif_class,
348 name, create, &dpif);
349 if (!error) {
350 struct dpif_port_dump port_dump;
351 struct dpif_port dpif_port;
352
353 ovs_assert(dpif->dpif_class == registered_class->dpif_class);
354
355 DPIF_PORT_FOR_EACH(&dpif_port, &port_dump, dpif) {
356 struct netdev *netdev;
357 int err;
358
359 if (dpif_is_tap_port(dpif_port.type)) {
360 continue;
361 }
362
363 err = netdev_open(dpif_port.name, dpif_port.type, &netdev);
364
365 if (!err) {
366 netdev_ports_insert(netdev, dpif->dpif_class, &dpif_port);
367 netdev_close(netdev);
368 } else {
369 VLOG_WARN("could not open netdev %s type %s: %s",
370 dpif_port.name, dpif_port.type, ovs_strerror(err));
371 }
372 }
373 } else {
374 dp_class_unref(registered_class);
375 }
376
377 exit:
378 *dpifp = error ? NULL : dpif;
379 return error;
380 }
381
382 /* Tries to open an existing datapath named 'name' and type 'type'. Will fail
383 * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
384 * the empty string to specify the default system type. Returns 0 if
385 * successful, otherwise a positive errno value. On success stores a pointer
386 * to the datapath in '*dpifp', otherwise a null pointer. */
387 int
388 dpif_open(const char *name, const char *type, struct dpif **dpifp)
389 {
390 return do_open(name, type, false, dpifp);
391 }
392
393 /* Tries to create and open a new datapath with the given 'name' and 'type'.
394 * 'type' may be either NULL or the empty string to specify the default system
395 * type. Will fail if a datapath with 'name' and 'type' already exists.
396 * Returns 0 if successful, otherwise a positive errno value. On success
397 * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */
398 int
399 dpif_create(const char *name, const char *type, struct dpif **dpifp)
400 {
401 return do_open(name, type, true, dpifp);
402 }
403
404 /* Tries to open a datapath with the given 'name' and 'type', creating it if it
405 * does not exist. 'type' may be either NULL or the empty string to specify
406 * the default system type. Returns 0 if successful, otherwise a positive
407 * errno value. On success stores a pointer to the datapath in '*dpifp',
408 * otherwise a null pointer. */
409 int
410 dpif_create_and_open(const char *name, const char *type, struct dpif **dpifp)
411 {
412 int error;
413
414 error = dpif_create(name, type, dpifp);
415 if (error == EEXIST || error == EBUSY) {
416 error = dpif_open(name, type, dpifp);
417 if (error) {
418 VLOG_WARN("datapath %s already exists but cannot be opened: %s",
419 name, ovs_strerror(error));
420 }
421 } else if (error) {
422 VLOG_WARN("failed to create datapath %s: %s",
423 name, ovs_strerror(error));
424 }
425 return error;
426 }
427
428 static void
429 dpif_remove_netdev_ports(struct dpif *dpif) {
430 struct dpif_port_dump port_dump;
431 struct dpif_port dpif_port;
432
433 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
434 if (!dpif_is_tap_port(dpif_port.type)) {
435 netdev_ports_remove(dpif_port.port_no, dpif->dpif_class);
436 }
437 }
438 }
439
440 /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
441 * itself; call dpif_delete() first, instead, if that is desirable. */
442 void
443 dpif_close(struct dpif *dpif)
444 {
445 if (dpif) {
446 struct registered_dpif_class *rc;
447
448 rc = shash_find_data(&dpif_classes, dpif->dpif_class->type);
449
450 if (rc->refcount == 1) {
451 dpif_remove_netdev_ports(dpif);
452 }
453 dpif_uninit(dpif, true);
454 dp_class_unref(rc);
455 }
456 }
457
458 /* Performs periodic work needed by 'dpif'. */
459 bool
460 dpif_run(struct dpif *dpif)
461 {
462 if (dpif->dpif_class->run) {
463 return dpif->dpif_class->run(dpif);
464 }
465 return false;
466 }
467
468 /* Arranges for poll_block() to wake up when dp_run() needs to be called for
469 * 'dpif'. */
470 void
471 dpif_wait(struct dpif *dpif)
472 {
473 if (dpif->dpif_class->wait) {
474 dpif->dpif_class->wait(dpif);
475 }
476 }
477
478 /* Returns the name of datapath 'dpif' prefixed with the type
479 * (for use in log messages). */
480 const char *
481 dpif_name(const struct dpif *dpif)
482 {
483 return dpif->full_name;
484 }
485
486 /* Returns the name of datapath 'dpif' without the type
487 * (for use in device names). */
488 const char *
489 dpif_base_name(const struct dpif *dpif)
490 {
491 return dpif->base_name;
492 }
493
494 /* Returns the type of datapath 'dpif'. */
495 const char *
496 dpif_type(const struct dpif *dpif)
497 {
498 return dpif->dpif_class->type;
499 }
500
501 /* Checks if datapath 'dpif' requires cleanup. */
502 bool
503 dpif_cleanup_required(const struct dpif *dpif)
504 {
505 return dpif->dpif_class->cleanup_required;
506 }
507
508 /* Returns the fully spelled out name for the given datapath 'type'.
509 *
510 * Normalized type string can be compared with strcmp(). Unnormalized type
511 * string might be the same even if they have different spellings. */
512 const char *
513 dpif_normalize_type(const char *type)
514 {
515 return type && type[0] ? type : "system";
516 }
517
518 /* Destroys the datapath that 'dpif' is connected to, first removing all of its
519 * ports. After calling this function, it does not make sense to pass 'dpif'
520 * to any functions other than dpif_name() or dpif_close(). */
521 int
522 dpif_delete(struct dpif *dpif)
523 {
524 int error;
525
526 COVERAGE_INC(dpif_destroy);
527
528 error = dpif->dpif_class->destroy(dpif);
529 log_operation(dpif, "delete", error);
530 return error;
531 }
532
533 /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
534 * otherwise a positive errno value. */
535 int
536 dpif_get_dp_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
537 {
538 int error = dpif->dpif_class->get_stats(dpif, stats);
539 if (error) {
540 memset(stats, 0, sizeof *stats);
541 }
542 log_operation(dpif, "get_stats", error);
543 return error;
544 }
545
546 int
547 dpif_set_features(struct dpif *dpif, uint32_t new_features)
548 {
549 int error = dpif->dpif_class->set_features(dpif, new_features);
550
551 log_operation(dpif, "set_features", error);
552 return error;
553 }
554
555 const char *
556 dpif_port_open_type(const char *datapath_type, const char *port_type)
557 {
558 struct registered_dpif_class *rc;
559
560 datapath_type = dpif_normalize_type(datapath_type);
561
562 ovs_mutex_lock(&dpif_mutex);
563 rc = shash_find_data(&dpif_classes, datapath_type);
564 if (rc && rc->dpif_class->port_open_type) {
565 port_type = rc->dpif_class->port_open_type(rc->dpif_class, port_type);
566 }
567 ovs_mutex_unlock(&dpif_mutex);
568
569 return port_type;
570 }
571
572 /* Attempts to add 'netdev' as a port on 'dpif'. If 'port_nop' is
573 * non-null and its value is not ODPP_NONE, then attempts to use the
574 * value as the port number.
575 *
576 * If successful, returns 0 and sets '*port_nop' to the new port's port
577 * number (if 'port_nop' is non-null). On failure, returns a positive
578 * errno value and sets '*port_nop' to ODPP_NONE (if 'port_nop' is
579 * non-null). */
580 int
581 dpif_port_add(struct dpif *dpif, struct netdev *netdev, odp_port_t *port_nop)
582 {
583 const char *netdev_name = netdev_get_name(netdev);
584 odp_port_t port_no = ODPP_NONE;
585 int error;
586
587 COVERAGE_INC(dpif_port_add);
588
589 if (port_nop) {
590 port_no = *port_nop;
591 }
592
593 error = dpif->dpif_class->port_add(dpif, netdev, &port_no);
594 if (!error) {
595 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu32,
596 dpif_name(dpif), netdev_name, port_no);
597
598 if (!dpif_is_tap_port(netdev_get_type(netdev))) {
599
600 struct dpif_port dpif_port;
601
602 dpif_port.type = CONST_CAST(char *, netdev_get_type(netdev));
603 dpif_port.name = CONST_CAST(char *, netdev_name);
604 dpif_port.port_no = port_no;
605 netdev_ports_insert(netdev, dpif->dpif_class, &dpif_port);
606 }
607 } else {
608 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
609 dpif_name(dpif), netdev_name, ovs_strerror(error));
610 port_no = ODPP_NONE;
611 }
612 if (port_nop) {
613 *port_nop = port_no;
614 }
615 return error;
616 }
617
618 /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
619 * otherwise a positive errno value. */
620 int
621 dpif_port_del(struct dpif *dpif, odp_port_t port_no, bool local_delete)
622 {
623 int error = 0;
624
625 COVERAGE_INC(dpif_port_del);
626
627 if (!local_delete) {
628 error = dpif->dpif_class->port_del(dpif, port_no);
629 if (!error) {
630 VLOG_DBG_RL(&dpmsg_rl, "%s: port_del(%"PRIu32")",
631 dpif_name(dpif), port_no);
632 } else {
633 log_operation(dpif, "port_del", error);
634 }
635 }
636
637 netdev_ports_remove(port_no, dpif->dpif_class);
638 return error;
639 }
640
641 /* Makes a deep copy of 'src' into 'dst'. */
642 void
643 dpif_port_clone(struct dpif_port *dst, const struct dpif_port *src)
644 {
645 dst->name = xstrdup(src->name);
646 dst->type = xstrdup(src->type);
647 dst->port_no = src->port_no;
648 }
649
650 /* Frees memory allocated to members of 'dpif_port'.
651 *
652 * Do not call this function on a dpif_port obtained from
653 * dpif_port_dump_next(): that function retains ownership of the data in the
654 * dpif_port. */
655 void
656 dpif_port_destroy(struct dpif_port *dpif_port)
657 {
658 free(dpif_port->name);
659 free(dpif_port->type);
660 }
661
662 /* Checks if port named 'devname' exists in 'dpif'. If so, returns
663 * true; otherwise, returns false. */
664 bool
665 dpif_port_exists(const struct dpif *dpif, const char *devname)
666 {
667 int error = dpif->dpif_class->port_query_by_name(dpif, devname, NULL);
668 if (error != 0 && error != ENODEV) {
669 VLOG_WARN_RL(&error_rl, "%s: failed to query port %s: %s",
670 dpif_name(dpif), devname, ovs_strerror(error));
671 }
672
673 return !error;
674 }
675
676 /* Refreshes configuration of 'dpif's port. */
677 int
678 dpif_port_set_config(struct dpif *dpif, odp_port_t port_no,
679 const struct smap *cfg)
680 {
681 int error = 0;
682
683 if (dpif->dpif_class->port_set_config) {
684 error = dpif->dpif_class->port_set_config(dpif, port_no, cfg);
685 if (error) {
686 log_operation(dpif, "port_set_config", error);
687 }
688 }
689
690 return error;
691 }
692
693 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
694 * initializes '*port' appropriately; on failure, returns a positive errno
695 * value.
696 *
697 * Retuns ENODEV if the port doesn't exist.
698 *
699 * The caller owns the data in 'port' and must free it with
700 * dpif_port_destroy() when it is no longer needed. */
701 int
702 dpif_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
703 struct dpif_port *port)
704 {
705 int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port);
706 if (!error) {
707 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu32" is device %s",
708 dpif_name(dpif), port_no, port->name);
709 } else {
710 memset(port, 0, sizeof *port);
711 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu32": %s",
712 dpif_name(dpif), port_no, ovs_strerror(error));
713 }
714 return error;
715 }
716
717 /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
718 * initializes '*port' appropriately; on failure, returns a positive errno
719 * value.
720 *
721 * Retuns ENODEV if the port doesn't exist.
722 *
723 * The caller owns the data in 'port' and must free it with
724 * dpif_port_destroy() when it is no longer needed. */
725 int
726 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
727 struct dpif_port *port)
728 {
729 int error = dpif->dpif_class->port_query_by_name(dpif, devname, port);
730 if (!error) {
731 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu32,
732 dpif_name(dpif), devname, port->port_no);
733 } else {
734 memset(port, 0, sizeof *port);
735
736 /* For ENODEV we use DBG level because the caller is probably
737 * interested in whether 'dpif' actually has a port 'devname', so that
738 * it's not an issue worth logging if it doesn't. Other errors are
739 * uncommon and more likely to indicate a real problem. */
740 VLOG_RL(&error_rl, error == ENODEV ? VLL_DBG : VLL_WARN,
741 "%s: failed to query port %s: %s",
742 dpif_name(dpif), devname, ovs_strerror(error));
743 }
744 return error;
745 }
746
747 /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
748 * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
749 * flows whose packets arrived on port 'port_no'.
750 *
751 * A 'port_no' of ODPP_NONE is a special case: it returns a reserved PID, not
752 * allocated to any port, that the client may use for special purposes.
753 *
754 * The return value is only meaningful when DPIF_UC_ACTION has been enabled in
755 * the 'dpif''s listen mask. It is allowed to change when DPIF_UC_ACTION is
756 * disabled and then re-enabled, so a client that does that must be prepared to
757 * update all of the flows that it installed that contain
758 * OVS_ACTION_ATTR_USERSPACE actions. */
759 uint32_t
760 dpif_port_get_pid(const struct dpif *dpif, odp_port_t port_no)
761 {
762 return (dpif->dpif_class->port_get_pid
763 ? (dpif->dpif_class->port_get_pid)(dpif, port_no)
764 : 0);
765 }
766
767 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
768 * the port's name into the 'name_size' bytes in 'name', ensuring that the
769 * result is null-terminated. On failure, returns a positive errno value and
770 * makes 'name' the empty string. */
771 int
772 dpif_port_get_name(struct dpif *dpif, odp_port_t port_no,
773 char *name, size_t name_size)
774 {
775 struct dpif_port port;
776 int error;
777
778 ovs_assert(name_size > 0);
779
780 error = dpif_port_query_by_number(dpif, port_no, &port);
781 if (!error) {
782 ovs_strlcpy(name, port.name, name_size);
783 dpif_port_destroy(&port);
784 } else {
785 *name = '\0';
786 }
787 return error;
788 }
789
790 /* Initializes 'dump' to begin dumping the ports in a dpif.
791 *
792 * This function provides no status indication. An error status for the entire
793 * dump operation is provided when it is completed by calling
794 * dpif_port_dump_done().
795 */
796 void
797 dpif_port_dump_start(struct dpif_port_dump *dump, const struct dpif *dpif)
798 {
799 dump->dpif = dpif;
800 dump->error = dpif->dpif_class->port_dump_start(dpif, &dump->state);
801 log_operation(dpif, "port_dump_start", dump->error);
802 }
803
804 /* Attempts to retrieve another port from 'dump', which must have been
805 * initialized with dpif_port_dump_start(). On success, stores a new dpif_port
806 * into 'port' and returns true. On failure, returns false.
807 *
808 * Failure might indicate an actual error or merely that the last port has been
809 * dumped. An error status for the entire dump operation is provided when it
810 * is completed by calling dpif_port_dump_done().
811 *
812 * The dpif owns the data stored in 'port'. It will remain valid until at
813 * least the next time 'dump' is passed to dpif_port_dump_next() or
814 * dpif_port_dump_done(). */
815 bool
816 dpif_port_dump_next(struct dpif_port_dump *dump, struct dpif_port *port)
817 {
818 const struct dpif *dpif = dump->dpif;
819
820 if (dump->error) {
821 return false;
822 }
823
824 dump->error = dpif->dpif_class->port_dump_next(dpif, dump->state, port);
825 if (dump->error == EOF) {
826 VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all ports", dpif_name(dpif));
827 } else {
828 log_operation(dpif, "port_dump_next", dump->error);
829 }
830
831 if (dump->error) {
832 dpif->dpif_class->port_dump_done(dpif, dump->state);
833 return false;
834 }
835 return true;
836 }
837
838 /* Completes port table dump operation 'dump', which must have been initialized
839 * with dpif_port_dump_start(). Returns 0 if the dump operation was
840 * error-free, otherwise a positive errno value describing the problem. */
841 int
842 dpif_port_dump_done(struct dpif_port_dump *dump)
843 {
844 const struct dpif *dpif = dump->dpif;
845 if (!dump->error) {
846 dump->error = dpif->dpif_class->port_dump_done(dpif, dump->state);
847 log_operation(dpif, "port_dump_done", dump->error);
848 }
849 return dump->error == EOF ? 0 : dump->error;
850 }
851
852 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
853 * 'dpif' has changed, this function does one of the following:
854 *
855 * - Stores the name of the device that was added to or deleted from 'dpif' in
856 * '*devnamep' and returns 0. The caller is responsible for freeing
857 * '*devnamep' (with free()) when it no longer needs it.
858 *
859 * - Returns ENOBUFS and sets '*devnamep' to NULL.
860 *
861 * This function may also return 'false positives', where it returns 0 and
862 * '*devnamep' names a device that was not actually added or deleted or it
863 * returns ENOBUFS without any change.
864 *
865 * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also
866 * return other positive errno values to indicate that something has gone
867 * wrong. */
868 int
869 dpif_port_poll(const struct dpif *dpif, char **devnamep)
870 {
871 int error = dpif->dpif_class->port_poll(dpif, devnamep);
872 if (error) {
873 *devnamep = NULL;
874 }
875 return error;
876 }
877
878 /* Arranges for the poll loop to wake up when port_poll(dpif) will return a
879 * value other than EAGAIN. */
880 void
881 dpif_port_poll_wait(const struct dpif *dpif)
882 {
883 dpif->dpif_class->port_poll_wait(dpif);
884 }
885
886 /* Extracts the flow stats for a packet. The 'flow' and 'packet'
887 * arguments must have been initialized through a call to flow_extract().
888 * 'used' is stored into stats->used. */
889 void
890 dpif_flow_stats_extract(const struct flow *flow, const struct dp_packet *packet,
891 long long int used, struct dpif_flow_stats *stats)
892 {
893 stats->tcp_flags = ntohs(flow->tcp_flags);
894 stats->n_bytes = dp_packet_size(packet);
895 stats->n_packets = 1;
896 stats->used = used;
897 }
898
899 /* Appends a human-readable representation of 'stats' to 's'. */
900 void
901 dpif_flow_stats_format(const struct dpif_flow_stats *stats, struct ds *s)
902 {
903 ds_put_format(s, "packets:%"PRIu64", bytes:%"PRIu64", used:",
904 stats->n_packets, stats->n_bytes);
905 if (stats->used) {
906 ds_put_format(s, "%.3fs", (time_msec() - stats->used) / 1000.0);
907 } else {
908 ds_put_format(s, "never");
909 }
910 if (stats->tcp_flags) {
911 ds_put_cstr(s, ", flags:");
912 packet_format_tcp_flags(s, stats->tcp_flags);
913 }
914 }
915
916 /* Places the hash of the 'key_len' bytes starting at 'key' into '*hash'. */
917 void
918 dpif_flow_hash(const struct dpif *dpif OVS_UNUSED,
919 const void *key, size_t key_len, ovs_u128 *hash)
920 {
921 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
922 static uint32_t secret;
923
924 if (ovsthread_once_start(&once)) {
925 secret = random_uint32();
926 ovsthread_once_done(&once);
927 }
928 hash_bytes128(key, key_len, secret, hash);
929 uuid_set_bits_v4((struct uuid *)hash);
930 }
931
932 /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
933 * positive errno value. */
934 int
935 dpif_flow_flush(struct dpif *dpif)
936 {
937 int error;
938
939 COVERAGE_INC(dpif_flow_flush);
940
941 error = dpif->dpif_class->flow_flush(dpif);
942 log_operation(dpif, "flow_flush", error);
943 return error;
944 }
945
946 /* Attempts to install 'key' into the datapath, fetches it, then deletes it.
947 * Returns true if the datapath supported installing 'flow', false otherwise.
948 */
949 bool
950 dpif_probe_feature(struct dpif *dpif, const char *name,
951 const struct ofpbuf *key, const struct ofpbuf *actions,
952 const ovs_u128 *ufid)
953 {
954 struct dpif_flow flow;
955 struct ofpbuf reply;
956 uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
957 bool enable_feature = false;
958 int error;
959 const struct nlattr *nl_actions = actions ? actions->data : NULL;
960 const size_t nl_actions_size = actions ? actions->size : 0;
961
962 /* Use DPIF_FP_MODIFY to cover the case where ovs-vswitchd is killed (and
963 * restarted) at just the right time such that feature probes from the
964 * previous run are still present in the datapath. */
965 error = dpif_flow_put(dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY | DPIF_FP_PROBE,
966 key->data, key->size, NULL, 0,
967 nl_actions, nl_actions_size,
968 ufid, NON_PMD_CORE_ID, NULL);
969 if (error) {
970 if (error != EINVAL && error != EOVERFLOW) {
971 VLOG_WARN("%s: %s flow probe failed (%s)",
972 dpif_name(dpif), name, ovs_strerror(error));
973 }
974 return false;
975 }
976
977 ofpbuf_use_stack(&reply, &stub, sizeof stub);
978 error = dpif_flow_get(dpif, key->data, key->size, ufid,
979 NON_PMD_CORE_ID, &reply, &flow);
980 if (!error
981 && (!ufid || (flow.ufid_present
982 && ovs_u128_equals(*ufid, flow.ufid)))) {
983 enable_feature = true;
984 }
985
986 error = dpif_flow_del(dpif, key->data, key->size, ufid,
987 NON_PMD_CORE_ID, NULL);
988 if (error) {
989 VLOG_WARN("%s: failed to delete %s feature probe flow",
990 dpif_name(dpif), name);
991 }
992
993 return enable_feature;
994 }
995
996 /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_GET. */
997 int
998 dpif_flow_get(struct dpif *dpif,
999 const struct nlattr *key, size_t key_len, const ovs_u128 *ufid,
1000 const unsigned pmd_id, struct ofpbuf *buf, struct dpif_flow *flow)
1001 {
1002 struct dpif_op *opp;
1003 struct dpif_op op;
1004
1005 op.type = DPIF_OP_FLOW_GET;
1006 op.flow_get.key = key;
1007 op.flow_get.key_len = key_len;
1008 op.flow_get.ufid = ufid;
1009 op.flow_get.pmd_id = pmd_id;
1010 op.flow_get.buffer = buf;
1011
1012 memset(flow, 0, sizeof *flow);
1013 op.flow_get.flow = flow;
1014 op.flow_get.flow->key = key;
1015 op.flow_get.flow->key_len = key_len;
1016
1017 opp = &op;
1018 dpif_operate(dpif, &opp, 1, DPIF_OFFLOAD_AUTO);
1019
1020 return op.error;
1021 }
1022
1023 /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_PUT. */
1024 int
1025 dpif_flow_put(struct dpif *dpif, enum dpif_flow_put_flags flags,
1026 const struct nlattr *key, size_t key_len,
1027 const struct nlattr *mask, size_t mask_len,
1028 const struct nlattr *actions, size_t actions_len,
1029 const ovs_u128 *ufid, const unsigned pmd_id,
1030 struct dpif_flow_stats *stats)
1031 {
1032 struct dpif_op *opp;
1033 struct dpif_op op;
1034
1035 op.type = DPIF_OP_FLOW_PUT;
1036 op.flow_put.flags = flags;
1037 op.flow_put.key = key;
1038 op.flow_put.key_len = key_len;
1039 op.flow_put.mask = mask;
1040 op.flow_put.mask_len = mask_len;
1041 op.flow_put.actions = actions;
1042 op.flow_put.actions_len = actions_len;
1043 op.flow_put.ufid = ufid;
1044 op.flow_put.pmd_id = pmd_id;
1045 op.flow_put.stats = stats;
1046
1047 opp = &op;
1048 dpif_operate(dpif, &opp, 1, DPIF_OFFLOAD_AUTO);
1049
1050 return op.error;
1051 }
1052
1053 /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_DEL. */
1054 int
1055 dpif_flow_del(struct dpif *dpif,
1056 const struct nlattr *key, size_t key_len, const ovs_u128 *ufid,
1057 const unsigned pmd_id, struct dpif_flow_stats *stats)
1058 {
1059 struct dpif_op *opp;
1060 struct dpif_op op;
1061
1062 op.type = DPIF_OP_FLOW_DEL;
1063 op.flow_del.key = key;
1064 op.flow_del.key_len = key_len;
1065 op.flow_del.ufid = ufid;
1066 op.flow_del.pmd_id = pmd_id;
1067 op.flow_del.stats = stats;
1068 op.flow_del.terse = false;
1069
1070 opp = &op;
1071 dpif_operate(dpif, &opp, 1, DPIF_OFFLOAD_AUTO);
1072
1073 return op.error;
1074 }
1075
1076 /* Creates and returns a new 'struct dpif_flow_dump' for iterating through the
1077 * flows in 'dpif'. If 'terse' is true, then only UFID and statistics will
1078 * be returned in the dump. Otherwise, all fields will be returned.
1079 *
1080 * This function always successfully returns a dpif_flow_dump. Error
1081 * reporting is deferred to dpif_flow_dump_destroy(). */
1082 struct dpif_flow_dump *
1083 dpif_flow_dump_create(const struct dpif *dpif, bool terse,
1084 struct dpif_flow_dump_types *types)
1085 {
1086 return dpif->dpif_class->flow_dump_create(dpif, terse, types);
1087 }
1088
1089 /* Destroys 'dump', which must have been created with dpif_flow_dump_create().
1090 * All dpif_flow_dump_thread structures previously created for 'dump' must
1091 * previously have been destroyed.
1092 *
1093 * Returns 0 if the dump operation was error-free, otherwise a positive errno
1094 * value describing the problem. */
1095 int
1096 dpif_flow_dump_destroy(struct dpif_flow_dump *dump)
1097 {
1098 const struct dpif *dpif = dump->dpif;
1099 int error = dpif->dpif_class->flow_dump_destroy(dump);
1100 log_operation(dpif, "flow_dump_destroy", error);
1101 return error == EOF ? 0 : error;
1102 }
1103
1104 /* Returns new thread-local state for use with dpif_flow_dump_next(). */
1105 struct dpif_flow_dump_thread *
1106 dpif_flow_dump_thread_create(struct dpif_flow_dump *dump)
1107 {
1108 return dump->dpif->dpif_class->flow_dump_thread_create(dump);
1109 }
1110
1111 /* Releases 'thread'. */
1112 void
1113 dpif_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread)
1114 {
1115 thread->dpif->dpif_class->flow_dump_thread_destroy(thread);
1116 }
1117
1118 /* Attempts to retrieve up to 'max_flows' more flows from 'thread'. Returns 0
1119 * if and only if no flows remained to be retrieved, otherwise a positive
1120 * number reflecting the number of elements in 'flows[]' that were updated.
1121 * The number of flows returned might be less than 'max_flows' because
1122 * fewer than 'max_flows' remained, because this particular datapath does not
1123 * benefit from batching, or because an error occurred partway through
1124 * retrieval. Thus, the caller should continue calling until a 0 return value,
1125 * even if intermediate return values are less than 'max_flows'.
1126 *
1127 * No error status is immediately provided. An error status for the entire
1128 * dump operation is provided when it is completed by calling
1129 * dpif_flow_dump_destroy().
1130 *
1131 * All of the data stored into 'flows' is owned by the datapath, not by the
1132 * caller, and the caller must not modify or free it. The datapath guarantees
1133 * that it remains accessible and unchanged until the first of:
1134 * - The next call to dpif_flow_dump_next() for 'thread', or
1135 * - The next rcu quiescent period. */
1136 int
1137 dpif_flow_dump_next(struct dpif_flow_dump_thread *thread,
1138 struct dpif_flow *flows, int max_flows)
1139 {
1140 struct dpif *dpif = thread->dpif;
1141 int n;
1142
1143 ovs_assert(max_flows > 0);
1144 n = dpif->dpif_class->flow_dump_next(thread, flows, max_flows);
1145 if (n > 0) {
1146 struct dpif_flow *f;
1147
1148 for (f = flows; f < &flows[n]
1149 && should_log_flow_message(&this_module, 0); f++) {
1150 log_flow_message(dpif, 0, &this_module, "flow_dump",
1151 f->key, f->key_len, f->mask, f->mask_len,
1152 &f->ufid, &f->stats, f->actions, f->actions_len);
1153 }
1154 } else {
1155 VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all flows", dpif_name(dpif));
1156 }
1157 return n;
1158 }
1159
1160 struct dpif_execute_helper_aux {
1161 struct dpif *dpif;
1162 const struct flow *flow;
1163 int error;
1164 const struct nlattr *meter_action; /* Non-NULL, if have a meter action. */
1165 };
1166
1167 /* This is called for actions that need the context of the datapath to be
1168 * meaningful. */
1169 static void
1170 dpif_execute_helper_cb(void *aux_, struct dp_packet_batch *packets_,
1171 const struct nlattr *action, bool should_steal)
1172 {
1173 struct dpif_execute_helper_aux *aux = aux_;
1174 int type = nl_attr_type(action);
1175 struct dp_packet *packet = packets_->packets[0];
1176
1177 ovs_assert(dp_packet_batch_size(packets_) == 1);
1178
1179 switch ((enum ovs_action_attr)type) {
1180 case OVS_ACTION_ATTR_METER:
1181 /* Maintain a pointer to the first meter action seen. */
1182 if (!aux->meter_action) {
1183 aux->meter_action = action;
1184 }
1185 break;
1186
1187 case OVS_ACTION_ATTR_CT:
1188 case OVS_ACTION_ATTR_OUTPUT:
1189 case OVS_ACTION_ATTR_TUNNEL_PUSH:
1190 case OVS_ACTION_ATTR_TUNNEL_POP:
1191 case OVS_ACTION_ATTR_USERSPACE:
1192 case OVS_ACTION_ATTR_RECIRC: {
1193 struct dpif_execute execute;
1194 struct ofpbuf execute_actions;
1195 uint64_t stub[256 / 8];
1196 struct pkt_metadata *md = &packet->md;
1197
1198 if (flow_tnl_dst_is_set(&md->tunnel) || aux->meter_action) {
1199 ofpbuf_use_stub(&execute_actions, stub, sizeof stub);
1200
1201 if (aux->meter_action) {
1202 const struct nlattr *a = aux->meter_action;
1203
1204 /* XXX: This code collects meter actions since the last action
1205 * execution via the datapath to be executed right before the
1206 * current action that needs to be executed by the datapath.
1207 * This is only an approximation, but better than nothing.
1208 * Fundamentally, we should have a mechanism by which the
1209 * datapath could return the result of the meter action so that
1210 * we could execute them at the right order. */
1211 do {
1212 ofpbuf_put(&execute_actions, a, NLA_ALIGN(a->nla_len));
1213 /* Find next meter action before 'action', if any. */
1214 do {
1215 a = nl_attr_next(a);
1216 } while (a != action &&
1217 nl_attr_type(a) != OVS_ACTION_ATTR_METER);
1218 } while (a != action);
1219 }
1220
1221 /* The Linux kernel datapath throws away the tunnel information
1222 * that we supply as metadata. We have to use a "set" action to
1223 * supply it. */
1224 if (md->tunnel.ip_dst) {
1225 odp_put_tunnel_action(&md->tunnel, &execute_actions, NULL);
1226 }
1227 ofpbuf_put(&execute_actions, action, NLA_ALIGN(action->nla_len));
1228
1229 execute.actions = execute_actions.data;
1230 execute.actions_len = execute_actions.size;
1231 } else {
1232 execute.actions = action;
1233 execute.actions_len = NLA_ALIGN(action->nla_len);
1234 }
1235
1236 struct dp_packet *clone = NULL;
1237 uint32_t cutlen = dp_packet_get_cutlen(packet);
1238 if (cutlen && (type == OVS_ACTION_ATTR_OUTPUT
1239 || type == OVS_ACTION_ATTR_TUNNEL_PUSH
1240 || type == OVS_ACTION_ATTR_TUNNEL_POP
1241 || type == OVS_ACTION_ATTR_USERSPACE)) {
1242 dp_packet_reset_cutlen(packet);
1243 if (!should_steal) {
1244 packet = clone = dp_packet_clone(packet);
1245 }
1246 dp_packet_set_size(packet, dp_packet_size(packet) - cutlen);
1247 }
1248
1249 execute.packet = packet;
1250 execute.flow = aux->flow;
1251 execute.needs_help = false;
1252 execute.probe = false;
1253 execute.mtu = 0;
1254 aux->error = dpif_execute(aux->dpif, &execute);
1255 log_execute_message(aux->dpif, &this_module, &execute,
1256 true, aux->error);
1257
1258 dp_packet_delete(clone);
1259
1260 if (flow_tnl_dst_is_set(&md->tunnel) || aux->meter_action) {
1261 ofpbuf_uninit(&execute_actions);
1262
1263 /* Do not re-use the same meters for later output actions. */
1264 aux->meter_action = NULL;
1265 }
1266 break;
1267 }
1268
1269 case OVS_ACTION_ATTR_HASH:
1270 case OVS_ACTION_ATTR_PUSH_VLAN:
1271 case OVS_ACTION_ATTR_POP_VLAN:
1272 case OVS_ACTION_ATTR_PUSH_MPLS:
1273 case OVS_ACTION_ATTR_POP_MPLS:
1274 case OVS_ACTION_ATTR_SET:
1275 case OVS_ACTION_ATTR_SET_MASKED:
1276 case OVS_ACTION_ATTR_SAMPLE:
1277 case OVS_ACTION_ATTR_TRUNC:
1278 case OVS_ACTION_ATTR_PUSH_ETH:
1279 case OVS_ACTION_ATTR_POP_ETH:
1280 case OVS_ACTION_ATTR_CLONE:
1281 case OVS_ACTION_ATTR_PUSH_NSH:
1282 case OVS_ACTION_ATTR_POP_NSH:
1283 case OVS_ACTION_ATTR_CT_CLEAR:
1284 case OVS_ACTION_ATTR_UNSPEC:
1285 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
1286 case __OVS_ACTION_ATTR_MAX:
1287 OVS_NOT_REACHED();
1288 }
1289 dp_packet_delete_batch(packets_, should_steal);
1290 }
1291
1292 /* Executes 'execute' by performing most of the actions in userspace and
1293 * passing the fully constructed packets to 'dpif' for output and userspace
1294 * actions.
1295 *
1296 * This helps with actions that a given 'dpif' doesn't implement directly. */
1297 static int
1298 dpif_execute_with_help(struct dpif *dpif, struct dpif_execute *execute)
1299 {
1300 struct dpif_execute_helper_aux aux = {dpif, execute->flow, 0, NULL};
1301 struct dp_packet_batch pb;
1302
1303 COVERAGE_INC(dpif_execute_with_help);
1304
1305 dp_packet_batch_init_packet(&pb, execute->packet);
1306 odp_execute_actions(&aux, &pb, false, execute->actions,
1307 execute->actions_len, dpif_execute_helper_cb);
1308 return aux.error;
1309 }
1310
1311 /* Returns true if the datapath needs help executing 'execute'. */
1312 static bool
1313 dpif_execute_needs_help(const struct dpif_execute *execute)
1314 {
1315 return execute->needs_help || nl_attr_oversized(execute->actions_len);
1316 }
1317
1318 /* A dpif_operate() wrapper for performing a single DPIF_OP_EXECUTE. */
1319 int
1320 dpif_execute(struct dpif *dpif, struct dpif_execute *execute)
1321 {
1322 if (execute->actions_len) {
1323 struct dpif_op *opp;
1324 struct dpif_op op;
1325
1326 op.type = DPIF_OP_EXECUTE;
1327 op.execute = *execute;
1328
1329 opp = &op;
1330 dpif_operate(dpif, &opp, 1, DPIF_OFFLOAD_AUTO);
1331
1332 return op.error;
1333 } else {
1334 return 0;
1335 }
1336 }
1337
1338 /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order in
1339 * which they are specified. Places each operation's results in the "output"
1340 * members documented in comments, and 0 in the 'error' member on success or a
1341 * positive errno on failure.
1342 */
1343 void
1344 dpif_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops,
1345 enum dpif_offload_type offload_type)
1346 {
1347 if (offload_type == DPIF_OFFLOAD_ALWAYS && !netdev_is_flow_api_enabled()) {
1348 size_t i;
1349 for (i = 0; i < n_ops; i++) {
1350 struct dpif_op *op = ops[i];
1351 op->error = EINVAL;
1352 }
1353 return;
1354 }
1355
1356 while (n_ops > 0) {
1357 size_t chunk;
1358
1359 /* Count 'chunk', the number of ops that can be executed without
1360 * needing any help. Ops that need help should be rare, so we
1361 * expect this to ordinarily be 'n_ops', that is, all the ops. */
1362 for (chunk = 0; chunk < n_ops; chunk++) {
1363 struct dpif_op *op = ops[chunk];
1364
1365 if (op->type == DPIF_OP_EXECUTE
1366 && dpif_execute_needs_help(&op->execute)) {
1367 break;
1368 }
1369 }
1370
1371 if (chunk) {
1372 /* Execute a chunk full of ops that the dpif provider can
1373 * handle itself, without help. */
1374 size_t i;
1375
1376 dpif->dpif_class->operate(dpif, ops, chunk, offload_type);
1377
1378 for (i = 0; i < chunk; i++) {
1379 struct dpif_op *op = ops[i];
1380 int error = op->error;
1381
1382 switch (op->type) {
1383 case DPIF_OP_FLOW_PUT: {
1384 struct dpif_flow_put *put = &op->flow_put;
1385
1386 COVERAGE_INC(dpif_flow_put);
1387 log_flow_put_message(dpif, &this_module, put, error);
1388 if (error && put->stats) {
1389 memset(put->stats, 0, sizeof *put->stats);
1390 }
1391 break;
1392 }
1393
1394 case DPIF_OP_FLOW_GET: {
1395 struct dpif_flow_get *get = &op->flow_get;
1396
1397 COVERAGE_INC(dpif_flow_get);
1398 if (error) {
1399 memset(get->flow, 0, sizeof *get->flow);
1400 }
1401 log_flow_get_message(dpif, &this_module, get, error);
1402
1403 break;
1404 }
1405
1406 case DPIF_OP_FLOW_DEL: {
1407 struct dpif_flow_del *del = &op->flow_del;
1408
1409 COVERAGE_INC(dpif_flow_del);
1410 log_flow_del_message(dpif, &this_module, del, error);
1411 if (error && del->stats) {
1412 memset(del->stats, 0, sizeof *del->stats);
1413 }
1414 break;
1415 }
1416
1417 case DPIF_OP_EXECUTE:
1418 COVERAGE_INC(dpif_execute);
1419 log_execute_message(dpif, &this_module, &op->execute,
1420 false, error);
1421 break;
1422 }
1423 }
1424
1425 ops += chunk;
1426 n_ops -= chunk;
1427 } else {
1428 /* Help the dpif provider to execute one op. */
1429 struct dpif_op *op = ops[0];
1430
1431 COVERAGE_INC(dpif_execute);
1432 op->error = dpif_execute_with_help(dpif, &op->execute);
1433 ops++;
1434 n_ops--;
1435 }
1436 }
1437 }
1438
1439 /* Returns a string that represents 'type', for use in log messages. */
1440 const char *
1441 dpif_upcall_type_to_string(enum dpif_upcall_type type)
1442 {
1443 switch (type) {
1444 case DPIF_UC_MISS: return "miss";
1445 case DPIF_UC_ACTION: return "action";
1446 case DPIF_N_UC_TYPES: default: return "<unknown>";
1447 }
1448 }
1449
1450 /* Enables or disables receiving packets with dpif_recv() on 'dpif'. Returns 0
1451 * if successful, otherwise a positive errno value.
1452 *
1453 * Turning packet receive off and then back on may change the Netlink PID
1454 * assignments returned by dpif_port_get_pid(). If the client does this, it
1455 * must update all of the flows that have OVS_ACTION_ATTR_USERSPACE actions
1456 * using the new PID assignment. */
1457 int
1458 dpif_recv_set(struct dpif *dpif, bool enable)
1459 {
1460 int error = 0;
1461
1462 if (dpif->dpif_class->recv_set) {
1463 error = dpif->dpif_class->recv_set(dpif, enable);
1464 log_operation(dpif, "recv_set", error);
1465 }
1466 return error;
1467 }
1468
1469 /* Refreshes the poll loops and Netlink sockets associated to each port,
1470 * when the number of upcall handlers (upcall receiving thread) is changed
1471 * to 'n_handlers' and receiving packets for 'dpif' is enabled by
1472 * recv_set().
1473 *
1474 * Since multiple upcall handlers can read upcalls simultaneously from
1475 * 'dpif', each port can have multiple Netlink sockets, one per upcall
1476 * handler. So, handlers_set() is responsible for the following tasks:
1477 *
1478 * When receiving upcall is enabled, extends or creates the
1479 * configuration to support:
1480 *
1481 * - 'n_handlers' Netlink sockets for each port.
1482 *
1483 * - 'n_handlers' poll loops, one for each upcall handler.
1484 *
1485 * - registering the Netlink sockets for the same upcall handler to
1486 * the corresponding poll loop.
1487 *
1488 * Returns 0 if successful, otherwise a positive errno value. */
1489 int
1490 dpif_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1491 {
1492 int error = 0;
1493
1494 if (dpif->dpif_class->handlers_set) {
1495 error = dpif->dpif_class->handlers_set(dpif, n_handlers);
1496 log_operation(dpif, "handlers_set", error);
1497 }
1498 return error;
1499 }
1500
1501 void
1502 dpif_register_dp_purge_cb(struct dpif *dpif, dp_purge_callback *cb, void *aux)
1503 {
1504 if (dpif->dpif_class->register_dp_purge_cb) {
1505 dpif->dpif_class->register_dp_purge_cb(dpif, cb, aux);
1506 }
1507 }
1508
1509 void
1510 dpif_register_upcall_cb(struct dpif *dpif, upcall_callback *cb, void *aux)
1511 {
1512 if (dpif->dpif_class->register_upcall_cb) {
1513 dpif->dpif_class->register_upcall_cb(dpif, cb, aux);
1514 }
1515 }
1516
1517 void
1518 dpif_enable_upcall(struct dpif *dpif)
1519 {
1520 if (dpif->dpif_class->enable_upcall) {
1521 dpif->dpif_class->enable_upcall(dpif);
1522 }
1523 }
1524
1525 void
1526 dpif_disable_upcall(struct dpif *dpif)
1527 {
1528 if (dpif->dpif_class->disable_upcall) {
1529 dpif->dpif_class->disable_upcall(dpif);
1530 }
1531 }
1532
1533 void
1534 dpif_print_packet(struct dpif *dpif, struct dpif_upcall *upcall)
1535 {
1536 if (!VLOG_DROP_DBG(&dpmsg_rl)) {
1537 struct ds flow;
1538 char *packet;
1539
1540 packet = ofp_dp_packet_to_string(&upcall->packet);
1541
1542 ds_init(&flow);
1543 odp_flow_key_format(upcall->key, upcall->key_len, &flow);
1544
1545 VLOG_DBG("%s: %s upcall:\n%s\n%s",
1546 dpif_name(dpif), dpif_upcall_type_to_string(upcall->type),
1547 ds_cstr(&flow), packet);
1548
1549 ds_destroy(&flow);
1550 free(packet);
1551 }
1552 }
1553
1554 /* Pass custom configuration to the datapath implementation. Some of the
1555 * changes can be postponed until dpif_run() is called. */
1556 int
1557 dpif_set_config(struct dpif *dpif, const struct smap *cfg)
1558 {
1559 int error = 0;
1560
1561 if (dpif->dpif_class->set_config) {
1562 error = dpif->dpif_class->set_config(dpif, cfg);
1563 if (error) {
1564 log_operation(dpif, "set_config", error);
1565 }
1566 }
1567
1568 return error;
1569 }
1570
1571 /* Polls for an upcall from 'dpif' for an upcall handler. Since there can
1572 * be multiple poll loops, 'handler_id' is needed as index to identify the
1573 * corresponding poll loop. If successful, stores the upcall into '*upcall',
1574 * using 'buf' for storage. Should only be called if 'recv_set' has been used
1575 * to enable receiving packets from 'dpif'.
1576 *
1577 * 'upcall->key' and 'upcall->userdata' point into data in the caller-provided
1578 * 'buf', so their memory cannot be freed separately from 'buf'.
1579 *
1580 * The caller owns the data of 'upcall->packet' and may modify it. If
1581 * packet's headroom is exhausted as it is manipulated, 'upcall->packet'
1582 * will be reallocated. This requires the data of 'upcall->packet' to be
1583 * released with ofpbuf_uninit() before 'upcall' is destroyed. However,
1584 * when an error is returned, the 'upcall->packet' may be uninitialized
1585 * and should not be released.
1586 *
1587 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
1588 * if no upcall is immediately available. */
1589 int
1590 dpif_recv(struct dpif *dpif, uint32_t handler_id, struct dpif_upcall *upcall,
1591 struct ofpbuf *buf)
1592 {
1593 int error = EAGAIN;
1594
1595 if (dpif->dpif_class->recv) {
1596 error = dpif->dpif_class->recv(dpif, handler_id, upcall, buf);
1597 if (!error) {
1598 dpif_print_packet(dpif, upcall);
1599 } else if (error != EAGAIN) {
1600 log_operation(dpif, "recv", error);
1601 }
1602 }
1603 return error;
1604 }
1605
1606 /* Discards all messages that would otherwise be received by dpif_recv() on
1607 * 'dpif'. */
1608 void
1609 dpif_recv_purge(struct dpif *dpif)
1610 {
1611 COVERAGE_INC(dpif_purge);
1612 if (dpif->dpif_class->recv_purge) {
1613 dpif->dpif_class->recv_purge(dpif);
1614 }
1615 }
1616
1617 /* Arranges for the poll loop for an upcall handler to wake up when 'dpif'
1618 * 'dpif' has a message queued to be received with the recv member
1619 * function. Since there can be multiple poll loops, 'handler_id' is
1620 * needed as index to identify the corresponding poll loop. */
1621 void
1622 dpif_recv_wait(struct dpif *dpif, uint32_t handler_id)
1623 {
1624 if (dpif->dpif_class->recv_wait) {
1625 dpif->dpif_class->recv_wait(dpif, handler_id);
1626 }
1627 }
1628
1629 /*
1630 * Return the datapath version. Caller is responsible for freeing
1631 * the string.
1632 */
1633 char *
1634 dpif_get_dp_version(const struct dpif *dpif)
1635 {
1636 char *version = NULL;
1637
1638 if (dpif->dpif_class->get_datapath_version) {
1639 version = dpif->dpif_class->get_datapath_version();
1640 }
1641
1642 return version;
1643 }
1644
1645 /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
1646 * and '*engine_id', respectively. */
1647 void
1648 dpif_get_netflow_ids(const struct dpif *dpif,
1649 uint8_t *engine_type, uint8_t *engine_id)
1650 {
1651 *engine_type = dpif->netflow_engine_type;
1652 *engine_id = dpif->netflow_engine_id;
1653 }
1654
1655 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority
1656 * value used for setting packet priority.
1657 * On success, returns 0 and stores the priority into '*priority'.
1658 * On failure, returns a positive errno value and stores 0 into '*priority'. */
1659 int
1660 dpif_queue_to_priority(const struct dpif *dpif, uint32_t queue_id,
1661 uint32_t *priority)
1662 {
1663 int error = (dpif->dpif_class->queue_to_priority
1664 ? dpif->dpif_class->queue_to_priority(dpif, queue_id,
1665 priority)
1666 : EOPNOTSUPP);
1667 if (error) {
1668 *priority = 0;
1669 }
1670 log_operation(dpif, "queue_to_priority", error);
1671 return error;
1672 }
1673 \f
1674 void
1675 dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class,
1676 const char *name,
1677 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1678 {
1679 dpif->dpif_class = dpif_class;
1680 dpif->base_name = xstrdup(name);
1681 dpif->full_name = xasprintf("%s@%s", dpif_class->type, name);
1682 dpif->netflow_engine_type = netflow_engine_type;
1683 dpif->netflow_engine_id = netflow_engine_id;
1684 }
1685
1686 /* Undoes the results of initialization.
1687 *
1688 * Normally this function only needs to be called from dpif_close().
1689 * However, it may be called by providers due to an error on opening
1690 * that occurs after initialization. It this case dpif_close() would
1691 * never be called. */
1692 void
1693 dpif_uninit(struct dpif *dpif, bool close)
1694 {
1695 char *base_name = dpif->base_name;
1696 char *full_name = dpif->full_name;
1697
1698 if (close) {
1699 dpif->dpif_class->close(dpif);
1700 }
1701
1702 free(base_name);
1703 free(full_name);
1704 }
1705 \f
1706 static void
1707 log_operation(const struct dpif *dpif, const char *operation, int error)
1708 {
1709 if (!error) {
1710 VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation);
1711 } else if (ofperr_is_valid(error)) {
1712 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1713 dpif_name(dpif), operation, ofperr_get_name(error));
1714 } else {
1715 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1716 dpif_name(dpif), operation, ovs_strerror(error));
1717 }
1718 }
1719
1720 static enum vlog_level
1721 flow_message_log_level(int error)
1722 {
1723 /* If flows arrive in a batch, userspace may push down multiple
1724 * unique flow definitions that overlap when wildcards are applied.
1725 * Kernels that support flow wildcarding will reject these flows as
1726 * duplicates (EEXIST), so lower the log level to debug for these
1727 * types of messages. */
1728 return (error && error != EEXIST) ? VLL_WARN : VLL_DBG;
1729 }
1730
1731 static bool
1732 should_log_flow_message(const struct vlog_module *module, int error)
1733 {
1734 return !vlog_should_drop(module, flow_message_log_level(error),
1735 error ? &error_rl : &dpmsg_rl);
1736 }
1737
1738 void
1739 log_flow_message(const struct dpif *dpif, int error,
1740 const struct vlog_module *module,
1741 const char *operation,
1742 const struct nlattr *key, size_t key_len,
1743 const struct nlattr *mask, size_t mask_len,
1744 const ovs_u128 *ufid, const struct dpif_flow_stats *stats,
1745 const struct nlattr *actions, size_t actions_len)
1746 {
1747 struct ds ds = DS_EMPTY_INITIALIZER;
1748 ds_put_format(&ds, "%s: ", dpif_name(dpif));
1749 if (error) {
1750 ds_put_cstr(&ds, "failed to ");
1751 }
1752 ds_put_format(&ds, "%s ", operation);
1753 if (error) {
1754 ds_put_format(&ds, "(%s) ", ovs_strerror(error));
1755 }
1756 if (ufid) {
1757 odp_format_ufid(ufid, &ds);
1758 ds_put_cstr(&ds, " ");
1759 }
1760 odp_flow_format(key, key_len, mask, mask_len, NULL, &ds, true);
1761 if (stats) {
1762 ds_put_cstr(&ds, ", ");
1763 dpif_flow_stats_format(stats, &ds);
1764 }
1765 if (actions || actions_len) {
1766 ds_put_cstr(&ds, ", actions:");
1767 format_odp_actions(&ds, actions, actions_len, NULL);
1768 }
1769 vlog(module, flow_message_log_level(error), "%s", ds_cstr(&ds));
1770 ds_destroy(&ds);
1771 }
1772
1773 void
1774 log_flow_put_message(const struct dpif *dpif,
1775 const struct vlog_module *module,
1776 const struct dpif_flow_put *put,
1777 int error)
1778 {
1779 if (should_log_flow_message(module, error)
1780 && !(put->flags & DPIF_FP_PROBE)) {
1781 struct ds s;
1782
1783 ds_init(&s);
1784 ds_put_cstr(&s, "put");
1785 if (put->flags & DPIF_FP_CREATE) {
1786 ds_put_cstr(&s, "[create]");
1787 }
1788 if (put->flags & DPIF_FP_MODIFY) {
1789 ds_put_cstr(&s, "[modify]");
1790 }
1791 if (put->flags & DPIF_FP_ZERO_STATS) {
1792 ds_put_cstr(&s, "[zero]");
1793 }
1794 log_flow_message(dpif, error, module, ds_cstr(&s),
1795 put->key, put->key_len, put->mask, put->mask_len,
1796 put->ufid, put->stats, put->actions,
1797 put->actions_len);
1798 ds_destroy(&s);
1799 }
1800 }
1801
1802 void
1803 log_flow_del_message(const struct dpif *dpif,
1804 const struct vlog_module *module,
1805 const struct dpif_flow_del *del,
1806 int error)
1807 {
1808 if (should_log_flow_message(module, error)) {
1809 log_flow_message(dpif, error, module, "flow_del",
1810 del->key, del->key_len,
1811 NULL, 0, del->ufid, !error ? del->stats : NULL,
1812 NULL, 0);
1813 }
1814 }
1815
1816 /* Logs that 'execute' was executed on 'dpif' and completed with errno 'error'
1817 * (0 for success). 'subexecute' should be true if the execution is a result
1818 * of breaking down a larger execution that needed help, false otherwise.
1819 *
1820 *
1821 * XXX In theory, the log message could be deceptive because this function is
1822 * called after the dpif_provider's '->execute' function, which is allowed to
1823 * modify execute->packet and execute->md. In practice, though:
1824 *
1825 * - dpif-netlink doesn't modify execute->packet or execute->md.
1826 *
1827 * - dpif-netdev does modify them but it is less likely to have problems
1828 * because it is built into ovs-vswitchd and cannot have version skew,
1829 * etc.
1830 *
1831 * It would still be better to avoid the potential problem. I don't know of a
1832 * good way to do that, though, that isn't expensive. */
1833 void
1834 log_execute_message(const struct dpif *dpif,
1835 const struct vlog_module *module,
1836 const struct dpif_execute *execute,
1837 bool subexecute, int error)
1838 {
1839 if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))
1840 && !execute->probe) {
1841 struct ds ds = DS_EMPTY_INITIALIZER;
1842 char *packet;
1843 uint64_t stub[1024 / 8];
1844 struct ofpbuf md = OFPBUF_STUB_INITIALIZER(stub);
1845
1846 packet = ofp_packet_to_string(dp_packet_data(execute->packet),
1847 dp_packet_size(execute->packet),
1848 execute->packet->packet_type);
1849 odp_key_from_dp_packet(&md, execute->packet);
1850 ds_put_format(&ds, "%s: %sexecute ",
1851 dpif_name(dpif),
1852 (subexecute ? "sub-"
1853 : dpif_execute_needs_help(execute) ? "super-"
1854 : ""));
1855 format_odp_actions(&ds, execute->actions, execute->actions_len, NULL);
1856 if (error) {
1857 ds_put_format(&ds, " failed (%s)", ovs_strerror(error));
1858 }
1859 ds_put_format(&ds, " on packet %s", packet);
1860 ds_put_format(&ds, " with metadata ");
1861 odp_flow_format(md.data, md.size, NULL, 0, NULL, &ds, true);
1862 ds_put_format(&ds, " mtu %d", execute->mtu);
1863 vlog(module, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
1864 ds_destroy(&ds);
1865 free(packet);
1866 ofpbuf_uninit(&md);
1867 }
1868 }
1869
1870 void
1871 log_flow_get_message(const struct dpif *dpif,
1872 const struct vlog_module *module,
1873 const struct dpif_flow_get *get,
1874 int error)
1875 {
1876 if (should_log_flow_message(module, error)) {
1877 log_flow_message(dpif, error, module, "flow_get",
1878 get->key, get->key_len,
1879 get->flow->mask, get->flow->mask_len,
1880 get->ufid, &get->flow->stats,
1881 get->flow->actions, get->flow->actions_len);
1882 }
1883 }
1884
1885 bool
1886 dpif_supports_tnl_push_pop(const struct dpif *dpif)
1887 {
1888 return dpif_is_netdev(dpif);
1889 }
1890
1891 /* Meters */
1892 void
1893 dpif_meter_get_features(const struct dpif *dpif,
1894 struct ofputil_meter_features *features)
1895 {
1896 memset(features, 0, sizeof *features);
1897 if (dpif->dpif_class->meter_get_features) {
1898 dpif->dpif_class->meter_get_features(dpif, features);
1899 }
1900 }
1901
1902 /* Adds or modifies the meter in 'dpif' with the given 'meter_id' and
1903 * the configuration in 'config'.
1904 *
1905 * The meter id specified through 'config->meter_id' is ignored. */
1906 int
1907 dpif_meter_set(struct dpif *dpif, ofproto_meter_id meter_id,
1908 struct ofputil_meter_config *config)
1909 {
1910 COVERAGE_INC(dpif_meter_set);
1911
1912 if (!(config->flags & (OFPMF13_KBPS | OFPMF13_PKTPS))) {
1913 return EBADF; /* Rate unit type not set. */
1914 }
1915
1916 if ((config->flags & OFPMF13_KBPS) && (config->flags & OFPMF13_PKTPS)) {
1917 return EBADF; /* Both rate units may not be set. */
1918 }
1919
1920 if (config->n_bands == 0) {
1921 return EINVAL;
1922 }
1923
1924 for (size_t i = 0; i < config->n_bands; i++) {
1925 if (config->bands[i].rate == 0) {
1926 return EDOM; /* Rate must be non-zero */
1927 }
1928 }
1929
1930 int error = dpif->dpif_class->meter_set(dpif, meter_id, config);
1931 if (!error) {
1932 VLOG_DBG_RL(&dpmsg_rl, "%s: DPIF meter %"PRIu32" set",
1933 dpif_name(dpif), meter_id.uint32);
1934 } else {
1935 VLOG_WARN_RL(&error_rl, "%s: failed to set DPIF meter %"PRIu32": %s",
1936 dpif_name(dpif), meter_id.uint32, ovs_strerror(error));
1937 }
1938 return error;
1939 }
1940
1941 int
1942 dpif_meter_get(const struct dpif *dpif, ofproto_meter_id meter_id,
1943 struct ofputil_meter_stats *stats, uint16_t n_bands)
1944 {
1945 int error;
1946
1947 COVERAGE_INC(dpif_meter_get);
1948
1949 error = dpif->dpif_class->meter_get(dpif, meter_id, stats, n_bands);
1950 if (!error) {
1951 VLOG_DBG_RL(&dpmsg_rl, "%s: DPIF meter %"PRIu32" get stats",
1952 dpif_name(dpif), meter_id.uint32);
1953 } else {
1954 VLOG_WARN_RL(&error_rl,
1955 "%s: failed to get DPIF meter %"PRIu32" stats: %s",
1956 dpif_name(dpif), meter_id.uint32, ovs_strerror(error));
1957 stats->packet_in_count = ~0;
1958 stats->byte_in_count = ~0;
1959 stats->n_bands = 0;
1960 }
1961 return error;
1962 }
1963
1964 int
1965 dpif_meter_del(struct dpif *dpif, ofproto_meter_id meter_id,
1966 struct ofputil_meter_stats *stats, uint16_t n_bands)
1967 {
1968 int error;
1969
1970 COVERAGE_INC(dpif_meter_del);
1971
1972 error = dpif->dpif_class->meter_del(dpif, meter_id, stats, n_bands);
1973 if (!error) {
1974 VLOG_DBG_RL(&dpmsg_rl, "%s: DPIF meter %"PRIu32" deleted",
1975 dpif_name(dpif), meter_id.uint32);
1976 } else {
1977 VLOG_WARN_RL(&error_rl,
1978 "%s: failed to delete DPIF meter %"PRIu32": %s",
1979 dpif_name(dpif), meter_id.uint32, ovs_strerror(error));
1980 if (stats) {
1981 stats->packet_in_count = ~0;
1982 stats->byte_in_count = ~0;
1983 stats->n_bands = 0;
1984 }
1985 }
1986 return error;
1987 }