]> git.proxmox.com Git - ovs.git/blob - ofproto/ofproto-dpif.c
ofproto: Set to revalidate when a new version is available.
[ovs.git] / ofproto / ofproto-dpif.c
1 /*
2 * Copyright (c) 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 <errno.h>
19
20 #include "bfd.h"
21 #include "bond.h"
22 #include "bundle.h"
23 #include "byte-order.h"
24 #include "connectivity.h"
25 #include "connmgr.h"
26 #include "coverage.h"
27 #include "cfm.h"
28 #include "dpif.h"
29 #include "fail-open.h"
30 #include "guarded-list.h"
31 #include "hmapx.h"
32 #include "lacp.h"
33 #include "learn.h"
34 #include "mac-learning.h"
35 #include "mcast-snooping.h"
36 #include "multipath.h"
37 #include "netdev-vport.h"
38 #include "netdev.h"
39 #include "netlink.h"
40 #include "nx-match.h"
41 #include "odp-util.h"
42 #include "odp-execute.h"
43 #include "ofproto/ofproto-dpif.h"
44 #include "ofproto/ofproto-provider.h"
45 #include "ofproto-dpif-ipfix.h"
46 #include "ofproto-dpif-mirror.h"
47 #include "ofproto-dpif-monitor.h"
48 #include "ofproto-dpif-rid.h"
49 #include "ofproto-dpif-sflow.h"
50 #include "ofproto-dpif-upcall.h"
51 #include "ofproto-dpif-xlate.h"
52 #include "openvswitch/ofp-actions.h"
53 #include "openvswitch/dynamic-string.h"
54 #include "openvswitch/meta-flow.h"
55 #include "openvswitch/ofp-parse.h"
56 #include "openvswitch/ofp-print.h"
57 #include "openvswitch/ofp-util.h"
58 #include "openvswitch/ofpbuf.h"
59 #include "openvswitch/vlog.h"
60 #include "ovs-lldp.h"
61 #include "ovs-rcu.h"
62 #include "ovs-router.h"
63 #include "poll-loop.h"
64 #include "seq.h"
65 #include "simap.h"
66 #include "smap.h"
67 #include "timer.h"
68 #include "tunnel.h"
69 #include "unaligned.h"
70 #include "unixctl.h"
71 #include "vlan-bitmap.h"
72
73 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
74
75 COVERAGE_DEFINE(ofproto_dpif_expired);
76 COVERAGE_DEFINE(packet_in_overflow);
77
78 struct flow_miss;
79
80 struct rule_dpif {
81 struct rule up;
82
83 /* These statistics:
84 *
85 * - Do include packets and bytes from datapath flows which have not
86 * recently been processed by a revalidator. */
87 struct ovs_mutex stats_mutex;
88 struct dpif_flow_stats stats OVS_GUARDED;
89
90 /* In non-NULL, will point to a new rule (for which a reference is held) to
91 * which all the stats updates should be forwarded. This exists only
92 * transitionally when flows are replaced.
93 *
94 * Protected by stats_mutex. If both 'rule->stats_mutex' and
95 * 'rule->new_rule->stats_mutex' must be held together, acquire them in that
96 * order, */
97 struct rule_dpif *new_rule OVS_GUARDED;
98
99 /* If non-zero then the recirculation id that has
100 * been allocated for use with this rule.
101 * The recirculation id and associated internal flow should
102 * be freed when the rule is freed */
103 uint32_t recirc_id;
104 };
105
106 /* RULE_CAST() depends on this. */
107 BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
108
109 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
110 long long int *used);
111 static struct rule_dpif *rule_dpif_cast(const struct rule *);
112 static void rule_expire(struct rule_dpif *, long long now);
113
114 struct group_dpif {
115 struct ofgroup up;
116
117 /* These statistics:
118 *
119 * - Do include packets and bytes from datapath flows which have not
120 * recently been processed by a revalidator. */
121 struct ovs_mutex stats_mutex;
122 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
123 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
124 };
125
126 struct ofbundle {
127 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
128 struct ofproto_dpif *ofproto; /* Owning ofproto. */
129 void *aux; /* Key supplied by ofproto's client. */
130 char *name; /* Identifier for log messages. */
131
132 /* Configuration. */
133 struct ovs_list ports; /* Contains "struct ofport"s. */
134 enum port_vlan_mode vlan_mode; /* VLAN mode */
135 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
136 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
137 * NULL if all VLANs are trunked. */
138 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
139 struct bond *bond; /* Nonnull iff more than one port. */
140 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
141
142 /* Status. */
143 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
144 };
145
146 static void bundle_remove(struct ofport *);
147 static void bundle_update(struct ofbundle *);
148 static void bundle_destroy(struct ofbundle *);
149 static void bundle_del_port(struct ofport_dpif *);
150 static void bundle_run(struct ofbundle *);
151 static void bundle_wait(struct ofbundle *);
152 static void bundle_flush_macs(struct ofbundle *, bool);
153 static void bundle_move(struct ofbundle *, struct ofbundle *);
154
155 static void stp_run(struct ofproto_dpif *ofproto);
156 static void stp_wait(struct ofproto_dpif *ofproto);
157 static int set_stp_port(struct ofport *,
158 const struct ofproto_port_stp_settings *);
159
160 static void rstp_run(struct ofproto_dpif *ofproto);
161 static void set_rstp_port(struct ofport *,
162 const struct ofproto_port_rstp_settings *);
163
164 struct ofport_dpif {
165 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
166 struct ofport up;
167
168 odp_port_t odp_port;
169 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
170 struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
171 struct cfm *cfm; /* Connectivity Fault Management, if any. */
172 struct bfd *bfd; /* BFD, if any. */
173 struct lldp *lldp; /* lldp, if any. */
174 bool may_enable; /* May be enabled in bonds. */
175 bool is_tunnel; /* This port is a tunnel. */
176 bool is_layer3; /* This is a layer 3 port. */
177 long long int carrier_seq; /* Carrier status changes. */
178 struct ofport_dpif *peer; /* Peer if patch port. */
179
180 /* Spanning tree. */
181 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
182 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
183 long long int stp_state_entered;
184
185 /* Rapid Spanning Tree. */
186 struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
187 enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
188
189 /* Queue to DSCP mapping. */
190 struct ofproto_port_queue *qdscp;
191 size_t n_qdscp;
192 };
193
194 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
195 ofp_port_t);
196
197 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
198 odp_port_t);
199
200 static struct ofport_dpif *
201 ofport_dpif_cast(const struct ofport *ofport)
202 {
203 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
204 }
205
206 static void port_run(struct ofport_dpif *);
207 static int set_bfd(struct ofport *, const struct smap *);
208 static int set_cfm(struct ofport *, const struct cfm_settings *);
209 static int set_lldp(struct ofport *ofport_, const struct smap *cfg);
210 static void ofport_update_peer(struct ofport_dpif *);
211
212 /* Reasons that we might need to revalidate every datapath flow, and
213 * corresponding coverage counters.
214 *
215 * A value of 0 means that there is no need to revalidate.
216 *
217 * It would be nice to have some cleaner way to integrate with coverage
218 * counters, but with only a few reasons I guess this is good enough for
219 * now. */
220 enum revalidate_reason {
221 REV_RECONFIGURE = 1, /* Switch configuration changed. */
222 REV_STP, /* Spanning tree protocol port status change. */
223 REV_RSTP, /* RSTP port status change. */
224 REV_BOND, /* Bonding changed. */
225 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
226 REV_FLOW_TABLE, /* Flow table changed. */
227 REV_MAC_LEARNING, /* Mac learning changed. */
228 REV_MCAST_SNOOPING, /* Multicast snooping changed. */
229 };
230 COVERAGE_DEFINE(rev_reconfigure);
231 COVERAGE_DEFINE(rev_stp);
232 COVERAGE_DEFINE(rev_rstp);
233 COVERAGE_DEFINE(rev_bond);
234 COVERAGE_DEFINE(rev_port_toggled);
235 COVERAGE_DEFINE(rev_flow_table);
236 COVERAGE_DEFINE(rev_mac_learning);
237 COVERAGE_DEFINE(rev_mcast_snooping);
238
239 /* All datapaths of a given type share a single dpif backer instance. */
240 struct dpif_backer {
241 char *type;
242 int refcount;
243 struct dpif *dpif;
244 struct udpif *udpif;
245
246 struct ovs_rwlock odp_to_ofport_lock;
247 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
248
249 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
250
251 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
252
253 bool recv_set_enable; /* Enables or disables receiving packets. */
254
255 /* Version string of the datapath stored in OVSDB. */
256 char *dp_version_string;
257
258 /* Datapath feature support. */
259 struct dpif_backer_support support;
260 struct atomic_count tnl_count;
261 };
262
263 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
264 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
265
266 struct ofproto_dpif {
267 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
268 struct ofproto up;
269 struct dpif_backer *backer;
270
271 /* Unique identifier for this instantiation of this bridge in this running
272 * process. */
273 struct uuid uuid;
274
275 ATOMIC(cls_version_t) tables_version; /* For classifier lookups. */
276
277 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
278
279 /* Special OpenFlow rules. */
280 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
281 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
282 struct rule_dpif *drop_frags_rule; /* Used in OFPUTIL_FRAG_DROP mode. */
283
284 /* Bridging. */
285 struct netflow *netflow;
286 struct dpif_sflow *sflow;
287 struct dpif_ipfix *ipfix;
288 struct hmap bundles; /* Contains "struct ofbundle"s. */
289 struct mac_learning *ml;
290 struct mcast_snooping *ms;
291 bool has_bonded_bundles;
292 bool lacp_enabled;
293 struct mbridge *mbridge;
294
295 struct ovs_mutex stats_mutex;
296 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
297 * consumed in userspace. */
298
299 /* Spanning tree. */
300 struct stp *stp;
301 long long int stp_last_tick;
302
303 /* Rapid Spanning Tree. */
304 struct rstp *rstp;
305 long long int rstp_last_tick;
306
307 /* Ports. */
308 struct sset ports; /* Set of standard port names. */
309 struct sset ghost_ports; /* Ports with no datapath port. */
310 struct sset port_poll_set; /* Queued names for port_poll() reply. */
311 int port_poll_errno; /* Last errno for port_poll() reply. */
312 uint64_t change_seq; /* Connectivity status changes. */
313
314 /* Work queues. */
315 struct guarded_list ams; /* Contains "struct ofproto_async_msgs"s. */
316 struct seq *ams_seq; /* For notifying 'ams' reception. */
317 uint64_t ams_seqno;
318 };
319
320 /* All existing ofproto_dpif instances, indexed by ->up.name. */
321 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
322
323 static bool ofproto_use_tnl_push_pop = true;
324 static void ofproto_unixctl_init(void);
325
326 static inline struct ofproto_dpif *
327 ofproto_dpif_cast(const struct ofproto *ofproto)
328 {
329 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
330 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
331 }
332
333 bool
334 ofproto_dpif_get_enable_ufid(const struct dpif_backer *backer)
335 {
336 return backer->support.ufid;
337 }
338
339 struct dpif_backer_support *
340 ofproto_dpif_get_support(const struct ofproto_dpif *ofproto)
341 {
342 return &ofproto->backer->support;
343 }
344
345 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
346 const struct dp_packet *packet,
347 const struct ofpact[], size_t ofpacts_len,
348 struct ds *);
349
350 /* Global variables. */
351 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
352
353 /* Initial mappings of port to bridge mappings. */
354 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
355
356 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
357 * it. */
358 void
359 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
360 const struct ofputil_flow_mod *fm)
361 {
362 struct ofproto_flow_mod ofm;
363
364 /* Multiple threads may do this for the same 'fm' at the same time.
365 * Allocate ofproto_flow_mod with execution context from stack.
366 *
367 * Note: This copy could be avoided by making ofproto_flow_mod more
368 * complex, but that may not be desireable, and a learn action is not that
369 * fast to begin with. */
370 ofm.fm = *fm;
371 ofproto_flow_mod(&ofproto->up, &ofm);
372 }
373
374 /* Appends 'am' to the queue of asynchronous messages to be sent to the
375 * controller. Takes ownership of 'am' and any data it points to. */
376 void
377 ofproto_dpif_send_async_msg(struct ofproto_dpif *ofproto,
378 struct ofproto_async_msg *am)
379 {
380 if (!guarded_list_push_back(&ofproto->ams, &am->list_node, 1024)) {
381 COVERAGE_INC(packet_in_overflow);
382 ofproto_async_msg_free(am);
383 }
384
385 /* Wakes up main thread for packet-in I/O. */
386 seq_change(ofproto->ams_seq);
387 }
388
389 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
390 * packet rather than to send the packet to the controller.
391 *
392 * This function returns false to indicate that a packet_in message
393 * for a "table-miss" should be sent to at least one controller.
394 * False otherwise. */
395 bool
396 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
397 {
398 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
399 }
400 \f
401 /* Factory functions. */
402
403 static void
404 init(const struct shash *iface_hints)
405 {
406 struct shash_node *node;
407
408 /* Make a local copy, since we don't own 'iface_hints' elements. */
409 SHASH_FOR_EACH(node, iface_hints) {
410 const struct iface_hint *orig_hint = node->data;
411 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
412
413 new_hint->br_name = xstrdup(orig_hint->br_name);
414 new_hint->br_type = xstrdup(orig_hint->br_type);
415 new_hint->ofp_port = orig_hint->ofp_port;
416
417 shash_add(&init_ofp_ports, node->name, new_hint);
418 }
419
420 ofproto_unixctl_init();
421 udpif_init();
422 }
423
424 static void
425 enumerate_types(struct sset *types)
426 {
427 dp_enumerate_types(types);
428 }
429
430 static int
431 enumerate_names(const char *type, struct sset *names)
432 {
433 struct ofproto_dpif *ofproto;
434
435 sset_clear(names);
436 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
437 if (strcmp(type, ofproto->up.type)) {
438 continue;
439 }
440 sset_add(names, ofproto->up.name);
441 }
442
443 return 0;
444 }
445
446 static int
447 del(const char *type, const char *name)
448 {
449 struct dpif *dpif;
450 int error;
451
452 error = dpif_open(name, type, &dpif);
453 if (!error) {
454 error = dpif_delete(dpif);
455 dpif_close(dpif);
456 }
457 return error;
458 }
459 \f
460 static const char *
461 port_open_type(const char *datapath_type, const char *port_type)
462 {
463 return dpif_port_open_type(datapath_type, port_type);
464 }
465
466 /* Type functions. */
467
468 static void process_dpif_port_changes(struct dpif_backer *);
469 static void process_dpif_all_ports_changed(struct dpif_backer *);
470 static void process_dpif_port_change(struct dpif_backer *,
471 const char *devname);
472 static void process_dpif_port_error(struct dpif_backer *, int error);
473
474 static struct ofproto_dpif *
475 lookup_ofproto_dpif_by_port_name(const char *name)
476 {
477 struct ofproto_dpif *ofproto;
478
479 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
480 if (sset_contains(&ofproto->ports, name)) {
481 return ofproto;
482 }
483 }
484
485 return NULL;
486 }
487
488 bool
489 ofproto_dpif_backer_enabled(struct dpif_backer* backer)
490 {
491 return backer->recv_set_enable;
492 }
493
494 static int
495 type_run(const char *type)
496 {
497 struct dpif_backer *backer;
498
499 backer = shash_find_data(&all_dpif_backers, type);
500 if (!backer) {
501 /* This is not necessarily a problem, since backers are only
502 * created on demand. */
503 return 0;
504 }
505
506 /* This must be called before dpif_run() */
507 dpif_poll_threads_set(backer->dpif, pmd_cpu_mask);
508
509 if (dpif_run(backer->dpif)) {
510 backer->need_revalidate = REV_RECONFIGURE;
511 }
512
513 udpif_run(backer->udpif);
514
515 /* If vswitchd started with other_config:flow_restore_wait set as "true",
516 * and the configuration has now changed to "false", enable receiving
517 * packets from the datapath. */
518 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
519 int error;
520
521 backer->recv_set_enable = true;
522
523 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
524 if (error) {
525 VLOG_ERR("Failed to enable receiving packets in dpif.");
526 return error;
527 }
528 dpif_flow_flush(backer->dpif);
529 backer->need_revalidate = REV_RECONFIGURE;
530 }
531
532 if (backer->recv_set_enable) {
533 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
534 }
535
536 if (backer->need_revalidate) {
537 struct ofproto_dpif *ofproto;
538 struct simap_node *node;
539 struct simap tmp_backers;
540
541 /* Handle tunnel garbage collection. */
542 simap_init(&tmp_backers);
543 simap_swap(&backer->tnl_backers, &tmp_backers);
544
545 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
546 struct ofport_dpif *iter;
547
548 if (backer != ofproto->backer) {
549 continue;
550 }
551
552 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
553 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
554 const char *dp_port;
555
556 if (!iter->is_tunnel) {
557 continue;
558 }
559
560 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
561 namebuf, sizeof namebuf);
562 node = simap_find(&tmp_backers, dp_port);
563 if (node) {
564 simap_put(&backer->tnl_backers, dp_port, node->data);
565 simap_delete(&tmp_backers, node);
566 node = simap_find(&backer->tnl_backers, dp_port);
567 } else {
568 node = simap_find(&backer->tnl_backers, dp_port);
569 if (!node) {
570 odp_port_t odp_port = ODPP_NONE;
571
572 if (!dpif_port_add(backer->dpif, iter->up.netdev,
573 &odp_port)) {
574 simap_put(&backer->tnl_backers, dp_port,
575 odp_to_u32(odp_port));
576 node = simap_find(&backer->tnl_backers, dp_port);
577 }
578 }
579 }
580
581 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
582 if (tnl_port_reconfigure(iter, iter->up.netdev,
583 iter->odp_port,
584 ovs_native_tunneling_is_on(ofproto), dp_port)) {
585 backer->need_revalidate = REV_RECONFIGURE;
586 }
587 }
588 }
589
590 SIMAP_FOR_EACH (node, &tmp_backers) {
591 dpif_port_del(backer->dpif, u32_to_odp(node->data));
592 }
593 simap_destroy(&tmp_backers);
594
595 switch (backer->need_revalidate) {
596 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
597 case REV_STP: COVERAGE_INC(rev_stp); break;
598 case REV_RSTP: COVERAGE_INC(rev_rstp); break;
599 case REV_BOND: COVERAGE_INC(rev_bond); break;
600 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
601 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
602 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
603 case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
604 }
605 backer->need_revalidate = 0;
606
607 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
608 struct ofport_dpif *ofport;
609 struct ofbundle *bundle;
610
611 if (ofproto->backer != backer) {
612 continue;
613 }
614
615 xlate_txn_start();
616 xlate_ofproto_set(ofproto, ofproto->up.name,
617 ofproto->backer->dpif, ofproto->ml,
618 ofproto->stp, ofproto->rstp, ofproto->ms,
619 ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
620 ofproto->netflow,
621 ofproto->up.forward_bpdu,
622 connmgr_has_in_band(ofproto->up.connmgr),
623 &ofproto->backer->support);
624
625 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
626 xlate_bundle_set(ofproto, bundle, bundle->name,
627 bundle->vlan_mode, bundle->vlan,
628 bundle->trunks, bundle->use_priority_tags,
629 bundle->bond, bundle->lacp,
630 bundle->floodable);
631 }
632
633 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
634 int stp_port = ofport->stp_port
635 ? stp_port_no(ofport->stp_port)
636 : -1;
637 xlate_ofport_set(ofproto, ofport->bundle, ofport,
638 ofport->up.ofp_port, ofport->odp_port,
639 ofport->up.netdev, ofport->cfm, ofport->bfd,
640 ofport->lldp, ofport->peer, stp_port,
641 ofport->rstp_port, ofport->qdscp,
642 ofport->n_qdscp, ofport->up.pp.config,
643 ofport->up.pp.state, ofport->is_tunnel,
644 ofport->may_enable);
645 }
646 xlate_txn_commit();
647 }
648
649 udpif_revalidate(backer->udpif);
650 }
651
652 process_dpif_port_changes(backer);
653
654 return 0;
655 }
656
657 /* Check for and handle port changes in 'backer''s dpif. */
658 static void
659 process_dpif_port_changes(struct dpif_backer *backer)
660 {
661 for (;;) {
662 char *devname;
663 int error;
664
665 error = dpif_port_poll(backer->dpif, &devname);
666 switch (error) {
667 case EAGAIN:
668 return;
669
670 case ENOBUFS:
671 process_dpif_all_ports_changed(backer);
672 break;
673
674 case 0:
675 process_dpif_port_change(backer, devname);
676 free(devname);
677 break;
678
679 default:
680 process_dpif_port_error(backer, error);
681 break;
682 }
683 }
684 }
685
686 static void
687 process_dpif_all_ports_changed(struct dpif_backer *backer)
688 {
689 struct ofproto_dpif *ofproto;
690 struct dpif_port dpif_port;
691 struct dpif_port_dump dump;
692 struct sset devnames;
693 const char *devname;
694
695 sset_init(&devnames);
696 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
697 if (ofproto->backer == backer) {
698 struct ofport *ofport;
699
700 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
701 sset_add(&devnames, netdev_get_name(ofport->netdev));
702 }
703 }
704 }
705 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
706 sset_add(&devnames, dpif_port.name);
707 }
708
709 SSET_FOR_EACH (devname, &devnames) {
710 process_dpif_port_change(backer, devname);
711 }
712 sset_destroy(&devnames);
713 }
714
715 static void
716 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
717 {
718 struct ofproto_dpif *ofproto;
719 struct dpif_port port;
720
721 /* Don't report on the datapath's device. */
722 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
723 return;
724 }
725
726 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
727 &all_ofproto_dpifs) {
728 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
729 return;
730 }
731 }
732
733 ofproto = lookup_ofproto_dpif_by_port_name(devname);
734 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
735 /* The port was removed. If we know the datapath,
736 * report it through poll_set(). If we don't, it may be
737 * notifying us of a removal we initiated, so ignore it.
738 * If there's a pending ENOBUFS, let it stand, since
739 * everything will be reevaluated. */
740 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
741 sset_add(&ofproto->port_poll_set, devname);
742 ofproto->port_poll_errno = 0;
743 }
744 } else if (!ofproto) {
745 /* The port was added, but we don't know with which
746 * ofproto we should associate it. Delete it. */
747 dpif_port_del(backer->dpif, port.port_no);
748 } else {
749 struct ofport_dpif *ofport;
750
751 ofport = ofport_dpif_cast(shash_find_data(
752 &ofproto->up.port_by_name, devname));
753 if (ofport
754 && ofport->odp_port != port.port_no
755 && !odp_port_to_ofport(backer, port.port_no))
756 {
757 /* 'ofport''s datapath port number has changed from
758 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
759 * structures to match. */
760 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
761 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
762 ofport->odp_port = port.port_no;
763 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
764 hash_odp_port(port.port_no));
765 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
766 backer->need_revalidate = REV_RECONFIGURE;
767 }
768 }
769 dpif_port_destroy(&port);
770 }
771
772 /* Propagate 'error' to all ofprotos based on 'backer'. */
773 static void
774 process_dpif_port_error(struct dpif_backer *backer, int error)
775 {
776 struct ofproto_dpif *ofproto;
777
778 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
779 if (ofproto->backer == backer) {
780 sset_clear(&ofproto->port_poll_set);
781 ofproto->port_poll_errno = error;
782 }
783 }
784 }
785
786 static void
787 type_wait(const char *type)
788 {
789 struct dpif_backer *backer;
790
791 backer = shash_find_data(&all_dpif_backers, type);
792 if (!backer) {
793 /* This is not necessarily a problem, since backers are only
794 * created on demand. */
795 return;
796 }
797
798 dpif_wait(backer->dpif);
799 }
800 \f
801 /* Basic life-cycle. */
802
803 static int add_internal_flows(struct ofproto_dpif *);
804
805 static struct ofproto *
806 alloc(void)
807 {
808 struct ofproto_dpif *ofproto = xzalloc(sizeof *ofproto);
809 return &ofproto->up;
810 }
811
812 static void
813 dealloc(struct ofproto *ofproto_)
814 {
815 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
816 free(ofproto);
817 }
818
819 static void
820 close_dpif_backer(struct dpif_backer *backer)
821 {
822 ovs_assert(backer->refcount > 0);
823
824 if (--backer->refcount) {
825 return;
826 }
827
828 udpif_destroy(backer->udpif);
829
830 simap_destroy(&backer->tnl_backers);
831 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
832 hmap_destroy(&backer->odp_to_ofport_map);
833 shash_find_and_delete(&all_dpif_backers, backer->type);
834 free(backer->type);
835 free(backer->dp_version_string);
836 dpif_close(backer->dpif);
837 free(backer);
838 }
839
840 /* Datapath port slated for removal from datapath. */
841 struct odp_garbage {
842 struct ovs_list list_node;
843 odp_port_t odp_port;
844 };
845
846 static bool check_variable_length_userdata(struct dpif_backer *backer);
847 static void check_support(struct dpif_backer *backer);
848
849 static int
850 open_dpif_backer(const char *type, struct dpif_backer **backerp)
851 {
852 struct dpif_backer *backer;
853 struct dpif_port_dump port_dump;
854 struct dpif_port port;
855 struct shash_node *node;
856 struct ovs_list garbage_list;
857 struct odp_garbage *garbage;
858
859 struct sset names;
860 char *backer_name;
861 const char *name;
862 int error;
863
864 backer = shash_find_data(&all_dpif_backers, type);
865 if (backer) {
866 backer->refcount++;
867 *backerp = backer;
868 return 0;
869 }
870
871 backer_name = xasprintf("ovs-%s", type);
872
873 /* Remove any existing datapaths, since we assume we're the only
874 * userspace controlling the datapath. */
875 sset_init(&names);
876 dp_enumerate_names(type, &names);
877 SSET_FOR_EACH(name, &names) {
878 struct dpif *old_dpif;
879
880 /* Don't remove our backer if it exists. */
881 if (!strcmp(name, backer_name)) {
882 continue;
883 }
884
885 if (dpif_open(name, type, &old_dpif)) {
886 VLOG_WARN("couldn't open old datapath %s to remove it", name);
887 } else {
888 dpif_delete(old_dpif);
889 dpif_close(old_dpif);
890 }
891 }
892 sset_destroy(&names);
893
894 backer = xmalloc(sizeof *backer);
895
896 error = dpif_create_and_open(backer_name, type, &backer->dpif);
897 free(backer_name);
898 if (error) {
899 VLOG_ERR("failed to open datapath of type %s: %s", type,
900 ovs_strerror(error));
901 free(backer);
902 return error;
903 }
904 backer->udpif = udpif_create(backer, backer->dpif);
905
906 backer->type = xstrdup(type);
907 backer->refcount = 1;
908 hmap_init(&backer->odp_to_ofport_map);
909 ovs_rwlock_init(&backer->odp_to_ofport_lock);
910 backer->need_revalidate = 0;
911 simap_init(&backer->tnl_backers);
912 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
913 *backerp = backer;
914
915 if (backer->recv_set_enable) {
916 dpif_flow_flush(backer->dpif);
917 }
918
919 /* Loop through the ports already on the datapath and remove any
920 * that we don't need anymore. */
921 ovs_list_init(&garbage_list);
922 dpif_port_dump_start(&port_dump, backer->dpif);
923 while (dpif_port_dump_next(&port_dump, &port)) {
924 node = shash_find(&init_ofp_ports, port.name);
925 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
926 garbage = xmalloc(sizeof *garbage);
927 garbage->odp_port = port.port_no;
928 ovs_list_push_front(&garbage_list, &garbage->list_node);
929 }
930 }
931 dpif_port_dump_done(&port_dump);
932
933 LIST_FOR_EACH_POP (garbage, list_node, &garbage_list) {
934 dpif_port_del(backer->dpif, garbage->odp_port);
935 free(garbage);
936 }
937
938 shash_add(&all_dpif_backers, type, backer);
939
940 check_support(backer);
941 atomic_count_init(&backer->tnl_count, 0);
942
943 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
944 if (error) {
945 VLOG_ERR("failed to listen on datapath of type %s: %s",
946 type, ovs_strerror(error));
947 close_dpif_backer(backer);
948 return error;
949 }
950
951 if (backer->recv_set_enable) {
952 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
953 }
954
955 /* This check fails if performed before udpif threads have been set,
956 * as the kernel module checks that the 'pid' in userspace action
957 * is non-zero. */
958 backer->support.variable_length_userdata
959 = check_variable_length_userdata(backer);
960 backer->dp_version_string = dpif_get_dp_version(backer->dpif);
961
962 return error;
963 }
964
965 bool
966 ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
967 {
968 return ofproto_use_tnl_push_pop && ofproto->backer->support.tnl_push_pop &&
969 atomic_count_get(&ofproto->backer->tnl_count);
970 }
971
972 /* Tests whether 'backer''s datapath supports recirculation. Only newer
973 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
974 * features on older datapaths that don't support this feature.
975 *
976 * Returns false if 'backer' definitely does not support recirculation, true if
977 * it seems to support recirculation or if at least the error we get is
978 * ambiguous. */
979 static bool
980 check_recirc(struct dpif_backer *backer)
981 {
982 struct flow flow;
983 struct odputil_keybuf keybuf;
984 struct ofpbuf key;
985 bool enable_recirc;
986 struct odp_flow_key_parms odp_parms = {
987 .flow = &flow,
988 .support = {
989 .recirc = true,
990 },
991 };
992
993 memset(&flow, 0, sizeof flow);
994 flow.recirc_id = 1;
995 flow.dp_hash = 1;
996
997 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
998 odp_flow_key_from_flow(&odp_parms, &key);
999 enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
1000 NULL);
1001
1002 if (enable_recirc) {
1003 VLOG_INFO("%s: Datapath supports recirculation",
1004 dpif_name(backer->dpif));
1005 } else {
1006 VLOG_INFO("%s: Datapath does not support recirculation",
1007 dpif_name(backer->dpif));
1008 }
1009
1010 return enable_recirc;
1011 }
1012
1013 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1014 * some flow attributes for datapaths that support this feature.
1015 *
1016 * Returns true if 'dpif' supports UFID for flow operations.
1017 * Returns false if 'dpif' does not support UFID. */
1018 static bool
1019 check_ufid(struct dpif_backer *backer)
1020 {
1021 struct flow flow;
1022 struct odputil_keybuf keybuf;
1023 struct ofpbuf key;
1024 ovs_u128 ufid;
1025 bool enable_ufid;
1026 struct odp_flow_key_parms odp_parms = {
1027 .flow = &flow,
1028 };
1029
1030 memset(&flow, 0, sizeof flow);
1031 flow.dl_type = htons(0x1234);
1032
1033 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1034 odp_flow_key_from_flow(&odp_parms, &key);
1035 dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
1036
1037 enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, &ufid);
1038
1039 if (enable_ufid) {
1040 VLOG_INFO("%s: Datapath supports unique flow ids",
1041 dpif_name(backer->dpif));
1042 } else {
1043 VLOG_INFO("%s: Datapath does not support unique flow ids",
1044 dpif_name(backer->dpif));
1045 }
1046 return enable_ufid;
1047 }
1048
1049 /* Tests whether 'backer''s datapath supports variable-length
1050 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1051 * to disable some features on older datapaths that don't support this
1052 * feature.
1053 *
1054 * Returns false if 'backer' definitely does not support variable-length
1055 * userdata, true if it seems to support them or if at least the error we get
1056 * is ambiguous. */
1057 static bool
1058 check_variable_length_userdata(struct dpif_backer *backer)
1059 {
1060 struct eth_header *eth;
1061 struct ofpbuf actions;
1062 struct dpif_execute execute;
1063 struct dp_packet packet;
1064 struct flow flow;
1065 size_t start;
1066 int error;
1067
1068 /* Compose a userspace action that will cause an ERANGE error on older
1069 * datapaths that don't support variable-length userdata.
1070 *
1071 * We really test for using userdata longer than 8 bytes, but older
1072 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1073 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1074 * test for that instead as a proxy for longer userdata support. */
1075 ofpbuf_init(&actions, 64);
1076 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1077 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1078 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1079 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1080 nl_msg_end_nested(&actions, start);
1081
1082 /* Compose a dummy ethernet packet. */
1083 dp_packet_init(&packet, ETH_HEADER_LEN);
1084 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1085 eth->eth_type = htons(0x1234);
1086
1087 flow_extract(&packet, &flow);
1088
1089 /* Execute the actions. On older datapaths this fails with ERANGE, on
1090 * newer datapaths it succeeds. */
1091 execute.actions = actions.data;
1092 execute.actions_len = actions.size;
1093 execute.packet = &packet;
1094 execute.flow = &flow;
1095 execute.needs_help = false;
1096 execute.probe = true;
1097 execute.mtu = 0;
1098
1099 error = dpif_execute(backer->dpif, &execute);
1100
1101 dp_packet_uninit(&packet);
1102 ofpbuf_uninit(&actions);
1103
1104 switch (error) {
1105 case 0:
1106 return true;
1107
1108 case ERANGE:
1109 /* Variable-length userdata is not supported. */
1110 VLOG_WARN("%s: datapath does not support variable-length userdata "
1111 "feature (needs Linux 3.10+ or kernel module from OVS "
1112 "1..11+). The NXAST_SAMPLE action will be ignored.",
1113 dpif_name(backer->dpif));
1114 return false;
1115
1116 default:
1117 /* Something odd happened. We're not sure whether variable-length
1118 * userdata is supported. Default to "yes". */
1119 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1120 dpif_name(backer->dpif), ovs_strerror(error));
1121 return true;
1122 }
1123 }
1124
1125 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1126 *
1127 * Returns the number of elements in a struct flow's mpls_lse field
1128 * if the datapath supports at least that many entries in an
1129 * MPLS label stack.
1130 * Otherwise returns the number of MPLS push actions supported by
1131 * the datapath. */
1132 static size_t
1133 check_max_mpls_depth(struct dpif_backer *backer)
1134 {
1135 struct flow flow;
1136 int n;
1137
1138 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1139 struct odputil_keybuf keybuf;
1140 struct ofpbuf key;
1141 struct odp_flow_key_parms odp_parms = {
1142 .flow = &flow,
1143 };
1144
1145 memset(&flow, 0, sizeof flow);
1146 flow.dl_type = htons(ETH_TYPE_MPLS);
1147 flow_set_mpls_bos(&flow, n, 1);
1148
1149 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1150 odp_flow_key_from_flow(&odp_parms, &key);
1151 if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL)) {
1152 break;
1153 }
1154 }
1155
1156 VLOG_INFO("%s: MPLS label stack length probed as %d",
1157 dpif_name(backer->dpif), n);
1158 return n;
1159 }
1160
1161 /* Tests whether 'backer''s datapath supports masked data in
1162 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1163 * datapaths that don't support this feature. */
1164 static bool
1165 check_masked_set_action(struct dpif_backer *backer)
1166 {
1167 struct eth_header *eth;
1168 struct ofpbuf actions;
1169 struct dpif_execute execute;
1170 struct dp_packet packet;
1171 struct flow flow;
1172 int error;
1173 struct ovs_key_ethernet key, mask;
1174
1175 /* Compose a set action that will cause an EINVAL error on older
1176 * datapaths that don't support masked set actions.
1177 * Avoid using a full mask, as it could be translated to a non-masked
1178 * set action instead. */
1179 ofpbuf_init(&actions, 64);
1180 memset(&key, 0x53, sizeof key);
1181 memset(&mask, 0x7f, sizeof mask);
1182 commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
1183 sizeof key);
1184
1185 /* Compose a dummy ethernet packet. */
1186 dp_packet_init(&packet, ETH_HEADER_LEN);
1187 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1188 eth->eth_type = htons(0x1234);
1189
1190 flow_extract(&packet, &flow);
1191
1192 /* Execute the actions. On older datapaths this fails with EINVAL, on
1193 * newer datapaths it succeeds. */
1194 execute.actions = actions.data;
1195 execute.actions_len = actions.size;
1196 execute.packet = &packet;
1197 execute.flow = &flow;
1198 execute.needs_help = false;
1199 execute.probe = true;
1200 execute.mtu = 0;
1201
1202 error = dpif_execute(backer->dpif, &execute);
1203
1204 dp_packet_uninit(&packet);
1205 ofpbuf_uninit(&actions);
1206
1207 if (error) {
1208 /* Masked set action is not supported. */
1209 VLOG_INFO("%s: datapath does not support masked set action feature.",
1210 dpif_name(backer->dpif));
1211 }
1212 return !error;
1213 }
1214
1215 #define CHECK_FEATURE__(NAME, SUPPORT, FIELD, VALUE) \
1216 static bool \
1217 check_##NAME(struct dpif_backer *backer) \
1218 { \
1219 struct flow flow; \
1220 struct odputil_keybuf keybuf; \
1221 struct ofpbuf key; \
1222 bool enable; \
1223 struct odp_flow_key_parms odp_parms = { \
1224 .flow = &flow, \
1225 .support = { \
1226 .SUPPORT = true, \
1227 }, \
1228 }; \
1229 \
1230 memset(&flow, 0, sizeof flow); \
1231 flow.FIELD = VALUE; \
1232 \
1233 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); \
1234 odp_flow_key_from_flow(&odp_parms, &key); \
1235 enable = dpif_probe_feature(backer->dpif, #NAME, &key, NULL); \
1236 \
1237 if (enable) { \
1238 VLOG_INFO("%s: Datapath supports "#NAME, dpif_name(backer->dpif)); \
1239 } else { \
1240 VLOG_INFO("%s: Datapath does not support "#NAME, \
1241 dpif_name(backer->dpif)); \
1242 } \
1243 \
1244 return enable; \
1245 }
1246 #define CHECK_FEATURE(FIELD) CHECK_FEATURE__(FIELD, FIELD, FIELD, 1)
1247
1248 CHECK_FEATURE(ct_state)
1249 CHECK_FEATURE(ct_zone)
1250 CHECK_FEATURE(ct_mark)
1251 CHECK_FEATURE__(ct_label, ct_label, ct_label.u64.lo, 1)
1252 CHECK_FEATURE__(ct_state_nat, ct_state, ct_state, CS_TRACKED|CS_SRC_NAT)
1253
1254 #undef CHECK_FEATURE
1255 #undef CHECK_FEATURE__
1256
1257 static void
1258 check_support(struct dpif_backer *backer)
1259 {
1260 /* This feature needs to be tested after udpif threads are set. */
1261 backer->support.variable_length_userdata = false;
1262
1263 backer->support.odp.recirc = check_recirc(backer);
1264 backer->support.odp.max_mpls_depth = check_max_mpls_depth(backer);
1265 backer->support.masked_set_action = check_masked_set_action(backer);
1266 backer->support.ufid = check_ufid(backer);
1267 backer->support.tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1268
1269 backer->support.odp.ct_state = check_ct_state(backer);
1270 backer->support.odp.ct_zone = check_ct_zone(backer);
1271 backer->support.odp.ct_mark = check_ct_mark(backer);
1272 backer->support.odp.ct_label = check_ct_label(backer);
1273
1274 backer->support.odp.ct_state_nat = check_ct_state_nat(backer);
1275 }
1276
1277 static int
1278 construct(struct ofproto *ofproto_)
1279 {
1280 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1281 struct shash_node *node, *next;
1282 int error;
1283
1284 /* Tunnel module can get used right after the udpif threads are running. */
1285 ofproto_tunnel_init();
1286
1287 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1288 if (error) {
1289 return error;
1290 }
1291
1292 uuid_generate(&ofproto->uuid);
1293 atomic_init(&ofproto->tables_version, CLS_MIN_VERSION);
1294 ofproto->netflow = NULL;
1295 ofproto->sflow = NULL;
1296 ofproto->ipfix = NULL;
1297 ofproto->stp = NULL;
1298 ofproto->rstp = NULL;
1299 ofproto->dump_seq = 0;
1300 hmap_init(&ofproto->bundles);
1301 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1302 ofproto->ms = NULL;
1303 ofproto->mbridge = mbridge_create();
1304 ofproto->has_bonded_bundles = false;
1305 ofproto->lacp_enabled = false;
1306 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1307
1308 guarded_list_init(&ofproto->ams);
1309
1310 sset_init(&ofproto->ports);
1311 sset_init(&ofproto->ghost_ports);
1312 sset_init(&ofproto->port_poll_set);
1313 ofproto->port_poll_errno = 0;
1314 ofproto->change_seq = 0;
1315 ofproto->ams_seq = seq_create();
1316 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
1317
1318
1319 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1320 struct iface_hint *iface_hint = node->data;
1321
1322 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1323 /* Check if the datapath already has this port. */
1324 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1325 sset_add(&ofproto->ports, node->name);
1326 }
1327
1328 free(iface_hint->br_name);
1329 free(iface_hint->br_type);
1330 free(iface_hint);
1331 shash_delete(&init_ofp_ports, node);
1332 }
1333 }
1334
1335 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1336 hash_string(ofproto->up.name, 0));
1337 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1338
1339 ofproto_init_tables(ofproto_, N_TABLES);
1340 error = add_internal_flows(ofproto);
1341
1342 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1343
1344 return error;
1345 }
1346
1347 static int
1348 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1349 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1350 {
1351 struct match match;
1352 int error;
1353 struct rule *rule;
1354
1355 match_init_catchall(&match);
1356 match_set_reg(&match, 0, id);
1357
1358 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1359 &rule);
1360 *rulep = error ? NULL : rule_dpif_cast(rule);
1361
1362 return error;
1363 }
1364
1365 static int
1366 add_internal_flows(struct ofproto_dpif *ofproto)
1367 {
1368 struct ofpact_controller *controller;
1369 uint64_t ofpacts_stub[128 / 8];
1370 struct ofpbuf ofpacts;
1371 struct rule *unused_rulep OVS_UNUSED;
1372 struct match match;
1373 int error;
1374 int id;
1375
1376 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1377 id = 1;
1378
1379 controller = ofpact_put_CONTROLLER(&ofpacts);
1380 controller->max_len = UINT16_MAX;
1381 controller->controller_id = 0;
1382 controller->reason = OFPR_IMPLICIT_MISS;
1383 ofpact_finish_CONTROLLER(&ofpacts, &controller);
1384
1385 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1386 &ofproto->miss_rule);
1387 if (error) {
1388 return error;
1389 }
1390
1391 ofpbuf_clear(&ofpacts);
1392 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1393 &ofproto->no_packet_in_rule);
1394 if (error) {
1395 return error;
1396 }
1397
1398 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1399 &ofproto->drop_frags_rule);
1400 if (error) {
1401 return error;
1402 }
1403
1404 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1405 * zero when reaching this rule.
1406 *
1407 * (priority=2), recirc_id=0, actions=drop
1408 */
1409 ofpbuf_clear(&ofpacts);
1410 match_init_catchall(&match);
1411 match_set_recirc_id(&match, 0);
1412 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1413 &unused_rulep);
1414 return error;
1415 }
1416
1417 static void
1418 destruct(struct ofproto *ofproto_)
1419 {
1420 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1421 struct ofproto_async_msg *am;
1422 struct rule_dpif *rule;
1423 struct oftable *table;
1424 struct ovs_list ams;
1425
1426 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1427 xlate_txn_start();
1428 xlate_remove_ofproto(ofproto);
1429 xlate_txn_commit();
1430
1431 /* Ensure that the upcall processing threads have no remaining references
1432 * to the ofproto or anything in it. */
1433 udpif_synchronize(ofproto->backer->udpif);
1434
1435 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1436
1437 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1438 CLS_FOR_EACH (rule, up.cr, &table->cls) {
1439 ofproto_rule_delete(&ofproto->up, &rule->up);
1440 }
1441 }
1442 ofproto_group_delete_all(&ofproto->up);
1443
1444 guarded_list_pop_all(&ofproto->ams, &ams);
1445 LIST_FOR_EACH_POP (am, list_node, &ams) {
1446 ofproto_async_msg_free(am);
1447 }
1448 guarded_list_destroy(&ofproto->ams);
1449
1450 recirc_free_ofproto(ofproto, ofproto->up.name);
1451
1452 mbridge_unref(ofproto->mbridge);
1453
1454 netflow_unref(ofproto->netflow);
1455 dpif_sflow_unref(ofproto->sflow);
1456 dpif_ipfix_unref(ofproto->ipfix);
1457 hmap_destroy(&ofproto->bundles);
1458 mac_learning_unref(ofproto->ml);
1459 mcast_snooping_unref(ofproto->ms);
1460
1461 sset_destroy(&ofproto->ports);
1462 sset_destroy(&ofproto->ghost_ports);
1463 sset_destroy(&ofproto->port_poll_set);
1464
1465 ovs_mutex_destroy(&ofproto->stats_mutex);
1466
1467 seq_destroy(ofproto->ams_seq);
1468
1469 close_dpif_backer(ofproto->backer);
1470 }
1471
1472 static int
1473 run(struct ofproto *ofproto_)
1474 {
1475 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1476 uint64_t new_seq, new_dump_seq;
1477
1478 if (mbridge_need_revalidate(ofproto->mbridge)) {
1479 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1480 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1481 mac_learning_flush(ofproto->ml);
1482 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1483 mcast_snooping_mdb_flush(ofproto->ms);
1484 }
1485
1486 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
1487 * flow restore. Even though nothing is processed during flow restore,
1488 * all queued 'ams' will be handled immediately when flow restore
1489 * completes. */
1490 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
1491
1492 /* Do not perform any periodic activity required by 'ofproto' while
1493 * waiting for flow restore to complete. */
1494 if (!ofproto_get_flow_restore_wait()) {
1495 struct ofproto_async_msg *am;
1496 struct ovs_list ams;
1497
1498 guarded_list_pop_all(&ofproto->ams, &ams);
1499 LIST_FOR_EACH_POP (am, list_node, &ams) {
1500 connmgr_send_async_msg(ofproto->up.connmgr, am);
1501 ofproto_async_msg_free(am);
1502 }
1503 }
1504
1505 if (ofproto->netflow) {
1506 netflow_run(ofproto->netflow);
1507 }
1508 if (ofproto->sflow) {
1509 dpif_sflow_run(ofproto->sflow);
1510 }
1511 if (ofproto->ipfix) {
1512 dpif_ipfix_run(ofproto->ipfix);
1513 }
1514
1515 new_seq = seq_read(connectivity_seq_get());
1516 if (ofproto->change_seq != new_seq) {
1517 struct ofport_dpif *ofport;
1518
1519 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1520 port_run(ofport);
1521 }
1522
1523 ofproto->change_seq = new_seq;
1524 }
1525 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1526 struct ofbundle *bundle;
1527
1528 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1529 bundle_run(bundle);
1530 }
1531 }
1532
1533 stp_run(ofproto);
1534 rstp_run(ofproto);
1535 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1536 if (mac_learning_run(ofproto->ml)) {
1537 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1538 }
1539 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1540
1541 if (mcast_snooping_run(ofproto->ms)) {
1542 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1543 }
1544
1545 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1546 if (ofproto->dump_seq != new_dump_seq) {
1547 struct rule *rule, *next_rule;
1548 long long now = time_msec();
1549
1550 /* We know stats are relatively fresh, so now is a good time to do some
1551 * periodic work. */
1552 ofproto->dump_seq = new_dump_seq;
1553
1554 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1555 * has passed. */
1556 ovs_mutex_lock(&ofproto_mutex);
1557 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1558 &ofproto->up.expirable) {
1559 rule_expire(rule_dpif_cast(rule), now);
1560 }
1561 ovs_mutex_unlock(&ofproto_mutex);
1562
1563 /* All outstanding data in existing flows has been accounted, so it's a
1564 * good time to do bond rebalancing. */
1565 if (ofproto->has_bonded_bundles) {
1566 struct ofbundle *bundle;
1567
1568 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1569 if (bundle->bond) {
1570 bond_rebalance(bundle->bond);
1571 }
1572 }
1573 }
1574 }
1575 return 0;
1576 }
1577
1578 static void
1579 ofproto_dpif_wait(struct ofproto *ofproto_)
1580 {
1581 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1582
1583 if (ofproto_get_flow_restore_wait()) {
1584 return;
1585 }
1586
1587 if (ofproto->sflow) {
1588 dpif_sflow_wait(ofproto->sflow);
1589 }
1590 if (ofproto->ipfix) {
1591 dpif_ipfix_wait(ofproto->ipfix);
1592 }
1593 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1594 struct ofbundle *bundle;
1595
1596 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1597 bundle_wait(bundle);
1598 }
1599 }
1600 if (ofproto->netflow) {
1601 netflow_wait(ofproto->netflow);
1602 }
1603 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1604 mac_learning_wait(ofproto->ml);
1605 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1606 mcast_snooping_wait(ofproto->ms);
1607 stp_wait(ofproto);
1608 if (ofproto->backer->need_revalidate) {
1609 poll_immediate_wake();
1610 }
1611
1612 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1613 seq_wait(ofproto->ams_seq, ofproto->ams_seqno);
1614 }
1615
1616 static void
1617 type_get_memory_usage(const char *type, struct simap *usage)
1618 {
1619 struct dpif_backer *backer;
1620
1621 backer = shash_find_data(&all_dpif_backers, type);
1622 if (backer) {
1623 udpif_get_memory_usage(backer->udpif, usage);
1624 }
1625 }
1626
1627 static void
1628 flush(struct ofproto *ofproto_)
1629 {
1630 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1631 struct dpif_backer *backer = ofproto->backer;
1632
1633 if (backer) {
1634 udpif_flush(backer->udpif);
1635 }
1636 }
1637
1638 static void
1639 query_tables(struct ofproto *ofproto,
1640 struct ofputil_table_features *features,
1641 struct ofputil_table_stats *stats)
1642 {
1643 strcpy(features->name, "classifier");
1644
1645 if (stats) {
1646 int i;
1647
1648 for (i = 0; i < ofproto->n_tables; i++) {
1649 unsigned long missed, matched;
1650
1651 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1652 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1653
1654 stats[i].matched_count = matched;
1655 stats[i].lookup_count = matched + missed;
1656 }
1657 }
1658 }
1659
1660 static void
1661 set_tables_version(struct ofproto *ofproto_, cls_version_t version)
1662 {
1663 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1664
1665 atomic_store_relaxed(&ofproto->tables_version, version);
1666 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
1667 }
1668
1669 static struct ofport *
1670 port_alloc(void)
1671 {
1672 struct ofport_dpif *port = xzalloc(sizeof *port);
1673 return &port->up;
1674 }
1675
1676 static void
1677 port_dealloc(struct ofport *port_)
1678 {
1679 struct ofport_dpif *port = ofport_dpif_cast(port_);
1680 free(port);
1681 }
1682
1683 static int
1684 port_construct(struct ofport *port_)
1685 {
1686 struct ofport_dpif *port = ofport_dpif_cast(port_);
1687 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1688 const struct netdev *netdev = port->up.netdev;
1689 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1690 const char *dp_port_name;
1691 struct dpif_port dpif_port;
1692 int error;
1693
1694 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1695 port->bundle = NULL;
1696 port->cfm = NULL;
1697 port->bfd = NULL;
1698 port->lldp = NULL;
1699 port->may_enable = false;
1700 port->stp_port = NULL;
1701 port->stp_state = STP_DISABLED;
1702 port->rstp_port = NULL;
1703 port->rstp_state = RSTP_DISABLED;
1704 port->is_tunnel = false;
1705 port->peer = NULL;
1706 port->qdscp = NULL;
1707 port->n_qdscp = 0;
1708 port->carrier_seq = netdev_get_carrier_resets(netdev);
1709 port->is_layer3 = netdev_vport_is_layer3(netdev);
1710
1711 if (netdev_vport_is_patch(netdev)) {
1712 /* By bailing out here, we don't submit the port to the sFlow module
1713 * to be considered for counter polling export. This is correct
1714 * because the patch port represents an interface that sFlow considers
1715 * to be "internal" to the switch as a whole, and therefore not a
1716 * candidate for counter polling. */
1717 port->odp_port = ODPP_NONE;
1718 ofport_update_peer(port);
1719 return 0;
1720 }
1721
1722 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1723 error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
1724 &dpif_port);
1725 if (error) {
1726 return error;
1727 }
1728
1729 port->odp_port = dpif_port.port_no;
1730
1731 if (netdev_get_tunnel_config(netdev)) {
1732 atomic_count_inc(&ofproto->backer->tnl_count);
1733 error = tnl_port_add(port, port->up.netdev, port->odp_port,
1734 ovs_native_tunneling_is_on(ofproto), dp_port_name);
1735 if (error) {
1736 atomic_count_dec(&ofproto->backer->tnl_count);
1737 dpif_port_destroy(&dpif_port);
1738 return error;
1739 }
1740
1741 port->is_tunnel = true;
1742 if (ofproto->ipfix) {
1743 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1744 }
1745 } else {
1746 /* Sanity-check that a mapping doesn't already exist. This
1747 * shouldn't happen for non-tunnel ports. */
1748 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1749 VLOG_ERR("port %s already has an OpenFlow port number",
1750 dpif_port.name);
1751 dpif_port_destroy(&dpif_port);
1752 return EBUSY;
1753 }
1754
1755 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1756 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1757 hash_odp_port(port->odp_port));
1758 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1759 }
1760 dpif_port_destroy(&dpif_port);
1761
1762 if (ofproto->sflow) {
1763 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1764 }
1765
1766 return 0;
1767 }
1768
1769 static void
1770 port_destruct(struct ofport *port_, bool del)
1771 {
1772 struct ofport_dpif *port = ofport_dpif_cast(port_);
1773 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1774 const char *devname = netdev_get_name(port->up.netdev);
1775 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1776 const char *dp_port_name;
1777
1778 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1779 xlate_txn_start();
1780 xlate_ofport_remove(port);
1781 xlate_txn_commit();
1782
1783 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1784 sizeof namebuf);
1785 if (del && dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1786 /* The underlying device is still there, so delete it. This
1787 * happens when the ofproto is being destroyed, since the caller
1788 * assumes that removal of attached ports will happen as part of
1789 * destruction. */
1790 if (!port->is_tunnel) {
1791 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1792 }
1793 }
1794
1795 if (port->peer) {
1796 port->peer->peer = NULL;
1797 port->peer = NULL;
1798 }
1799
1800 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1801 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1802 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1803 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1804 }
1805
1806 if (port->is_tunnel) {
1807 atomic_count_dec(&ofproto->backer->tnl_count);
1808 }
1809
1810 if (port->is_tunnel && ofproto->ipfix) {
1811 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1812 }
1813
1814 tnl_port_del(port);
1815 sset_find_and_delete(&ofproto->ports, devname);
1816 sset_find_and_delete(&ofproto->ghost_ports, devname);
1817 bundle_remove(port_);
1818 set_cfm(port_, NULL);
1819 set_bfd(port_, NULL);
1820 set_lldp(port_, NULL);
1821 if (port->stp_port) {
1822 stp_port_disable(port->stp_port);
1823 }
1824 set_rstp_port(port_, NULL);
1825 if (ofproto->sflow) {
1826 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1827 }
1828
1829 free(port->qdscp);
1830 }
1831
1832 static void
1833 port_modified(struct ofport *port_)
1834 {
1835 struct ofport_dpif *port = ofport_dpif_cast(port_);
1836 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1837 const char *dp_port_name;
1838 struct netdev *netdev = port->up.netdev;
1839
1840 if (port->bundle && port->bundle->bond) {
1841 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1842 }
1843
1844 if (port->cfm) {
1845 cfm_set_netdev(port->cfm, netdev);
1846 }
1847
1848 if (port->bfd) {
1849 bfd_set_netdev(port->bfd, netdev);
1850 }
1851
1852 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1853 port->lldp, &port->up.pp.hw_addr);
1854
1855 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1856
1857 if (port->is_tunnel) {
1858 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1859
1860 if (tnl_port_reconfigure(port, netdev, port->odp_port,
1861 ovs_native_tunneling_is_on(ofproto),
1862 dp_port_name)) {
1863 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1864 }
1865 }
1866
1867 ofport_update_peer(port);
1868 }
1869
1870 static void
1871 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1872 {
1873 struct ofport_dpif *port = ofport_dpif_cast(port_);
1874 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1875 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1876
1877 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1878 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1879 OFPUTIL_PC_NO_PACKET_IN)) {
1880 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1881
1882 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1883 bundle_update(port->bundle);
1884 }
1885 }
1886 }
1887
1888 static int
1889 set_sflow(struct ofproto *ofproto_,
1890 const struct ofproto_sflow_options *sflow_options)
1891 {
1892 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1893 struct dpif_sflow *ds = ofproto->sflow;
1894
1895 if (sflow_options) {
1896 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
1897 if (!ds) {
1898 struct ofport_dpif *ofport;
1899
1900 ds = ofproto->sflow = dpif_sflow_create();
1901 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1902 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1903 }
1904 }
1905 dpif_sflow_set_options(ds, sflow_options);
1906 if (dpif_sflow_get_probability(ds) != old_probability) {
1907 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1908 }
1909 } else {
1910 if (ds) {
1911 dpif_sflow_unref(ds);
1912 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1913 ofproto->sflow = NULL;
1914 }
1915 }
1916 return 0;
1917 }
1918
1919 static int
1920 set_ipfix(
1921 struct ofproto *ofproto_,
1922 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1923 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1924 size_t n_flow_exporters_options)
1925 {
1926 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1927 struct dpif_ipfix *di = ofproto->ipfix;
1928 bool has_options = bridge_exporter_options || flow_exporters_options;
1929 bool new_di = false;
1930
1931 if (has_options && !di) {
1932 di = ofproto->ipfix = dpif_ipfix_create();
1933 new_di = true;
1934 }
1935
1936 if (di) {
1937 /* Call set_options in any case to cleanly flush the flow
1938 * caches in the last exporters that are to be destroyed. */
1939 dpif_ipfix_set_options(
1940 di, bridge_exporter_options, flow_exporters_options,
1941 n_flow_exporters_options);
1942
1943 /* Add tunnel ports only when a new ipfix created */
1944 if (new_di == true) {
1945 struct ofport_dpif *ofport;
1946 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1947 if (ofport->is_tunnel == true) {
1948 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
1949 }
1950 }
1951 }
1952
1953 if (!has_options) {
1954 dpif_ipfix_unref(di);
1955 ofproto->ipfix = NULL;
1956 }
1957 }
1958
1959 return 0;
1960 }
1961
1962 static int
1963 get_ipfix_stats(const struct ofproto *ofproto_,
1964 bool bridge_ipfix,
1965 struct ovs_list *replies)
1966 {
1967 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1968 struct dpif_ipfix *di = ofproto->ipfix;
1969
1970 if (!di) {
1971 return OFPERR_NXST_NOT_CONFIGURED;
1972 }
1973
1974 return dpif_ipfix_get_stats(di, bridge_ipfix, replies);
1975 }
1976
1977 static int
1978 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1979 {
1980 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1981 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1982 struct cfm *old = ofport->cfm;
1983 int error = 0;
1984
1985 if (s) {
1986 if (!ofport->cfm) {
1987 ofport->cfm = cfm_create(ofport->up.netdev);
1988 }
1989
1990 if (cfm_configure(ofport->cfm, s)) {
1991 error = 0;
1992 goto out;
1993 }
1994
1995 error = EINVAL;
1996 }
1997 cfm_unref(ofport->cfm);
1998 ofport->cfm = NULL;
1999 out:
2000 if (ofport->cfm != old) {
2001 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2002 }
2003 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2004 ofport->lldp, &ofport->up.pp.hw_addr);
2005 return error;
2006 }
2007
2008 static bool
2009 cfm_status_changed(struct ofport *ofport_)
2010 {
2011 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2012
2013 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
2014 }
2015
2016 static int
2017 get_cfm_status(const struct ofport *ofport_,
2018 struct cfm_status *status)
2019 {
2020 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2021 int ret = 0;
2022
2023 if (ofport->cfm) {
2024 cfm_get_status(ofport->cfm, status);
2025 } else {
2026 ret = ENOENT;
2027 }
2028
2029 return ret;
2030 }
2031
2032 static int
2033 set_bfd(struct ofport *ofport_, const struct smap *cfg)
2034 {
2035 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2036 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2037 struct bfd *old;
2038
2039 old = ofport->bfd;
2040 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2041 cfg, ofport->up.netdev);
2042 if (ofport->bfd != old) {
2043 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2044 }
2045 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2046 ofport->lldp, &ofport->up.pp.hw_addr);
2047 return 0;
2048 }
2049
2050 static bool
2051 bfd_status_changed(struct ofport *ofport_)
2052 {
2053 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2054
2055 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2056 }
2057
2058 static int
2059 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2060 {
2061 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2062 int ret = 0;
2063
2064 if (ofport->bfd) {
2065 bfd_get_status(ofport->bfd, smap);
2066 } else {
2067 ret = ENOENT;
2068 }
2069
2070 return ret;
2071 }
2072
2073 static int
2074 set_lldp(struct ofport *ofport_,
2075 const struct smap *cfg)
2076 {
2077 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2078 int error = 0;
2079
2080 if (cfg) {
2081 if (!ofport->lldp) {
2082 struct ofproto_dpif *ofproto;
2083
2084 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2085 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2086 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2087 }
2088
2089 if (!lldp_configure(ofport->lldp, cfg)) {
2090 error = EINVAL;
2091 }
2092 }
2093 if (error) {
2094 lldp_unref(ofport->lldp);
2095 ofport->lldp = NULL;
2096 }
2097
2098 ofproto_dpif_monitor_port_update(ofport,
2099 ofport->bfd,
2100 ofport->cfm,
2101 ofport->lldp,
2102 &ofport->up.pp.hw_addr);
2103 return error;
2104 }
2105
2106 static bool
2107 get_lldp_status(const struct ofport *ofport_,
2108 struct lldp_status *status OVS_UNUSED)
2109 {
2110 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2111
2112 return ofport->lldp ? true : false;
2113 }
2114
2115 static int
2116 set_aa(struct ofproto *ofproto OVS_UNUSED,
2117 const struct aa_settings *s)
2118 {
2119 return aa_configure(s);
2120 }
2121
2122 static int
2123 aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2124 const struct aa_mapping_settings *s)
2125 {
2126 return aa_mapping_register(aux, s);
2127 }
2128
2129 static int
2130 aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2131 {
2132 return aa_mapping_unregister(aux);
2133 }
2134
2135 static int
2136 aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2137 {
2138 return aa_get_vlan_queued(list);
2139 }
2140
2141 static unsigned int
2142 aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2143 {
2144 return aa_get_vlan_queue_size();
2145 }
2146
2147 \f
2148 /* Spanning Tree. */
2149
2150 /* Called while rstp_mutex is held. */
2151 static void
2152 rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
2153 {
2154 struct ofproto_dpif *ofproto = ofproto_;
2155 struct ofport_dpif *ofport = ofport_;
2156 struct eth_header *eth = dp_packet_l2(pkt);
2157
2158 netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
2159 if (eth_addr_is_zero(eth->eth_src)) {
2160 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2161 "does not have a configured source MAC address.",
2162 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2163 } else {
2164 ofproto_dpif_send_packet(ofport, pkt);
2165 }
2166 dp_packet_delete(pkt);
2167 }
2168
2169 static void
2170 send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
2171 {
2172 struct ofproto_dpif *ofproto = ofproto_;
2173 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2174 struct ofport_dpif *ofport;
2175
2176 ofport = stp_port_get_aux(sp);
2177 if (!ofport) {
2178 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2179 ofproto->up.name, port_num);
2180 } else {
2181 struct eth_header *eth = dp_packet_l2(pkt);
2182
2183 netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
2184 if (eth_addr_is_zero(eth->eth_src)) {
2185 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2186 "with unknown MAC", ofproto->up.name, port_num);
2187 } else {
2188 ofproto_dpif_send_packet(ofport, pkt);
2189 }
2190 }
2191 dp_packet_delete(pkt);
2192 }
2193
2194 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2195 static void
2196 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2197 {
2198 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2199
2200 /* Only revalidate flows if the configuration changed. */
2201 if (!s != !ofproto->rstp) {
2202 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2203 }
2204
2205 if (s) {
2206 if (!ofproto->rstp) {
2207 ofproto->rstp = rstp_create(ofproto_->name, s->address,
2208 rstp_send_bpdu_cb, ofproto);
2209 ofproto->rstp_last_tick = time_msec();
2210 }
2211 rstp_set_bridge_address(ofproto->rstp, s->address);
2212 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2213 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2214 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2215 s->force_protocol_version);
2216 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2217 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2218 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2219 s->transmit_hold_count);
2220 } else {
2221 struct ofport *ofport;
2222 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2223 set_rstp_port(ofport, NULL);
2224 }
2225 rstp_unref(ofproto->rstp);
2226 ofproto->rstp = NULL;
2227 }
2228 }
2229
2230 static void
2231 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2232 {
2233 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2234
2235 if (ofproto->rstp) {
2236 s->enabled = true;
2237 s->root_id = rstp_get_root_id(ofproto->rstp);
2238 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2239 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2240 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2241 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2242 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2243 } else {
2244 s->enabled = false;
2245 }
2246 }
2247
2248 static void
2249 update_rstp_port_state(struct ofport_dpif *ofport)
2250 {
2251 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2252 enum rstp_state state;
2253
2254 /* Figure out new state. */
2255 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2256 : RSTP_DISABLED;
2257
2258 /* Update state. */
2259 if (ofport->rstp_state != state) {
2260 enum ofputil_port_state of_state;
2261 bool fwd_change;
2262
2263 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2264 netdev_get_name(ofport->up.netdev),
2265 rstp_state_name(ofport->rstp_state),
2266 rstp_state_name(state));
2267
2268 if (rstp_learn_in_state(ofport->rstp_state)
2269 != rstp_learn_in_state(state)) {
2270 /* XXX: Learning action flows should also be flushed. */
2271 if (ofport->bundle) {
2272 if (!rstp_shift_root_learned_address(ofproto->rstp)
2273 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2274 bundle_flush_macs(ofport->bundle, false);
2275 }
2276 }
2277 }
2278 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2279 != rstp_forward_in_state(state);
2280
2281 ofproto->backer->need_revalidate = REV_RSTP;
2282 ofport->rstp_state = state;
2283
2284 if (fwd_change && ofport->bundle) {
2285 bundle_update(ofport->bundle);
2286 }
2287
2288 /* Update the RSTP state bits in the OpenFlow port description. */
2289 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2290 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2291 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2292 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2293 : 0);
2294 ofproto_port_set_state(&ofport->up, of_state);
2295 }
2296 }
2297
2298 static void
2299 rstp_run(struct ofproto_dpif *ofproto)
2300 {
2301 if (ofproto->rstp) {
2302 long long int now = time_msec();
2303 long long int elapsed = now - ofproto->rstp_last_tick;
2304 struct rstp_port *rp;
2305 struct ofport_dpif *ofport;
2306
2307 /* Every second, decrease the values of the timers. */
2308 if (elapsed >= 1000) {
2309 rstp_tick_timers(ofproto->rstp);
2310 ofproto->rstp_last_tick = now;
2311 }
2312 rp = NULL;
2313 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2314 update_rstp_port_state(ofport);
2315 }
2316 rp = NULL;
2317 ofport = NULL;
2318 /* FIXME: This check should be done on-event (i.e., when setting
2319 * p->fdb_flush) and not periodically.
2320 */
2321 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2322 if (!rstp_shift_root_learned_address(ofproto->rstp)
2323 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2324 bundle_flush_macs(ofport->bundle, false);
2325 }
2326 }
2327
2328 if (rstp_shift_root_learned_address(ofproto->rstp)) {
2329 struct ofport_dpif *old_root_aux =
2330 (struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp);
2331 struct ofport_dpif *new_root_aux =
2332 (struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp);
2333 if (old_root_aux != NULL && new_root_aux != NULL) {
2334 bundle_move(old_root_aux->bundle, new_root_aux->bundle);
2335 rstp_reset_root_changed(ofproto->rstp);
2336 }
2337 }
2338 }
2339 }
2340
2341 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2342 static int
2343 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2344 {
2345 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2346
2347 /* Only revalidate flows if the configuration changed. */
2348 if (!s != !ofproto->stp) {
2349 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2350 }
2351
2352 if (s) {
2353 if (!ofproto->stp) {
2354 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2355 send_bpdu_cb, ofproto);
2356 ofproto->stp_last_tick = time_msec();
2357 }
2358
2359 stp_set_bridge_id(ofproto->stp, s->system_id);
2360 stp_set_bridge_priority(ofproto->stp, s->priority);
2361 stp_set_hello_time(ofproto->stp, s->hello_time);
2362 stp_set_max_age(ofproto->stp, s->max_age);
2363 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2364 } else {
2365 struct ofport *ofport;
2366
2367 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2368 set_stp_port(ofport, NULL);
2369 }
2370
2371 stp_unref(ofproto->stp);
2372 ofproto->stp = NULL;
2373 }
2374
2375 return 0;
2376 }
2377
2378 static int
2379 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2380 {
2381 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2382
2383 if (ofproto->stp) {
2384 s->enabled = true;
2385 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2386 s->designated_root = stp_get_designated_root(ofproto->stp);
2387 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2388 } else {
2389 s->enabled = false;
2390 }
2391
2392 return 0;
2393 }
2394
2395 static void
2396 update_stp_port_state(struct ofport_dpif *ofport)
2397 {
2398 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2399 enum stp_state state;
2400
2401 /* Figure out new state. */
2402 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2403 : STP_DISABLED;
2404
2405 /* Update state. */
2406 if (ofport->stp_state != state) {
2407 enum ofputil_port_state of_state;
2408 bool fwd_change;
2409
2410 VLOG_DBG("port %s: STP state changed from %s to %s",
2411 netdev_get_name(ofport->up.netdev),
2412 stp_state_name(ofport->stp_state),
2413 stp_state_name(state));
2414 if (stp_learn_in_state(ofport->stp_state)
2415 != stp_learn_in_state(state)) {
2416 /* xxx Learning action flows should also be flushed. */
2417 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2418 mac_learning_flush(ofproto->ml);
2419 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2420 mcast_snooping_mdb_flush(ofproto->ms);
2421 }
2422 fwd_change = stp_forward_in_state(ofport->stp_state)
2423 != stp_forward_in_state(state);
2424
2425 ofproto->backer->need_revalidate = REV_STP;
2426 ofport->stp_state = state;
2427 ofport->stp_state_entered = time_msec();
2428
2429 if (fwd_change && ofport->bundle) {
2430 bundle_update(ofport->bundle);
2431 }
2432
2433 /* Update the STP state bits in the OpenFlow port description. */
2434 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2435 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2436 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2437 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2438 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2439 : 0);
2440 ofproto_port_set_state(&ofport->up, of_state);
2441 }
2442 }
2443
2444 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2445 * caller is responsible for assigning STP port numbers and ensuring
2446 * there are no duplicates. */
2447 static int
2448 set_stp_port(struct ofport *ofport_,
2449 const struct ofproto_port_stp_settings *s)
2450 {
2451 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2452 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2453 struct stp_port *sp = ofport->stp_port;
2454
2455 if (!s || !s->enable) {
2456 if (sp) {
2457 ofport->stp_port = NULL;
2458 stp_port_disable(sp);
2459 update_stp_port_state(ofport);
2460 }
2461 return 0;
2462 } else if (sp && stp_port_no(sp) != s->port_num
2463 && ofport == stp_port_get_aux(sp)) {
2464 /* The port-id changed, so disable the old one if it's not
2465 * already in use by another port. */
2466 stp_port_disable(sp);
2467 }
2468
2469 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2470
2471 /* Set name before enabling the port so that debugging messages can print
2472 * the name. */
2473 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2474 stp_port_enable(sp);
2475
2476 stp_port_set_aux(sp, ofport);
2477 stp_port_set_priority(sp, s->priority);
2478 stp_port_set_path_cost(sp, s->path_cost);
2479
2480 update_stp_port_state(ofport);
2481
2482 return 0;
2483 }
2484
2485 static int
2486 get_stp_port_status(struct ofport *ofport_,
2487 struct ofproto_port_stp_status *s)
2488 {
2489 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2490 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2491 struct stp_port *sp = ofport->stp_port;
2492
2493 if (!ofproto->stp || !sp) {
2494 s->enabled = false;
2495 return 0;
2496 }
2497
2498 s->enabled = true;
2499 s->port_id = stp_port_get_id(sp);
2500 s->state = stp_port_get_state(sp);
2501 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2502 s->role = stp_port_get_role(sp);
2503
2504 return 0;
2505 }
2506
2507 static int
2508 get_stp_port_stats(struct ofport *ofport_,
2509 struct ofproto_port_stp_stats *s)
2510 {
2511 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2512 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2513 struct stp_port *sp = ofport->stp_port;
2514
2515 if (!ofproto->stp || !sp) {
2516 s->enabled = false;
2517 return 0;
2518 }
2519
2520 s->enabled = true;
2521 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2522
2523 return 0;
2524 }
2525
2526 static void
2527 stp_run(struct ofproto_dpif *ofproto)
2528 {
2529 if (ofproto->stp) {
2530 long long int now = time_msec();
2531 long long int elapsed = now - ofproto->stp_last_tick;
2532 struct stp_port *sp;
2533
2534 if (elapsed > 0) {
2535 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2536 ofproto->stp_last_tick = now;
2537 }
2538 while (stp_get_changed_port(ofproto->stp, &sp)) {
2539 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2540
2541 if (ofport) {
2542 update_stp_port_state(ofport);
2543 }
2544 }
2545
2546 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2547 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2548 mac_learning_flush(ofproto->ml);
2549 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2550 mcast_snooping_mdb_flush(ofproto->ms);
2551 }
2552 }
2553 }
2554
2555 static void
2556 stp_wait(struct ofproto_dpif *ofproto)
2557 {
2558 if (ofproto->stp) {
2559 poll_timer_wait(1000);
2560 }
2561 }
2562
2563 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2564 * caller is responsible for assigning RSTP port numbers and ensuring
2565 * there are no duplicates. */
2566 static void
2567 set_rstp_port(struct ofport *ofport_,
2568 const struct ofproto_port_rstp_settings *s)
2569 {
2570 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2571 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2572 struct rstp_port *rp = ofport->rstp_port;
2573
2574 if (!s || !s->enable) {
2575 if (rp) {
2576 rstp_port_set_aux(rp, NULL);
2577 rstp_port_set_state(rp, RSTP_DISABLED);
2578 rstp_port_set_mac_operational(rp, false);
2579 ofport->rstp_port = NULL;
2580 rstp_port_unref(rp);
2581 update_rstp_port_state(ofport);
2582 }
2583 return;
2584 }
2585
2586 /* Check if need to add a new port. */
2587 if (!rp) {
2588 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2589 }
2590
2591 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2592 s->admin_edge_port, s->auto_edge,
2593 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2594 ofport);
2595 update_rstp_port_state(ofport);
2596 /* Synchronize operational status. */
2597 rstp_port_set_mac_operational(rp, ofport->may_enable);
2598 }
2599
2600 static void
2601 get_rstp_port_status(struct ofport *ofport_,
2602 struct ofproto_port_rstp_status *s)
2603 {
2604 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2605 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2606 struct rstp_port *rp = ofport->rstp_port;
2607
2608 if (!ofproto->rstp || !rp) {
2609 s->enabled = false;
2610 return;
2611 }
2612
2613 s->enabled = true;
2614 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2615 &s->designated_bridge_id, &s->designated_port_id,
2616 &s->designated_path_cost, &s->tx_count,
2617 &s->rx_count, &s->error_count, &s->uptime);
2618 }
2619
2620 \f
2621 static int
2622 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2623 size_t n_qdscp)
2624 {
2625 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2626 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2627
2628 if (ofport->n_qdscp != n_qdscp
2629 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2630 n_qdscp * sizeof *qdscp))) {
2631 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2632 free(ofport->qdscp);
2633 ofport->qdscp = n_qdscp
2634 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2635 : NULL;
2636 ofport->n_qdscp = n_qdscp;
2637 }
2638
2639 return 0;
2640 }
2641 \f
2642 /* Bundles. */
2643
2644 /* Expires all MAC learning entries associated with 'bundle' and forces its
2645 * ofproto to revalidate every flow.
2646 *
2647 * Normally MAC learning entries are removed only from the ofproto associated
2648 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2649 * are removed from every ofproto. When patch ports and SLB bonds are in use
2650 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2651 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2652 * with the host from which it migrated. */
2653 static void
2654 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2655 {
2656 struct ofproto_dpif *ofproto = bundle->ofproto;
2657 struct mac_learning *ml = ofproto->ml;
2658 struct mac_entry *mac, *next_mac;
2659
2660 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2661 ovs_rwlock_wrlock(&ml->rwlock);
2662 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2663 if (mac_entry_get_port(ml, mac) == bundle) {
2664 if (all_ofprotos) {
2665 struct ofproto_dpif *o;
2666
2667 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2668 if (o != ofproto) {
2669 struct mac_entry *e;
2670
2671 ovs_rwlock_wrlock(&o->ml->rwlock);
2672 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2673 if (e) {
2674 mac_learning_expire(o->ml, e);
2675 }
2676 ovs_rwlock_unlock(&o->ml->rwlock);
2677 }
2678 }
2679 }
2680
2681 mac_learning_expire(ml, mac);
2682 }
2683 }
2684 ovs_rwlock_unlock(&ml->rwlock);
2685 }
2686
2687 static void
2688 bundle_move(struct ofbundle *old, struct ofbundle *new)
2689 {
2690 struct ofproto_dpif *ofproto = old->ofproto;
2691 struct mac_learning *ml = ofproto->ml;
2692 struct mac_entry *mac, *next_mac;
2693
2694 ovs_assert(new->ofproto == old->ofproto);
2695
2696 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2697 ovs_rwlock_wrlock(&ml->rwlock);
2698 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2699 if (mac_entry_get_port(ml, mac) == old) {
2700 mac_entry_set_port(ml, mac, new);
2701 }
2702 }
2703 ovs_rwlock_unlock(&ml->rwlock);
2704 }
2705
2706 static struct ofbundle *
2707 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2708 {
2709 struct ofbundle *bundle;
2710
2711 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2712 &ofproto->bundles) {
2713 if (bundle->aux == aux) {
2714 return bundle;
2715 }
2716 }
2717 return NULL;
2718 }
2719
2720 static void
2721 bundle_update(struct ofbundle *bundle)
2722 {
2723 struct ofport_dpif *port;
2724
2725 bundle->floodable = true;
2726 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2727 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2728 || port->is_layer3
2729 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2730 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2731 bundle->floodable = false;
2732 break;
2733 }
2734 }
2735 }
2736
2737 static void
2738 bundle_del_port(struct ofport_dpif *port)
2739 {
2740 struct ofbundle *bundle = port->bundle;
2741
2742 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2743
2744 ovs_list_remove(&port->bundle_node);
2745 port->bundle = NULL;
2746
2747 if (bundle->lacp) {
2748 lacp_slave_unregister(bundle->lacp, port);
2749 }
2750 if (bundle->bond) {
2751 bond_slave_unregister(bundle->bond, port);
2752 }
2753
2754 bundle_update(bundle);
2755 }
2756
2757 static bool
2758 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2759 struct lacp_slave_settings *lacp)
2760 {
2761 struct ofport_dpif *port;
2762
2763 port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
2764 if (!port) {
2765 return false;
2766 }
2767
2768 if (port->bundle != bundle) {
2769 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2770 if (port->bundle) {
2771 bundle_remove(&port->up);
2772 }
2773
2774 port->bundle = bundle;
2775 ovs_list_push_back(&bundle->ports, &port->bundle_node);
2776 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2777 || port->is_layer3
2778 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2779 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2780 bundle->floodable = false;
2781 }
2782 }
2783 if (lacp) {
2784 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2785 lacp_slave_register(bundle->lacp, port, lacp);
2786 }
2787
2788 return true;
2789 }
2790
2791 static void
2792 bundle_destroy(struct ofbundle *bundle)
2793 {
2794 struct ofproto_dpif *ofproto;
2795 struct ofport_dpif *port, *next_port;
2796
2797 if (!bundle) {
2798 return;
2799 }
2800
2801 ofproto = bundle->ofproto;
2802 mbridge_unregister_bundle(ofproto->mbridge, bundle);
2803
2804 xlate_txn_start();
2805 xlate_bundle_remove(bundle);
2806 xlate_txn_commit();
2807
2808 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2809 bundle_del_port(port);
2810 }
2811
2812 bundle_flush_macs(bundle, true);
2813 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2814 free(bundle->name);
2815 free(bundle->trunks);
2816 lacp_unref(bundle->lacp);
2817 bond_unref(bundle->bond);
2818 free(bundle);
2819 }
2820
2821 static int
2822 bundle_set(struct ofproto *ofproto_, void *aux,
2823 const struct ofproto_bundle_settings *s)
2824 {
2825 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2826 bool need_flush = false;
2827 struct ofport_dpif *port;
2828 struct ofbundle *bundle;
2829 unsigned long *trunks;
2830 int vlan;
2831 size_t i;
2832 bool ok;
2833
2834 if (!s) {
2835 bundle_destroy(bundle_lookup(ofproto, aux));
2836 return 0;
2837 }
2838
2839 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2840 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2841
2842 bundle = bundle_lookup(ofproto, aux);
2843 if (!bundle) {
2844 bundle = xmalloc(sizeof *bundle);
2845
2846 bundle->ofproto = ofproto;
2847 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2848 hash_pointer(aux, 0));
2849 bundle->aux = aux;
2850 bundle->name = NULL;
2851
2852 ovs_list_init(&bundle->ports);
2853 bundle->vlan_mode = PORT_VLAN_TRUNK;
2854 bundle->vlan = -1;
2855 bundle->trunks = NULL;
2856 bundle->use_priority_tags = s->use_priority_tags;
2857 bundle->lacp = NULL;
2858 bundle->bond = NULL;
2859
2860 bundle->floodable = true;
2861 mbridge_register_bundle(ofproto->mbridge, bundle);
2862 }
2863
2864 if (!bundle->name || strcmp(s->name, bundle->name)) {
2865 free(bundle->name);
2866 bundle->name = xstrdup(s->name);
2867 }
2868
2869 /* LACP. */
2870 if (s->lacp) {
2871 ofproto->lacp_enabled = true;
2872 if (!bundle->lacp) {
2873 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2874 bundle->lacp = lacp_create();
2875 }
2876 lacp_configure(bundle->lacp, s->lacp);
2877 } else {
2878 lacp_unref(bundle->lacp);
2879 bundle->lacp = NULL;
2880 }
2881
2882 /* Update set of ports. */
2883 ok = true;
2884 for (i = 0; i < s->n_slaves; i++) {
2885 if (!bundle_add_port(bundle, s->slaves[i],
2886 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2887 ok = false;
2888 }
2889 }
2890 if (!ok || ovs_list_size(&bundle->ports) != s->n_slaves) {
2891 struct ofport_dpif *next_port;
2892
2893 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2894 for (i = 0; i < s->n_slaves; i++) {
2895 if (s->slaves[i] == port->up.ofp_port) {
2896 goto found;
2897 }
2898 }
2899
2900 bundle_del_port(port);
2901 found: ;
2902 }
2903 }
2904 ovs_assert(ovs_list_size(&bundle->ports) <= s->n_slaves);
2905
2906 if (ovs_list_is_empty(&bundle->ports)) {
2907 bundle_destroy(bundle);
2908 return EINVAL;
2909 }
2910
2911 /* Set VLAN tagging mode */
2912 if (s->vlan_mode != bundle->vlan_mode
2913 || s->use_priority_tags != bundle->use_priority_tags) {
2914 bundle->vlan_mode = s->vlan_mode;
2915 bundle->use_priority_tags = s->use_priority_tags;
2916 need_flush = true;
2917 }
2918
2919 /* Set VLAN tag. */
2920 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2921 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2922 : 0);
2923 if (vlan != bundle->vlan) {
2924 bundle->vlan = vlan;
2925 need_flush = true;
2926 }
2927
2928 /* Get trunked VLANs. */
2929 switch (s->vlan_mode) {
2930 case PORT_VLAN_ACCESS:
2931 trunks = NULL;
2932 break;
2933
2934 case PORT_VLAN_TRUNK:
2935 trunks = CONST_CAST(unsigned long *, s->trunks);
2936 break;
2937
2938 case PORT_VLAN_NATIVE_UNTAGGED:
2939 case PORT_VLAN_NATIVE_TAGGED:
2940 if (vlan != 0 && (!s->trunks
2941 || !bitmap_is_set(s->trunks, vlan)
2942 || bitmap_is_set(s->trunks, 0))) {
2943 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2944 if (s->trunks) {
2945 trunks = bitmap_clone(s->trunks, 4096);
2946 } else {
2947 trunks = bitmap_allocate1(4096);
2948 }
2949 bitmap_set1(trunks, vlan);
2950 bitmap_set0(trunks, 0);
2951 } else {
2952 trunks = CONST_CAST(unsigned long *, s->trunks);
2953 }
2954 break;
2955
2956 default:
2957 OVS_NOT_REACHED();
2958 }
2959 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2960 free(bundle->trunks);
2961 if (trunks == s->trunks) {
2962 bundle->trunks = vlan_bitmap_clone(trunks);
2963 } else {
2964 bundle->trunks = trunks;
2965 trunks = NULL;
2966 }
2967 need_flush = true;
2968 }
2969 if (trunks != s->trunks) {
2970 free(trunks);
2971 }
2972
2973 /* Bonding. */
2974 if (!ovs_list_is_short(&bundle->ports)) {
2975 bundle->ofproto->has_bonded_bundles = true;
2976 if (bundle->bond) {
2977 if (bond_reconfigure(bundle->bond, s->bond)) {
2978 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2979 }
2980 } else {
2981 bundle->bond = bond_create(s->bond, ofproto);
2982 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2983 }
2984
2985 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2986 bond_slave_register(bundle->bond, port,
2987 port->up.ofp_port, port->up.netdev);
2988 }
2989 } else {
2990 bond_unref(bundle->bond);
2991 bundle->bond = NULL;
2992 }
2993
2994 /* If we changed something that would affect MAC learning, un-learn
2995 * everything on this port and force flow revalidation. */
2996 if (need_flush) {
2997 bundle_flush_macs(bundle, false);
2998 }
2999
3000 return 0;
3001 }
3002
3003 static void
3004 bundle_remove(struct ofport *port_)
3005 {
3006 struct ofport_dpif *port = ofport_dpif_cast(port_);
3007 struct ofbundle *bundle = port->bundle;
3008
3009 if (bundle) {
3010 bundle_del_port(port);
3011 if (ovs_list_is_empty(&bundle->ports)) {
3012 bundle_destroy(bundle);
3013 } else if (ovs_list_is_short(&bundle->ports)) {
3014 bond_unref(bundle->bond);
3015 bundle->bond = NULL;
3016 }
3017 }
3018 }
3019
3020 static void
3021 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
3022 {
3023 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3024 struct ofport_dpif *port = port_;
3025 struct eth_addr ea;
3026 int error;
3027
3028 error = netdev_get_etheraddr(port->up.netdev, &ea);
3029 if (!error) {
3030 struct dp_packet packet;
3031 void *packet_pdu;
3032
3033 dp_packet_init(&packet, 0);
3034 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
3035 pdu_size);
3036 memcpy(packet_pdu, pdu, pdu_size);
3037
3038 ofproto_dpif_send_packet(port, &packet);
3039 dp_packet_uninit(&packet);
3040 } else {
3041 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
3042 "%s (%s)", port->bundle->name,
3043 netdev_get_name(port->up.netdev), ovs_strerror(error));
3044 }
3045 }
3046
3047 static void
3048 bundle_send_learning_packets(struct ofbundle *bundle)
3049 {
3050 struct ofproto_dpif *ofproto = bundle->ofproto;
3051 int error, n_packets, n_errors;
3052 struct mac_entry *e;
3053 struct pkt_list {
3054 struct ovs_list list_node;
3055 struct ofport_dpif *port;
3056 struct dp_packet *pkt;
3057 } *pkt_node;
3058 struct ovs_list packets;
3059
3060 ovs_list_init(&packets);
3061 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3062 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3063 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
3064 pkt_node = xmalloc(sizeof *pkt_node);
3065 pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
3066 e->mac, e->vlan,
3067 (void **)&pkt_node->port);
3068 ovs_list_push_back(&packets, &pkt_node->list_node);
3069 }
3070 }
3071 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3072
3073 error = n_packets = n_errors = 0;
3074 LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
3075 int ret;
3076
3077 ret = ofproto_dpif_send_packet(pkt_node->port, pkt_node->pkt);
3078 dp_packet_delete(pkt_node->pkt);
3079 free(pkt_node);
3080 if (ret) {
3081 error = ret;
3082 n_errors++;
3083 }
3084 n_packets++;
3085 }
3086
3087 if (n_errors) {
3088 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3089 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3090 "packets, last error was: %s",
3091 bundle->name, n_errors, n_packets, ovs_strerror(error));
3092 } else {
3093 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3094 bundle->name, n_packets);
3095 }
3096 }
3097
3098 static void
3099 bundle_run(struct ofbundle *bundle)
3100 {
3101 if (bundle->lacp) {
3102 lacp_run(bundle->lacp, send_pdu_cb);
3103 }
3104 if (bundle->bond) {
3105 struct ofport_dpif *port;
3106
3107 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3108 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3109 }
3110
3111 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3112 bundle->ofproto->backer->need_revalidate = REV_BOND;
3113 }
3114
3115 if (bond_should_send_learning_packets(bundle->bond)) {
3116 bundle_send_learning_packets(bundle);
3117 }
3118 }
3119 }
3120
3121 static void
3122 bundle_wait(struct ofbundle *bundle)
3123 {
3124 if (bundle->lacp) {
3125 lacp_wait(bundle->lacp);
3126 }
3127 if (bundle->bond) {
3128 bond_wait(bundle->bond);
3129 }
3130 }
3131 \f
3132 /* Mirrors. */
3133
3134 static int
3135 mirror_set__(struct ofproto *ofproto_, void *aux,
3136 const struct ofproto_mirror_settings *s)
3137 {
3138 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3139 struct ofbundle **srcs, **dsts;
3140 int error;
3141 size_t i;
3142
3143 if (!s) {
3144 mirror_destroy(ofproto->mbridge, aux);
3145 return 0;
3146 }
3147
3148 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3149 dsts = xmalloc(s->n_dsts * sizeof *dsts);
3150
3151 for (i = 0; i < s->n_srcs; i++) {
3152 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3153 }
3154
3155 for (i = 0; i < s->n_dsts; i++) {
3156 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3157 }
3158
3159 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3160 s->n_dsts, s->src_vlans,
3161 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
3162 free(srcs);
3163 free(dsts);
3164 return error;
3165 }
3166
3167 static int
3168 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3169 uint64_t *packets, uint64_t *bytes)
3170 {
3171 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3172 bytes);
3173 }
3174
3175 static int
3176 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3177 {
3178 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3179 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3180 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3181 mac_learning_flush(ofproto->ml);
3182 }
3183 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3184 return 0;
3185 }
3186
3187 static bool
3188 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3189 {
3190 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3191 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3192 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3193 }
3194
3195 static void
3196 forward_bpdu_changed(struct ofproto *ofproto_)
3197 {
3198 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3199 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3200 }
3201
3202 static void
3203 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3204 size_t max_entries)
3205 {
3206 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3207 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3208 mac_learning_set_idle_time(ofproto->ml, idle_time);
3209 mac_learning_set_max_entries(ofproto->ml, max_entries);
3210 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3211 }
3212
3213 /* Configures multicast snooping on 'ofport' using the settings
3214 * defined in 's'. */
3215 static int
3216 set_mcast_snooping(struct ofproto *ofproto_,
3217 const struct ofproto_mcast_snooping_settings *s)
3218 {
3219 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3220
3221 /* Only revalidate flows if the configuration changed. */
3222 if (!s != !ofproto->ms) {
3223 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3224 }
3225
3226 if (s) {
3227 if (!ofproto->ms) {
3228 ofproto->ms = mcast_snooping_create();
3229 }
3230
3231 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3232 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3233 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3234 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3235 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3236 }
3237 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3238 } else {
3239 mcast_snooping_unref(ofproto->ms);
3240 ofproto->ms = NULL;
3241 }
3242
3243 return 0;
3244 }
3245
3246 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3247 static int
3248 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3249 const struct ofproto_mcast_snooping_port_settings *s)
3250 {
3251 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3252 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3253
3254 if (ofproto->ms && s) {
3255 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3256 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3257 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3258 s->flood_reports);
3259 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3260 }
3261 return 0;
3262 }
3263
3264 \f
3265 /* Ports. */
3266
3267 struct ofport_dpif *
3268 ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3269 {
3270 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3271 return ofport ? ofport_dpif_cast(ofport) : NULL;
3272 }
3273
3274 static void
3275 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3276 struct ofproto_port *ofproto_port,
3277 struct dpif_port *dpif_port)
3278 {
3279 ofproto_port->name = dpif_port->name;
3280 ofproto_port->type = dpif_port->type;
3281 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3282 }
3283
3284 static void
3285 ofport_update_peer(struct ofport_dpif *ofport)
3286 {
3287 const struct ofproto_dpif *ofproto;
3288 struct dpif_backer *backer;
3289 char *peer_name;
3290
3291 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3292 return;
3293 }
3294
3295 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3296 backer->need_revalidate = REV_RECONFIGURE;
3297
3298 if (ofport->peer) {
3299 ofport->peer->peer = NULL;
3300 ofport->peer = NULL;
3301 }
3302
3303 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3304 if (!peer_name) {
3305 return;
3306 }
3307
3308 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3309 struct ofport *peer_ofport;
3310 struct ofport_dpif *peer;
3311 char *peer_peer;
3312
3313 if (ofproto->backer != backer) {
3314 continue;
3315 }
3316
3317 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3318 if (!peer_ofport) {
3319 continue;
3320 }
3321
3322 peer = ofport_dpif_cast(peer_ofport);
3323 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3324 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3325 peer_peer)) {
3326 ofport->peer = peer;
3327 ofport->peer->peer = ofport;
3328 }
3329 free(peer_peer);
3330
3331 break;
3332 }
3333 free(peer_name);
3334 }
3335
3336 static void
3337 port_run(struct ofport_dpif *ofport)
3338 {
3339 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3340 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3341 bool enable = netdev_get_carrier(ofport->up.netdev);
3342 bool cfm_enable = false;
3343 bool bfd_enable = false;
3344
3345 ofport->carrier_seq = carrier_seq;
3346
3347 if (ofport->cfm) {
3348 int cfm_opup = cfm_get_opup(ofport->cfm);
3349
3350 cfm_enable = !cfm_get_fault(ofport->cfm);
3351
3352 if (cfm_opup >= 0) {
3353 cfm_enable = cfm_enable && cfm_opup;
3354 }
3355 }
3356
3357 if (ofport->bfd) {
3358 bfd_enable = bfd_forwarding(ofport->bfd);
3359 }
3360
3361 if (ofport->bfd || ofport->cfm) {
3362 enable = enable && (cfm_enable || bfd_enable);
3363 }
3364
3365 if (ofport->bundle) {
3366 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3367 if (carrier_changed) {
3368 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3369 }
3370 }
3371
3372 if (ofport->may_enable != enable) {
3373 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3374
3375 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3376
3377 if (ofport->rstp_port) {
3378 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3379 }
3380 }
3381
3382 ofport->may_enable = enable;
3383 }
3384
3385 static int
3386 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3387 struct ofproto_port *ofproto_port)
3388 {
3389 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3390 struct dpif_port dpif_port;
3391 int error;
3392
3393 if (sset_contains(&ofproto->ghost_ports, devname)) {
3394 const char *type = netdev_get_type_from_name(devname);
3395
3396 /* We may be called before ofproto->up.port_by_name is populated with
3397 * the appropriate ofport. For this reason, we must get the name and
3398 * type from the netdev layer directly. */
3399 if (type) {
3400 const struct ofport *ofport;
3401
3402 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3403 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3404 ofproto_port->name = xstrdup(devname);
3405 ofproto_port->type = xstrdup(type);
3406 return 0;
3407 }
3408 return ENODEV;
3409 }
3410
3411 if (!sset_contains(&ofproto->ports, devname)) {
3412 return ENODEV;
3413 }
3414 error = dpif_port_query_by_name(ofproto->backer->dpif,
3415 devname, &dpif_port);
3416 if (!error) {
3417 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3418 }
3419 return error;
3420 }
3421
3422 static int
3423 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3424 {
3425 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3426 const char *devname = netdev_get_name(netdev);
3427 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3428 const char *dp_port_name;
3429
3430 if (netdev_vport_is_patch(netdev)) {
3431 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3432 return 0;
3433 }
3434
3435 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3436 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3437 odp_port_t port_no = ODPP_NONE;
3438 int error;
3439
3440 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3441 if (error) {
3442 return error;
3443 }
3444 if (netdev_get_tunnel_config(netdev)) {
3445 simap_put(&ofproto->backer->tnl_backers,
3446 dp_port_name, odp_to_u32(port_no));
3447 }
3448 }
3449
3450 if (netdev_get_tunnel_config(netdev)) {
3451 sset_add(&ofproto->ghost_ports, devname);
3452 } else {
3453 sset_add(&ofproto->ports, devname);
3454 }
3455 return 0;
3456 }
3457
3458 static int
3459 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3460 {
3461 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3462 struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
3463 int error = 0;
3464
3465 if (!ofport) {
3466 return 0;
3467 }
3468
3469 sset_find_and_delete(&ofproto->ghost_ports,
3470 netdev_get_name(ofport->up.netdev));
3471 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3472 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3473 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3474 if (!error) {
3475 /* The caller is going to close ofport->up.netdev. If this is a
3476 * bonded port, then the bond is using that netdev, so remove it
3477 * from the bond. The client will need to reconfigure everything
3478 * after deleting ports, so then the slave will get re-added. */
3479 bundle_remove(&ofport->up);
3480 }
3481 }
3482 return error;
3483 }
3484
3485 static int
3486 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3487 {
3488 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3489 int error;
3490
3491 error = netdev_get_stats(ofport->up.netdev, stats);
3492
3493 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3494 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3495
3496 ovs_mutex_lock(&ofproto->stats_mutex);
3497 /* ofproto->stats.tx_packets represents packets that we created
3498 * internally and sent to some port (e.g. packets sent with
3499 * ofproto_dpif_send_packet()). Account for them as if they had
3500 * come from OFPP_LOCAL and got forwarded. */
3501
3502 if (stats->rx_packets != UINT64_MAX) {
3503 stats->rx_packets += ofproto->stats.tx_packets;
3504 }
3505
3506 if (stats->rx_bytes != UINT64_MAX) {
3507 stats->rx_bytes += ofproto->stats.tx_bytes;
3508 }
3509
3510 /* ofproto->stats.rx_packets represents packets that were received on
3511 * some port and we processed internally and dropped (e.g. STP).
3512 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3513
3514 if (stats->tx_packets != UINT64_MAX) {
3515 stats->tx_packets += ofproto->stats.rx_packets;
3516 }
3517
3518 if (stats->tx_bytes != UINT64_MAX) {
3519 stats->tx_bytes += ofproto->stats.rx_bytes;
3520 }
3521 ovs_mutex_unlock(&ofproto->stats_mutex);
3522 }
3523
3524 return error;
3525 }
3526
3527 static int
3528 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3529 {
3530 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3531 if (ofport->bundle && ofport->bundle->lacp) {
3532 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3533 return 0;
3534 }
3535 }
3536 return -1;
3537 }
3538
3539 struct port_dump_state {
3540 struct sset_position pos;
3541 bool ghost;
3542
3543 struct ofproto_port port;
3544 bool has_port;
3545 };
3546
3547 static int
3548 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3549 {
3550 *statep = xzalloc(sizeof(struct port_dump_state));
3551 return 0;
3552 }
3553
3554 static int
3555 port_dump_next(const struct ofproto *ofproto_, void *state_,
3556 struct ofproto_port *port)
3557 {
3558 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3559 struct port_dump_state *state = state_;
3560 const struct sset *sset;
3561 struct sset_node *node;
3562
3563 if (state->has_port) {
3564 ofproto_port_destroy(&state->port);
3565 state->has_port = false;
3566 }
3567 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3568 while ((node = sset_at_position(sset, &state->pos))) {
3569 int error;
3570
3571 error = port_query_by_name(ofproto_, node->name, &state->port);
3572 if (!error) {
3573 *port = state->port;
3574 state->has_port = true;
3575 return 0;
3576 } else if (error != ENODEV) {
3577 return error;
3578 }
3579 }
3580
3581 if (!state->ghost) {
3582 state->ghost = true;
3583 memset(&state->pos, 0, sizeof state->pos);
3584 return port_dump_next(ofproto_, state_, port);
3585 }
3586
3587 return EOF;
3588 }
3589
3590 static int
3591 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3592 {
3593 struct port_dump_state *state = state_;
3594
3595 if (state->has_port) {
3596 ofproto_port_destroy(&state->port);
3597 }
3598 free(state);
3599 return 0;
3600 }
3601
3602 static int
3603 port_poll(const struct ofproto *ofproto_, char **devnamep)
3604 {
3605 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3606
3607 if (ofproto->port_poll_errno) {
3608 int error = ofproto->port_poll_errno;
3609 ofproto->port_poll_errno = 0;
3610 return error;
3611 }
3612
3613 if (sset_is_empty(&ofproto->port_poll_set)) {
3614 return EAGAIN;
3615 }
3616
3617 *devnamep = sset_pop(&ofproto->port_poll_set);
3618 return 0;
3619 }
3620
3621 static void
3622 port_poll_wait(const struct ofproto *ofproto_)
3623 {
3624 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3625 dpif_port_poll_wait(ofproto->backer->dpif);
3626 }
3627
3628 static int
3629 port_is_lacp_current(const struct ofport *ofport_)
3630 {
3631 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3632 return (ofport->bundle && ofport->bundle->lacp
3633 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3634 : -1);
3635 }
3636 \f
3637 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3638 * then delete it entirely. */
3639 static void
3640 rule_expire(struct rule_dpif *rule, long long now)
3641 OVS_REQUIRES(ofproto_mutex)
3642 {
3643 uint16_t hard_timeout, idle_timeout;
3644 int reason = -1;
3645
3646 hard_timeout = rule->up.hard_timeout;
3647 idle_timeout = rule->up.idle_timeout;
3648
3649 /* Has 'rule' expired? */
3650 if (hard_timeout) {
3651 long long int modified;
3652
3653 ovs_mutex_lock(&rule->up.mutex);
3654 modified = rule->up.modified;
3655 ovs_mutex_unlock(&rule->up.mutex);
3656
3657 if (now > modified + hard_timeout * 1000) {
3658 reason = OFPRR_HARD_TIMEOUT;
3659 }
3660 }
3661
3662 if (reason < 0 && idle_timeout) {
3663 long long int used;
3664
3665 ovs_mutex_lock(&rule->stats_mutex);
3666 used = rule->stats.used;
3667 ovs_mutex_unlock(&rule->stats_mutex);
3668
3669 if (now > used + idle_timeout * 1000) {
3670 reason = OFPRR_IDLE_TIMEOUT;
3671 }
3672 }
3673
3674 if (reason >= 0) {
3675 COVERAGE_INC(ofproto_dpif_expired);
3676 ofproto_rule_expire(&rule->up, reason);
3677 }
3678 }
3679
3680 static void
3681 ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif *ofproto,
3682 ofp_port_t in_port, struct dp_packet *packet)
3683 {
3684 if (in_port == OFPP_NONE) {
3685 in_port = OFPP_LOCAL;
3686 }
3687 packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3688 }
3689
3690 int
3691 ofproto_dpif_execute_actions__(struct ofproto_dpif *ofproto,
3692 const struct flow *flow,
3693 struct rule_dpif *rule,
3694 const struct ofpact *ofpacts, size_t ofpacts_len,
3695 int indentation, int depth, int resubmits,
3696 struct dp_packet *packet)
3697 {
3698 struct dpif_flow_stats stats;
3699 struct xlate_out xout;
3700 struct xlate_in xin;
3701 struct dpif_execute execute;
3702 int error;
3703
3704 ovs_assert((rule != NULL) != (ofpacts != NULL));
3705
3706 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3707
3708 if (rule) {
3709 rule_dpif_credit_stats(rule, &stats);
3710 }
3711
3712 uint64_t odp_actions_stub[1024 / 8];
3713 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
3714 xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
3715 stats.tcp_flags, packet, NULL, &odp_actions);
3716 xin.ofpacts = ofpacts;
3717 xin.ofpacts_len = ofpacts_len;
3718 xin.resubmit_stats = &stats;
3719 xin.indentation = indentation;
3720 xin.depth = depth;
3721 xin.resubmits = resubmits;
3722 if (xlate_actions(&xin, &xout) != XLATE_OK) {
3723 error = EINVAL;
3724 goto out;
3725 }
3726
3727 execute.actions = odp_actions.data;
3728 execute.actions_len = odp_actions.size;
3729
3730 pkt_metadata_from_flow(&packet->md, flow);
3731 execute.packet = packet;
3732 execute.flow = flow;
3733 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3734 execute.probe = false;
3735 execute.mtu = 0;
3736
3737 /* Fix up in_port. */
3738 ofproto_dpif_set_packet_odp_port(ofproto, flow->in_port.ofp_port, packet);
3739
3740 error = dpif_execute(ofproto->backer->dpif, &execute);
3741 out:
3742 xlate_out_uninit(&xout);
3743 ofpbuf_uninit(&odp_actions);
3744
3745 return error;
3746 }
3747
3748 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3749 * 'flow' must reflect the data in 'packet'. */
3750 int
3751 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3752 const struct flow *flow,
3753 struct rule_dpif *rule,
3754 const struct ofpact *ofpacts, size_t ofpacts_len,
3755 struct dp_packet *packet)
3756 {
3757 return ofproto_dpif_execute_actions__(ofproto, flow, rule, ofpacts,
3758 ofpacts_len, 0, 0, 0, packet);
3759 }
3760
3761 void
3762 rule_dpif_credit_stats(struct rule_dpif *rule,
3763 const struct dpif_flow_stats *stats)
3764 {
3765 ovs_mutex_lock(&rule->stats_mutex);
3766 if (OVS_UNLIKELY(rule->new_rule)) {
3767 rule_dpif_credit_stats(rule->new_rule, stats);
3768 } else {
3769 rule->stats.n_packets += stats->n_packets;
3770 rule->stats.n_bytes += stats->n_bytes;
3771 rule->stats.used = MAX(rule->stats.used, stats->used);
3772 }
3773 ovs_mutex_unlock(&rule->stats_mutex);
3774 }
3775
3776 ovs_be64
3777 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3778 OVS_REQUIRES(rule->up.mutex)
3779 {
3780 return rule->up.flow_cookie;
3781 }
3782
3783 void
3784 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3785 uint16_t hard_timeout)
3786 {
3787 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3788 }
3789
3790 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3791 * be read until the calling thread quiesces. */
3792 const struct rule_actions *
3793 rule_dpif_get_actions(const struct rule_dpif *rule)
3794 {
3795 return rule_get_actions(&rule->up);
3796 }
3797
3798 /* Sets 'rule''s recirculation id. */
3799 static void
3800 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3801 OVS_REQUIRES(rule->up.mutex)
3802 {
3803 ovs_assert(!rule->recirc_id || rule->recirc_id == id);
3804 if (rule->recirc_id == id) {
3805 /* Release the new reference to the same id. */
3806 recirc_free_id(id);
3807 } else {
3808 rule->recirc_id = id;
3809 }
3810 }
3811
3812 /* Sets 'rule''s recirculation id. */
3813 void
3814 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3815 {
3816 struct rule_dpif *rule = rule_dpif_cast(rule_);
3817
3818 ovs_mutex_lock(&rule->up.mutex);
3819 rule_dpif_set_recirc_id(rule, id);
3820 ovs_mutex_unlock(&rule->up.mutex);
3821 }
3822
3823 cls_version_t
3824 ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto OVS_UNUSED)
3825 {
3826 cls_version_t version;
3827
3828 atomic_read_relaxed(&ofproto->tables_version, &version);
3829
3830 return version;
3831 }
3832
3833 /* The returned rule (if any) is valid at least until the next RCU quiescent
3834 * period. If the rule needs to stay around longer, the caller should take
3835 * a reference.
3836 *
3837 * 'flow' is non-const to allow for temporary modifications during the lookup.
3838 * Any changes are restored before returning. */
3839 static struct rule_dpif *
3840 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, cls_version_t version,
3841 uint8_t table_id, struct flow *flow,
3842 struct flow_wildcards *wc)
3843 {
3844 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3845 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls, version,
3846 flow, wc)));
3847 }
3848
3849 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3850 * table '*table_id'. Returns the rule that was found, which may be one of the
3851 * special rules according to packet miss hadling. If 'may_packet_in' is
3852 * false, returning of the miss_rule (which issues packet ins for the
3853 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
3854 * that were used during the lookup.
3855 *
3856 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3857 * if none is found then the table miss configuration for that table is
3858 * honored, which can result in additional lookups in other OpenFlow tables.
3859 * In this case the function updates '*table_id' to reflect the final OpenFlow
3860 * table that was searched.
3861 *
3862 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3863 * '*table_id'.
3864 *
3865 * The rule is returned in '*rule', which is valid at least until the next
3866 * RCU quiescent period. If the '*rule' needs to stay around longer, the
3867 * caller must take a reference.
3868 *
3869 * 'in_port' allows the lookup to take place as if the in port had the value
3870 * 'in_port'. This is needed for resubmit action support.
3871 *
3872 * 'flow' is non-const to allow for temporary modifications during the lookup.
3873 * Any changes are restored before returning. */
3874 struct rule_dpif *
3875 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3876 cls_version_t version, struct flow *flow,
3877 struct flow_wildcards *wc,
3878 const struct dpif_flow_stats *stats,
3879 uint8_t *table_id, ofp_port_t in_port,
3880 bool may_packet_in, bool honor_table_miss)
3881 {
3882 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3883 ofp_port_t old_in_port = flow->in_port.ofp_port;
3884 enum ofputil_table_miss miss_config;
3885 struct rule_dpif *rule;
3886 uint8_t next_id;
3887
3888 /* We always unwildcard nw_frag (for IP), so they
3889 * need not be unwildcarded here. */
3890 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3891 && ofproto->up.frag_handling != OFPUTIL_FRAG_NX_MATCH) {
3892 if (ofproto->up.frag_handling == OFPUTIL_FRAG_NORMAL) {
3893 /* We must pretend that transport ports are unavailable. */
3894 flow->tp_src = htons(0);
3895 flow->tp_dst = htons(0);
3896 } else {
3897 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
3898 * Use the drop_frags_rule (which cannot disappear). */
3899 rule = ofproto->drop_frags_rule;
3900 if (stats) {
3901 struct oftable *tbl = &ofproto->up.tables[*table_id];
3902 unsigned long orig;
3903
3904 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3905 }
3906 return rule;
3907 }
3908 }
3909
3910 /* Look up a flow with 'in_port' as the input port. Then restore the
3911 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3912 * have surprising behavior). */
3913 flow->in_port.ofp_port = in_port;
3914
3915 /* Our current implementation depends on n_tables == N_TABLES, and
3916 * TBL_INTERNAL being the last table. */
3917 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3918
3919 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3920
3921 for (next_id = *table_id;
3922 next_id < ofproto->up.n_tables;
3923 next_id++, next_id += (next_id == TBL_INTERNAL))
3924 {
3925 *table_id = next_id;
3926 rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc);
3927 if (stats) {
3928 struct oftable *tbl = &ofproto->up.tables[next_id];
3929 unsigned long orig;
3930
3931 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3932 stats->n_packets, &orig);
3933 }
3934 if (rule) {
3935 goto out; /* Match. */
3936 }
3937 if (honor_table_miss) {
3938 miss_config = ofproto_table_get_miss_config(&ofproto->up,
3939 *table_id);
3940 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
3941 continue;
3942 }
3943 }
3944 break;
3945 }
3946 /* Miss. */
3947 rule = ofproto->no_packet_in_rule;
3948 if (may_packet_in) {
3949 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
3950 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
3951 struct ofport_dpif *port;
3952
3953 port = ofp_port_to_ofport(ofproto, old_in_port);
3954 if (!port) {
3955 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3956 old_in_port);
3957 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
3958 rule = ofproto->miss_rule;
3959 }
3960 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
3961 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3962 rule = ofproto->miss_rule;
3963 }
3964 }
3965 out:
3966 /* Restore port numbers, as they may have been modified above. */
3967 flow->tp_src = old_tp_src;
3968 flow->tp_dst = old_tp_dst;
3969 /* Restore the old in port. */
3970 flow->in_port.ofp_port = old_in_port;
3971
3972 return rule;
3973 }
3974
3975 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3976 {
3977 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3978 }
3979
3980 static struct rule *
3981 rule_alloc(void)
3982 {
3983 struct rule_dpif *rule = xzalloc(sizeof *rule);
3984 return &rule->up;
3985 }
3986
3987 static void
3988 rule_dealloc(struct rule *rule_)
3989 {
3990 struct rule_dpif *rule = rule_dpif_cast(rule_);
3991 free(rule);
3992 }
3993
3994 static enum ofperr
3995 check_mask(struct ofproto_dpif *ofproto, const struct miniflow *flow)
3996 {
3997 const struct odp_support *support;
3998 uint16_t ct_state, ct_zone;
3999 ovs_u128 ct_label;
4000 uint32_t ct_mark;
4001
4002 support = &ofproto_dpif_get_support(ofproto)->odp;
4003 ct_state = MINIFLOW_GET_U16(flow, ct_state);
4004 if (support->ct_state && support->ct_zone && support->ct_mark
4005 && support->ct_label && support->ct_state_nat) {
4006 return ct_state & CS_UNSUPPORTED_MASK ? OFPERR_OFPBMC_BAD_MASK : 0;
4007 }
4008
4009 ct_zone = MINIFLOW_GET_U16(flow, ct_zone);
4010 ct_mark = MINIFLOW_GET_U32(flow, ct_mark);
4011 ct_label = MINIFLOW_GET_U128(flow, ct_label);
4012
4013 if ((ct_state && !support->ct_state)
4014 || (ct_state & CS_UNSUPPORTED_MASK)
4015 || ((ct_state & (CS_SRC_NAT | CS_DST_NAT)) && !support->ct_state_nat)
4016 || (ct_zone && !support->ct_zone)
4017 || (ct_mark && !support->ct_mark)
4018 || (!ovs_u128_is_zero(ct_label) && !support->ct_label)) {
4019 return OFPERR_OFPBMC_BAD_MASK;
4020 }
4021
4022 return 0;
4023 }
4024
4025 static enum ofperr
4026 check_actions(const struct ofproto_dpif *ofproto,
4027 const struct rule_actions *const actions)
4028 {
4029 const struct ofpact *ofpact;
4030
4031 OFPACT_FOR_EACH (ofpact, actions->ofpacts, actions->ofpacts_len) {
4032 const struct odp_support *support;
4033 const struct ofpact_conntrack *ct;
4034 const struct ofpact *a;
4035
4036 if (ofpact->type != OFPACT_CT) {
4037 continue;
4038 }
4039
4040 ct = CONTAINER_OF(ofpact, struct ofpact_conntrack, ofpact);
4041 support = &ofproto_dpif_get_support(ofproto)->odp;
4042
4043 if (!support->ct_state) {
4044 return OFPERR_OFPBAC_BAD_TYPE;
4045 }
4046 if ((ct->zone_imm || ct->zone_src.field) && !support->ct_zone) {
4047 return OFPERR_OFPBAC_BAD_ARGUMENT;
4048 }
4049
4050 OFPACT_FOR_EACH(a, ct->actions, ofpact_ct_get_action_len(ct)) {
4051 const struct mf_field *dst = ofpact_get_mf_dst(a);
4052
4053 if (a->type == OFPACT_NAT && !support->ct_state_nat) {
4054 /* The backer doesn't seem to support the NAT bits in
4055 * 'ct_state': assume that it doesn't support the NAT
4056 * action. */
4057 return OFPERR_OFPBAC_BAD_TYPE;
4058 }
4059 if (dst && ((dst->id == MFF_CT_MARK && !support->ct_mark)
4060 || (dst->id == MFF_CT_LABEL && !support->ct_label))) {
4061 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
4062 }
4063 }
4064 }
4065
4066 return 0;
4067 }
4068
4069 static enum ofperr
4070 rule_check(struct rule *rule)
4071 {
4072 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->ofproto);
4073 enum ofperr err;
4074
4075 err = check_mask(ofproto, &rule->cr.match.mask->masks);
4076 if (err) {
4077 return err;
4078 }
4079 return check_actions(ofproto, rule->actions);
4080 }
4081
4082 static enum ofperr
4083 rule_construct(struct rule *rule_)
4084 OVS_NO_THREAD_SAFETY_ANALYSIS
4085 {
4086 struct rule_dpif *rule = rule_dpif_cast(rule_);
4087 int error;
4088
4089 error = rule_check(rule_);
4090 if (error) {
4091 return error;
4092 }
4093
4094 ovs_mutex_init_adaptive(&rule->stats_mutex);
4095 rule->stats.n_packets = 0;
4096 rule->stats.n_bytes = 0;
4097 rule->stats.used = rule->up.modified;
4098 rule->recirc_id = 0;
4099 rule->new_rule = NULL;
4100
4101 return 0;
4102 }
4103
4104 static void
4105 rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_stats)
4106 OVS_REQUIRES(ofproto_mutex)
4107 {
4108 struct rule_dpif *rule = rule_dpif_cast(rule_);
4109
4110 if (old_rule_ && forward_stats) {
4111 struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
4112
4113 ovs_assert(!old_rule->new_rule);
4114
4115 /* Take a reference to the new rule, and refer all stats updates from
4116 * the old rule to the new rule. */
4117 rule_dpif_ref(rule);
4118
4119 ovs_mutex_lock(&old_rule->stats_mutex);
4120 ovs_mutex_lock(&rule->stats_mutex);
4121 old_rule->new_rule = rule; /* Forward future stats. */
4122 rule->stats = old_rule->stats; /* Transfer stats to the new rule. */
4123 ovs_mutex_unlock(&rule->stats_mutex);
4124 ovs_mutex_unlock(&old_rule->stats_mutex);
4125 }
4126 }
4127
4128 static void
4129 rule_destruct(struct rule *rule_)
4130 OVS_NO_THREAD_SAFETY_ANALYSIS
4131 {
4132 struct rule_dpif *rule = rule_dpif_cast(rule_);
4133
4134 ovs_mutex_destroy(&rule->stats_mutex);
4135 /* Release reference to the new rule, if any. */
4136 if (rule->new_rule) {
4137 rule_dpif_unref(rule->new_rule);
4138 }
4139 if (rule->recirc_id) {
4140 recirc_free_id(rule->recirc_id);
4141 }
4142 }
4143
4144 static void
4145 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4146 long long int *used)
4147 {
4148 struct rule_dpif *rule = rule_dpif_cast(rule_);
4149
4150 ovs_mutex_lock(&rule->stats_mutex);
4151 if (OVS_UNLIKELY(rule->new_rule)) {
4152 rule_get_stats(&rule->new_rule->up, packets, bytes, used);
4153 } else {
4154 *packets = rule->stats.n_packets;
4155 *bytes = rule->stats.n_bytes;
4156 *used = rule->stats.used;
4157 }
4158 ovs_mutex_unlock(&rule->stats_mutex);
4159 }
4160
4161 static void
4162 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4163 struct dp_packet *packet)
4164 {
4165 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4166
4167 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
4168 }
4169
4170 static enum ofperr
4171 rule_execute(struct rule *rule, const struct flow *flow,
4172 struct dp_packet *packet)
4173 {
4174 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4175 dp_packet_delete(packet);
4176 return 0;
4177 }
4178
4179 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4180 {
4181 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4182 }
4183
4184 static struct ofgroup *
4185 group_alloc(void)
4186 {
4187 struct group_dpif *group = xzalloc(sizeof *group);
4188 return &group->up;
4189 }
4190
4191 static void
4192 group_dealloc(struct ofgroup *group_)
4193 {
4194 struct group_dpif *group = group_dpif_cast(group_);
4195 free(group);
4196 }
4197
4198 static void
4199 group_construct_stats(struct group_dpif *group)
4200 OVS_REQUIRES(group->stats_mutex)
4201 {
4202 struct ofputil_bucket *bucket;
4203 const struct ovs_list *buckets;
4204
4205 group->packet_count = 0;
4206 group->byte_count = 0;
4207
4208 group_dpif_get_buckets(group, &buckets);
4209 LIST_FOR_EACH (bucket, list_node, buckets) {
4210 bucket->stats.packet_count = 0;
4211 bucket->stats.byte_count = 0;
4212 }
4213 }
4214
4215 void
4216 group_dpif_credit_stats(struct group_dpif *group,
4217 struct ofputil_bucket *bucket,
4218 const struct dpif_flow_stats *stats)
4219 {
4220 ovs_mutex_lock(&group->stats_mutex);
4221 group->packet_count += stats->n_packets;
4222 group->byte_count += stats->n_bytes;
4223 if (bucket) {
4224 bucket->stats.packet_count += stats->n_packets;
4225 bucket->stats.byte_count += stats->n_bytes;
4226 } else { /* Credit to all buckets */
4227 const struct ovs_list *buckets;
4228
4229 group_dpif_get_buckets(group, &buckets);
4230 LIST_FOR_EACH (bucket, list_node, buckets) {
4231 bucket->stats.packet_count += stats->n_packets;
4232 bucket->stats.byte_count += stats->n_bytes;
4233 }
4234 }
4235 ovs_mutex_unlock(&group->stats_mutex);
4236 }
4237
4238 static enum ofperr
4239 group_construct(struct ofgroup *group_)
4240 {
4241 struct group_dpif *group = group_dpif_cast(group_);
4242
4243 ovs_mutex_init_adaptive(&group->stats_mutex);
4244 ovs_mutex_lock(&group->stats_mutex);
4245 group_construct_stats(group);
4246 ovs_mutex_unlock(&group->stats_mutex);
4247 return 0;
4248 }
4249
4250 static void
4251 group_destruct(struct ofgroup *group_)
4252 {
4253 struct group_dpif *group = group_dpif_cast(group_);
4254 ovs_mutex_destroy(&group->stats_mutex);
4255 }
4256
4257 static enum ofperr
4258 group_modify(struct ofgroup *group_)
4259 {
4260 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4261
4262 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4263
4264 return 0;
4265 }
4266
4267 static enum ofperr
4268 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4269 {
4270 struct group_dpif *group = group_dpif_cast(group_);
4271 struct ofputil_bucket *bucket;
4272 const struct ovs_list *buckets;
4273 struct bucket_counter *bucket_stats;
4274
4275 ovs_mutex_lock(&group->stats_mutex);
4276 ogs->packet_count = group->packet_count;
4277 ogs->byte_count = group->byte_count;
4278
4279 group_dpif_get_buckets(group, &buckets);
4280 bucket_stats = ogs->bucket_stats;
4281 LIST_FOR_EACH (bucket, list_node, buckets) {
4282 bucket_stats->packet_count = bucket->stats.packet_count;
4283 bucket_stats->byte_count = bucket->stats.byte_count;
4284 bucket_stats++;
4285 }
4286 ovs_mutex_unlock(&group->stats_mutex);
4287
4288 return 0;
4289 }
4290
4291 /* If the group exists, this function increments the groups's reference count.
4292 *
4293 * Make sure to call group_dpif_unref() after no longer needing to maintain
4294 * a reference to the group. */
4295 bool
4296 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4297 struct group_dpif **group)
4298 {
4299 struct ofgroup *ofgroup;
4300 bool found;
4301
4302 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4303 *group = found ? group_dpif_cast(ofgroup) : NULL;
4304
4305 return found;
4306 }
4307
4308 void
4309 group_dpif_get_buckets(const struct group_dpif *group,
4310 const struct ovs_list **buckets)
4311 {
4312 *buckets = &group->up.buckets;
4313 }
4314
4315 enum ofp11_group_type
4316 group_dpif_get_type(const struct group_dpif *group)
4317 {
4318 return group->up.type;
4319 }
4320
4321 const char *
4322 group_dpif_get_selection_method(const struct group_dpif *group)
4323 {
4324 return group->up.props.selection_method;
4325 }
4326 \f
4327 /* Sends 'packet' out 'ofport'.
4328 * May modify 'packet'.
4329 * Returns 0 if successful, otherwise a positive errno value. */
4330 int
4331 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4332 {
4333 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4334 int error;
4335
4336 error = xlate_send_packet(ofport, packet);
4337
4338 ovs_mutex_lock(&ofproto->stats_mutex);
4339 ofproto->stats.tx_packets++;
4340 ofproto->stats.tx_bytes += dp_packet_size(packet);
4341 ovs_mutex_unlock(&ofproto->stats_mutex);
4342 return error;
4343 }
4344
4345 uint64_t
4346 group_dpif_get_selection_method_param(const struct group_dpif *group)
4347 {
4348 return group->up.props.selection_method_param;
4349 }
4350
4351 const struct field_array *
4352 group_dpif_get_fields(const struct group_dpif *group)
4353 {
4354 return &group->up.props.fields;
4355 }
4356 \f
4357 /* Return the version string of the datapath that backs up
4358 * this 'ofproto'.
4359 */
4360 static const char *
4361 get_datapath_version(const struct ofproto *ofproto_)
4362 {
4363 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4364
4365 return ofproto->backer->dp_version_string;
4366 }
4367
4368 static bool
4369 set_frag_handling(struct ofproto *ofproto_,
4370 enum ofputil_frag_handling frag_handling)
4371 {
4372 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4373 if (frag_handling != OFPUTIL_FRAG_REASM) {
4374 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4375 return true;
4376 } else {
4377 return false;
4378 }
4379 }
4380
4381 static enum ofperr
4382 packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
4383 const struct flow *flow,
4384 const struct ofpact *ofpacts, size_t ofpacts_len)
4385 {
4386 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4387
4388 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4389 ofpacts_len, packet);
4390 return 0;
4391 }
4392
4393 static enum ofperr
4394 nxt_resume(struct ofproto *ofproto_,
4395 const struct ofputil_packet_in_private *pin)
4396 {
4397 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4398
4399 /* Translate pin into datapath actions. */
4400 uint64_t odp_actions_stub[1024 / 8];
4401 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
4402 enum slow_path_reason slow;
4403 enum ofperr error = xlate_resume(ofproto, pin, &odp_actions, &slow);
4404
4405 /* Steal 'pin->packet' and put it into a dp_packet. */
4406 struct dp_packet packet;
4407 dp_packet_init(&packet, pin->public.packet_len);
4408 dp_packet_put(&packet, pin->public.packet, pin->public.packet_len);
4409
4410 pkt_metadata_from_flow(&packet.md, &pin->public.flow_metadata.flow);
4411
4412 /* Fix up in_port. */
4413 ofproto_dpif_set_packet_odp_port(ofproto,
4414 pin->public.flow_metadata.flow.in_port.ofp_port,
4415 &packet);
4416
4417 struct flow headers;
4418 flow_extract(&packet, &headers);
4419
4420 /* Execute the datapath actions on the packet. */
4421 struct dpif_execute execute = {
4422 .actions = odp_actions.data,
4423 .actions_len = odp_actions.size,
4424 .needs_help = (slow & SLOW_ACTION) != 0,
4425 .packet = &packet,
4426 .flow = &headers,
4427 };
4428 dpif_execute(ofproto->backer->dpif, &execute);
4429
4430 /* Clean up. */
4431 ofpbuf_uninit(&odp_actions);
4432 dp_packet_uninit(&packet);
4433
4434 return error;
4435 }
4436 \f
4437 /* NetFlow. */
4438
4439 static int
4440 set_netflow(struct ofproto *ofproto_,
4441 const struct netflow_options *netflow_options)
4442 {
4443 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4444
4445 if (netflow_options) {
4446 if (!ofproto->netflow) {
4447 ofproto->netflow = netflow_create();
4448 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4449 }
4450 return netflow_set_options(ofproto->netflow, netflow_options);
4451 } else if (ofproto->netflow) {
4452 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4453 netflow_unref(ofproto->netflow);
4454 ofproto->netflow = NULL;
4455 }
4456
4457 return 0;
4458 }
4459
4460 static void
4461 get_netflow_ids(const struct ofproto *ofproto_,
4462 uint8_t *engine_type, uint8_t *engine_id)
4463 {
4464 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4465
4466 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4467 }
4468 \f
4469 static struct ofproto_dpif *
4470 ofproto_dpif_lookup(const char *name)
4471 {
4472 struct ofproto_dpif *ofproto;
4473
4474 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4475 hash_string(name, 0), &all_ofproto_dpifs) {
4476 if (!strcmp(ofproto->up.name, name)) {
4477 return ofproto;
4478 }
4479 }
4480 return NULL;
4481 }
4482
4483 static void
4484 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4485 const char *argv[], void *aux OVS_UNUSED)
4486 {
4487 struct ofproto_dpif *ofproto;
4488
4489 if (argc > 1) {
4490 ofproto = ofproto_dpif_lookup(argv[1]);
4491 if (!ofproto) {
4492 unixctl_command_reply_error(conn, "no such bridge");
4493 return;
4494 }
4495 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4496 mac_learning_flush(ofproto->ml);
4497 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4498 } else {
4499 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4500 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4501 mac_learning_flush(ofproto->ml);
4502 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4503 }
4504 }
4505
4506 unixctl_command_reply(conn, "table successfully flushed");
4507 }
4508
4509 static void
4510 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4511 const char *argv[], void *aux OVS_UNUSED)
4512 {
4513 struct ofproto_dpif *ofproto;
4514
4515 if (argc > 1) {
4516 ofproto = ofproto_dpif_lookup(argv[1]);
4517 if (!ofproto) {
4518 unixctl_command_reply_error(conn, "no such bridge");
4519 return;
4520 }
4521
4522 if (!mcast_snooping_enabled(ofproto->ms)) {
4523 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4524 return;
4525 }
4526 mcast_snooping_mdb_flush(ofproto->ms);
4527 } else {
4528 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4529 if (!mcast_snooping_enabled(ofproto->ms)) {
4530 continue;
4531 }
4532 mcast_snooping_mdb_flush(ofproto->ms);
4533 }
4534 }
4535
4536 unixctl_command_reply(conn, "table successfully flushed");
4537 }
4538
4539 static struct ofport_dpif *
4540 ofbundle_get_a_port(const struct ofbundle *bundle)
4541 {
4542 return CONTAINER_OF(ovs_list_front(&bundle->ports), struct ofport_dpif,
4543 bundle_node);
4544 }
4545
4546 static void
4547 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4548 const char *argv[], void *aux OVS_UNUSED)
4549 {
4550 struct ds ds = DS_EMPTY_INITIALIZER;
4551 const struct ofproto_dpif *ofproto;
4552 const struct mac_entry *e;
4553
4554 ofproto = ofproto_dpif_lookup(argv[1]);
4555 if (!ofproto) {
4556 unixctl_command_reply_error(conn, "no such bridge");
4557 return;
4558 }
4559
4560 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4561 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4562 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4563 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
4564 char name[OFP_MAX_PORT_NAME_LEN];
4565
4566 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4567 name, sizeof name);
4568 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
4569 name, e->vlan, ETH_ADDR_ARGS(e->mac),
4570 mac_entry_age(ofproto->ml, e));
4571 }
4572 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4573 unixctl_command_reply(conn, ds_cstr(&ds));
4574 ds_destroy(&ds);
4575 }
4576
4577 static void
4578 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4579 int argc OVS_UNUSED,
4580 const char *argv[],
4581 void *aux OVS_UNUSED)
4582 {
4583 struct ds ds = DS_EMPTY_INITIALIZER;
4584 const struct ofproto_dpif *ofproto;
4585 const struct ofbundle *bundle;
4586 const struct mcast_group *grp;
4587 struct mcast_group_bundle *b;
4588 struct mcast_mrouter_bundle *mrouter;
4589
4590 ofproto = ofproto_dpif_lookup(argv[1]);
4591 if (!ofproto) {
4592 unixctl_command_reply_error(conn, "no such bridge");
4593 return;
4594 }
4595
4596 if (!mcast_snooping_enabled(ofproto->ms)) {
4597 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4598 return;
4599 }
4600
4601 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
4602 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4603 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4604 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4605 char name[OFP_MAX_PORT_NAME_LEN];
4606
4607 bundle = b->port;
4608 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4609 name, sizeof name);
4610 ds_put_format(&ds, "%5s %4d ", name, grp->vlan);
4611 ipv6_format_mapped(&grp->addr, &ds);
4612 ds_put_format(&ds, " %3d\n",
4613 mcast_bundle_age(ofproto->ms, b));
4614 }
4615 }
4616
4617 /* ports connected to multicast routers */
4618 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4619 char name[OFP_MAX_PORT_NAME_LEN];
4620
4621 bundle = mrouter->port;
4622 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4623 name, sizeof name);
4624 ds_put_format(&ds, "%5s %4d querier %3d\n",
4625 name, mrouter->vlan,
4626 mcast_mrouter_age(ofproto->ms, mrouter));
4627 }
4628 ovs_rwlock_unlock(&ofproto->ms->rwlock);
4629 unixctl_command_reply(conn, ds_cstr(&ds));
4630 ds_destroy(&ds);
4631 }
4632
4633 struct trace_ctx {
4634 struct xlate_out xout;
4635 struct xlate_in xin;
4636 const struct flow *key;
4637 struct flow flow;
4638 struct ds *result;
4639 struct flow_wildcards wc;
4640 struct ofpbuf odp_actions;
4641 };
4642
4643 static void
4644 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4645 {
4646 const struct rule_actions *actions;
4647 ovs_be64 cookie;
4648
4649 ds_put_char_multiple(result, '\t', level);
4650 if (!rule) {
4651 ds_put_cstr(result, "No match\n");
4652 return;
4653 }
4654
4655 ovs_mutex_lock(&rule->up.mutex);
4656 cookie = rule->up.flow_cookie;
4657 ovs_mutex_unlock(&rule->up.mutex);
4658
4659 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4660 rule ? rule->up.table_id : 0, ntohll(cookie));
4661 cls_rule_format(&rule->up.cr, result);
4662 ds_put_char(result, '\n');
4663
4664 actions = rule_dpif_get_actions(rule);
4665
4666 ds_put_char_multiple(result, '\t', level);
4667 ds_put_cstr(result, "OpenFlow actions=");
4668 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4669 ds_put_char(result, '\n');
4670 }
4671
4672 static void
4673 trace_format_flow(struct ds *result, int level, const char *title,
4674 struct trace_ctx *trace)
4675 {
4676 ds_put_char_multiple(result, '\t', level);
4677 ds_put_format(result, "%s: ", title);
4678 /* Do not report unchanged flows for resubmits. */
4679 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4680 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4681 ds_put_cstr(result, "unchanged");
4682 } else {
4683 flow_format(result, &trace->xin.flow);
4684 trace->flow = trace->xin.flow;
4685 }
4686 ds_put_char(result, '\n');
4687 }
4688
4689 static void
4690 trace_format_regs(struct ds *result, int level, const char *title,
4691 struct trace_ctx *trace)
4692 {
4693 size_t i;
4694
4695 ds_put_char_multiple(result, '\t', level);
4696 ds_put_format(result, "%s:", title);
4697 for (i = 0; i < FLOW_N_REGS; i++) {
4698 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4699 }
4700 ds_put_char(result, '\n');
4701 }
4702
4703 static void
4704 trace_format_odp(struct ds *result, int level, const char *title,
4705 struct trace_ctx *trace)
4706 {
4707 struct ofpbuf *odp_actions = &trace->odp_actions;
4708
4709 ds_put_char_multiple(result, '\t', level);
4710 ds_put_format(result, "%s: ", title);
4711 format_odp_actions(result, odp_actions->data, odp_actions->size);
4712 ds_put_char(result, '\n');
4713 }
4714
4715 static void
4716 trace_format_megaflow(struct ds *result, int level, const char *title,
4717 struct trace_ctx *trace)
4718 {
4719 struct match match;
4720
4721 ds_put_char_multiple(result, '\t', level);
4722 ds_put_format(result, "%s: ", title);
4723 match_init(&match, trace->key, &trace->wc);
4724 match_format(&match, result, OFP_DEFAULT_PRIORITY);
4725 ds_put_char(result, '\n');
4726 }
4727
4728 static void trace_report(struct xlate_in *, int indentation,
4729 const char *format, ...)
4730 OVS_PRINTF_FORMAT(3, 4);
4731 static void trace_report_valist(struct xlate_in *, int indentation,
4732 const char *format, va_list args)
4733 OVS_PRINTF_FORMAT(3, 0);
4734
4735 static void
4736 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int indentation)
4737 {
4738 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4739 struct ds *result = trace->result;
4740
4741 if (!indentation) {
4742 if (rule == xin->ofproto->miss_rule) {
4743 trace_report(xin, indentation,
4744 "No match, flow generates \"packet in\"s.");
4745 } else if (rule == xin->ofproto->no_packet_in_rule) {
4746 trace_report(xin, indentation, "No match, packets dropped because "
4747 "OFPPC_NO_PACKET_IN is set on in_port.");
4748 } else if (rule == xin->ofproto->drop_frags_rule) {
4749 trace_report(xin, indentation,
4750 "Packets dropped because they are IP fragments and "
4751 "the fragment handling mode is \"drop\".");
4752 }
4753 }
4754
4755 ds_put_char(result, '\n');
4756 if (indentation) {
4757 trace_format_flow(result, indentation, "Resubmitted flow", trace);
4758 trace_format_regs(result, indentation, "Resubmitted regs", trace);
4759 trace_format_odp(result, indentation, "Resubmitted odp", trace);
4760 trace_format_megaflow(result, indentation, "Resubmitted megaflow",
4761 trace);
4762 }
4763 trace_format_rule(result, indentation, rule);
4764 }
4765
4766 static void
4767 trace_report_valist(struct xlate_in *xin, int indentation,
4768 const char *format, va_list args)
4769 {
4770 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4771 struct ds *result = trace->result;
4772
4773 ds_put_char_multiple(result, '\t', indentation);
4774 ds_put_format_valist(result, format, args);
4775 ds_put_char(result, '\n');
4776 }
4777
4778 static void
4779 trace_report(struct xlate_in *xin, int indentation, const char *format, ...)
4780 {
4781 va_list args;
4782
4783 va_start(args, format);
4784 trace_report_valist(xin, indentation, format, args);
4785 va_end(args);
4786 }
4787
4788 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4789 * forms are supported:
4790 *
4791 * - [dpname] odp_flow [-generate | packet]
4792 * - bridge br_flow [-generate | packet]
4793 *
4794 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4795 * returns a nonnull malloced error message. */
4796 static char * OVS_WARN_UNUSED_RESULT
4797 parse_flow_and_packet(int argc, const char *argv[],
4798 struct ofproto_dpif **ofprotop, struct flow *flow,
4799 struct dp_packet **packetp)
4800 {
4801 const struct dpif_backer *backer = NULL;
4802 const char *error = NULL;
4803 char *m_err = NULL;
4804 struct simap port_names = SIMAP_INITIALIZER(&port_names);
4805 struct dp_packet *packet;
4806 struct ofpbuf odp_key;
4807 struct ofpbuf odp_mask;
4808
4809 ofpbuf_init(&odp_key, 0);
4810 ofpbuf_init(&odp_mask, 0);
4811
4812 /* Handle "-generate" or a hex string as the last argument. */
4813 if (!strcmp(argv[argc - 1], "-generate")) {
4814 packet = dp_packet_new(0);
4815 argc--;
4816 } else {
4817 error = eth_from_hex(argv[argc - 1], &packet);
4818 if (!error) {
4819 argc--;
4820 } else if (argc == 4) {
4821 /* The 3-argument form must end in "-generate' or a hex string. */
4822 goto exit;
4823 }
4824 error = NULL;
4825 }
4826
4827 /* odp_flow can have its in_port specified as a name instead of port no.
4828 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4829 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4830 * we need to create a simap of name to port no. */
4831 if (argc == 3) {
4832 const char *dp_type;
4833 if (!strncmp(argv[1], "ovs-", 4)) {
4834 dp_type = argv[1] + 4;
4835 } else {
4836 dp_type = argv[1];
4837 }
4838 backer = shash_find_data(&all_dpif_backers, dp_type);
4839 } else if (argc == 2) {
4840 struct shash_node *node;
4841 if (shash_count(&all_dpif_backers) == 1) {
4842 node = shash_first(&all_dpif_backers);
4843 backer = node->data;
4844 }
4845 } else {
4846 error = "Syntax error";
4847 goto exit;
4848 }
4849 if (backer && backer->dpif) {
4850 struct dpif_port dpif_port;
4851 struct dpif_port_dump port_dump;
4852 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4853 simap_put(&port_names, dpif_port.name,
4854 odp_to_u32(dpif_port.port_no));
4855 }
4856 }
4857
4858 /* Parse the flow and determine whether a datapath or
4859 * bridge is specified. If function odp_flow_key_from_string()
4860 * returns 0, the flow is a odp_flow. If function
4861 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4862 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4863 &odp_key, &odp_mask)) {
4864 if (!backer) {
4865 error = "Cannot find the datapath";
4866 goto exit;
4867 }
4868
4869 if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
4870 error = "Failed to parse datapath flow key";
4871 goto exit;
4872 }
4873
4874 *ofprotop = xlate_lookup_ofproto(backer, flow,
4875 &flow->in_port.ofp_port);
4876 if (*ofprotop == NULL) {
4877 error = "Invalid datapath flow";
4878 goto exit;
4879 }
4880 } else {
4881 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4882
4883 if (err) {
4884 m_err = xasprintf("Bad openflow flow syntax: %s", err);
4885 free(err);
4886 goto exit;
4887 } else {
4888 if (argc != 3) {
4889 error = "Must specify bridge name";
4890 goto exit;
4891 }
4892
4893 *ofprotop = ofproto_dpif_lookup(argv[1]);
4894 if (!*ofprotop) {
4895 error = "Unknown bridge name";
4896 goto exit;
4897 }
4898 }
4899 }
4900
4901 /* Generate a packet, if requested. */
4902 if (packet) {
4903 if (!dp_packet_size(packet)) {
4904 flow_compose(packet, flow);
4905 } else {
4906 /* Use the metadata from the flow and the packet argument
4907 * to reconstruct the flow. */
4908 pkt_metadata_from_flow(&packet->md, flow);
4909 flow_extract(packet, flow);
4910 }
4911 }
4912
4913 exit:
4914 if (error && !m_err) {
4915 m_err = xstrdup(error);
4916 }
4917 if (m_err) {
4918 dp_packet_delete(packet);
4919 packet = NULL;
4920 }
4921 *packetp = packet;
4922 ofpbuf_uninit(&odp_key);
4923 ofpbuf_uninit(&odp_mask);
4924 simap_destroy(&port_names);
4925 return m_err;
4926 }
4927
4928 static void
4929 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4930 void *aux OVS_UNUSED)
4931 {
4932 struct ofproto_dpif *ofproto;
4933 struct dp_packet *packet;
4934 char *error;
4935 struct flow flow;
4936
4937 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4938 if (!error) {
4939 struct ds result;
4940
4941 ds_init(&result);
4942 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4943 unixctl_command_reply(conn, ds_cstr(&result));
4944 ds_destroy(&result);
4945 dp_packet_delete(packet);
4946 } else {
4947 unixctl_command_reply_error(conn, error);
4948 free(error);
4949 }
4950 }
4951
4952 static void
4953 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4954 const char *argv[], void *aux OVS_UNUSED)
4955 {
4956 enum ofputil_protocol usable_protocols;
4957 struct ofproto_dpif *ofproto;
4958 bool enforce_consistency;
4959 struct ofpbuf ofpacts;
4960 struct dp_packet *packet;
4961 struct ds result;
4962 struct flow flow;
4963 uint16_t in_port;
4964
4965 /* Three kinds of error return values! */
4966 enum ofperr retval;
4967 char *error;
4968
4969 packet = NULL;
4970 ds_init(&result);
4971 ofpbuf_init(&ofpacts, 0);
4972
4973 /* Parse actions. */
4974 error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
4975 if (error) {
4976 unixctl_command_reply_error(conn, error);
4977 free(error);
4978 goto exit;
4979 }
4980
4981 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4982 * consistency between the flow and the actions. With -consistent, we
4983 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4984 if (!strcmp(argv[1], "-consistent")) {
4985 enforce_consistency = true;
4986 argv++;
4987 argc--;
4988 } else {
4989 enforce_consistency = false;
4990 }
4991
4992 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4993 if (error) {
4994 unixctl_command_reply_error(conn, error);
4995 free(error);
4996 goto exit;
4997 }
4998
4999 /* Do the same checks as handle_packet_out() in ofproto.c.
5000 *
5001 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
5002 * strictly correct because these actions aren't in any table, but it's OK
5003 * because it 'table_id' is used only to check goto_table instructions, but
5004 * packet-outs take a list of actions and therefore it can't include
5005 * instructions.
5006 *
5007 * We skip the "meter" check here because meter is an instruction, not an
5008 * action, and thus cannot appear in ofpacts. */
5009 in_port = ofp_to_u16(flow.in_port.ofp_port);
5010 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
5011 unixctl_command_reply_error(conn, "invalid in_port");
5012 goto exit;
5013 }
5014 if (enforce_consistency) {
5015 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
5016 u16_to_ofp(ofproto->up.max_ports),
5017 0, ofproto->up.n_tables,
5018 usable_protocols);
5019 } else {
5020 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
5021 u16_to_ofp(ofproto->up.max_ports), 0,
5022 ofproto->up.n_tables, &usable_protocols);
5023 }
5024 if (!retval) {
5025 retval = ofproto_check_ofpacts(&ofproto->up, ofpacts.data,
5026 ofpacts.size);
5027 }
5028
5029 if (retval) {
5030 ds_clear(&result);
5031 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
5032 unixctl_command_reply_error(conn, ds_cstr(&result));
5033 goto exit;
5034 }
5035
5036 ofproto_trace(ofproto, &flow, packet,
5037 ofpacts.data, ofpacts.size, &result);
5038 unixctl_command_reply(conn, ds_cstr(&result));
5039
5040 exit:
5041 ds_destroy(&result);
5042 dp_packet_delete(packet);
5043 ofpbuf_uninit(&ofpacts);
5044 }
5045
5046 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
5047 * description of the results to 'ds'.
5048 *
5049 * The trace follows a packet with the specified 'flow' through the flow
5050 * table. 'packet' may be nonnull to trace an actual packet, with consequent
5051 * side effects (if it is nonnull then its flow must be 'flow').
5052 *
5053 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
5054 * trace, otherwise the actions are determined by a flow table lookup. */
5055 static void
5056 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
5057 const struct dp_packet *packet,
5058 const struct ofpact ofpacts[], size_t ofpacts_len,
5059 struct ds *ds)
5060 {
5061 struct trace_ctx trace;
5062 enum xlate_error error;
5063
5064 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
5065 ds_put_cstr(ds, "Flow: ");
5066 flow_format(ds, flow);
5067 ds_put_char(ds, '\n');
5068
5069 ofpbuf_init(&trace.odp_actions, 0);
5070
5071 trace.result = ds;
5072 trace.key = flow; /* Original flow key, used for megaflow. */
5073 trace.flow = *flow; /* May be modified by actions. */
5074 xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
5075 ntohs(flow->tcp_flags), packet, &trace.wc,
5076 &trace.odp_actions);
5077 trace.xin.ofpacts = ofpacts;
5078 trace.xin.ofpacts_len = ofpacts_len;
5079 trace.xin.resubmit_hook = trace_resubmit;
5080 trace.xin.report_hook = trace_report_valist;
5081
5082 error = xlate_actions(&trace.xin, &trace.xout);
5083 ds_put_char(ds, '\n');
5084 trace.xin.flow.actset_output = 0;
5085 trace_format_flow(ds, 0, "Final flow", &trace);
5086 trace_format_megaflow(ds, 0, "Megaflow", &trace);
5087
5088 ds_put_cstr(ds, "Datapath actions: ");
5089 format_odp_actions(ds, trace.odp_actions.data, trace.odp_actions.size);
5090
5091 if (error != XLATE_OK) {
5092 ds_put_format(ds, "\nTranslation failed (%s), packet is dropped.\n",
5093 xlate_strerror(error));
5094 } else if (trace.xout.slow) {
5095 enum slow_path_reason slow;
5096
5097 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
5098 "slow path because it:");
5099
5100 slow = trace.xout.slow;
5101 while (slow) {
5102 enum slow_path_reason bit = rightmost_1bit(slow);
5103
5104 ds_put_format(ds, "\n\t- %s.",
5105 slow_path_reason_to_explanation(bit));
5106
5107 slow &= ~bit;
5108 }
5109 }
5110
5111 xlate_out_uninit(&trace.xout);
5112 ofpbuf_uninit(&trace.odp_actions);
5113 }
5114
5115 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5116 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5117 * to destroy 'ofproto_shash' and free the returned value. */
5118 static const struct shash_node **
5119 get_ofprotos(struct shash *ofproto_shash)
5120 {
5121 const struct ofproto_dpif *ofproto;
5122
5123 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5124 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5125 shash_add_nocopy(ofproto_shash, name, ofproto);
5126 }
5127
5128 return shash_sort(ofproto_shash);
5129 }
5130
5131 static void
5132 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5133 const char *argv[] OVS_UNUSED,
5134 void *aux OVS_UNUSED)
5135 {
5136 struct ds ds = DS_EMPTY_INITIALIZER;
5137 struct shash ofproto_shash;
5138 const struct shash_node **sorted_ofprotos;
5139 int i;
5140
5141 shash_init(&ofproto_shash);
5142 sorted_ofprotos = get_ofprotos(&ofproto_shash);
5143 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5144 const struct shash_node *node = sorted_ofprotos[i];
5145 ds_put_format(&ds, "%s\n", node->name);
5146 }
5147
5148 shash_destroy(&ofproto_shash);
5149 free(sorted_ofprotos);
5150
5151 unixctl_command_reply(conn, ds_cstr(&ds));
5152 ds_destroy(&ds);
5153 }
5154
5155 static void
5156 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5157 {
5158 const struct shash_node **ofprotos;
5159 struct dpif_dp_stats dp_stats;
5160 struct shash ofproto_shash;
5161 size_t i;
5162
5163 dpif_get_dp_stats(backer->dpif, &dp_stats);
5164
5165 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5166 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
5167
5168 shash_init(&ofproto_shash);
5169 ofprotos = get_ofprotos(&ofproto_shash);
5170 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5171 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5172 const struct shash_node **ports;
5173 size_t j;
5174
5175 if (ofproto->backer != backer) {
5176 continue;
5177 }
5178
5179 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
5180
5181 ports = shash_sort(&ofproto->up.port_by_name);
5182 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5183 const struct shash_node *node = ports[j];
5184 struct ofport *ofport = node->data;
5185 struct smap config;
5186 odp_port_t odp_port;
5187
5188 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5189 ofport->ofp_port);
5190
5191 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5192 if (odp_port != ODPP_NONE) {
5193 ds_put_format(ds, "%"PRIu32":", odp_port);
5194 } else {
5195 ds_put_cstr(ds, "none:");
5196 }
5197
5198 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5199
5200 smap_init(&config);
5201 if (!netdev_get_config(ofport->netdev, &config)) {
5202 const struct smap_node **nodes;
5203 size_t i;
5204
5205 nodes = smap_sort(&config);
5206 for (i = 0; i < smap_count(&config); i++) {
5207 const struct smap_node *node = nodes[i];
5208 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5209 node->key, node->value);
5210 }
5211 free(nodes);
5212 }
5213 smap_destroy(&config);
5214
5215 ds_put_char(ds, ')');
5216 ds_put_char(ds, '\n');
5217 }
5218 free(ports);
5219 }
5220 shash_destroy(&ofproto_shash);
5221 free(ofprotos);
5222 }
5223
5224 static void
5225 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5226 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5227 {
5228 struct ds ds = DS_EMPTY_INITIALIZER;
5229 const struct shash_node **backers;
5230 int i;
5231
5232 backers = shash_sort(&all_dpif_backers);
5233 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5234 dpif_show_backer(backers[i]->data, &ds);
5235 }
5236 free(backers);
5237
5238 unixctl_command_reply(conn, ds_cstr(&ds));
5239 ds_destroy(&ds);
5240 }
5241
5242 static void
5243 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5244 int argc OVS_UNUSED, const char *argv[],
5245 void *aux OVS_UNUSED)
5246 {
5247 const struct ofproto_dpif *ofproto;
5248
5249 struct ds ds = DS_EMPTY_INITIALIZER;
5250 bool verbosity = false;
5251
5252 struct dpif_port dpif_port;
5253 struct dpif_port_dump port_dump;
5254 struct hmap portno_names;
5255
5256 struct dpif_flow_dump *flow_dump;
5257 struct dpif_flow_dump_thread *flow_dump_thread;
5258 struct dpif_flow f;
5259 int error;
5260
5261 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5262 if (!ofproto) {
5263 unixctl_command_reply_error(conn, "no such bridge");
5264 return;
5265 }
5266
5267 if (argc > 2 && !strcmp(argv[1], "-m")) {
5268 verbosity = true;
5269 }
5270
5271 hmap_init(&portno_names);
5272 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5273 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5274 }
5275
5276 ds_init(&ds);
5277 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5278 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5279 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5280 struct flow flow;
5281
5282 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5283 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5284 continue;
5285 }
5286
5287 if (verbosity) {
5288 odp_format_ufid(&f.ufid, &ds);
5289 ds_put_cstr(&ds, " ");
5290 }
5291 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5292 &portno_names, &ds, verbosity);
5293 ds_put_cstr(&ds, ", ");
5294 dpif_flow_stats_format(&f.stats, &ds);
5295 ds_put_cstr(&ds, ", actions:");
5296 format_odp_actions(&ds, f.actions, f.actions_len);
5297 ds_put_char(&ds, '\n');
5298 }
5299 dpif_flow_dump_thread_destroy(flow_dump_thread);
5300 error = dpif_flow_dump_destroy(flow_dump);
5301
5302 if (error) {
5303 ds_clear(&ds);
5304 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5305 unixctl_command_reply_error(conn, ds_cstr(&ds));
5306 } else {
5307 unixctl_command_reply(conn, ds_cstr(&ds));
5308 }
5309 odp_portno_names_destroy(&portno_names);
5310 hmap_destroy(&portno_names);
5311 ds_destroy(&ds);
5312 }
5313
5314 static void
5315 ofproto_revalidate_all_backers(void)
5316 {
5317 const struct shash_node **backers;
5318 int i;
5319
5320 backers = shash_sort(&all_dpif_backers);
5321 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5322 struct dpif_backer *backer = backers[i]->data;
5323 backer->need_revalidate = REV_RECONFIGURE;
5324 }
5325 free(backers);
5326 }
5327
5328 static void
5329 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5330 const char *argv[], void *aux OVS_UNUSED)
5331 {
5332 if (!strcasecmp(argv[1], "off")) {
5333 ofproto_use_tnl_push_pop = false;
5334 unixctl_command_reply(conn, "Tunnel push-pop off");
5335 ofproto_revalidate_all_backers();
5336 } else if (!strcasecmp(argv[1], "on")) {
5337 ofproto_use_tnl_push_pop = true;
5338 unixctl_command_reply(conn, "Tunnel push-pop on");
5339 ofproto_revalidate_all_backers();
5340 } else {
5341 unixctl_command_reply_error(conn, "Invalid argument");
5342 }
5343 }
5344
5345 static void
5346 ofproto_unixctl_init(void)
5347 {
5348 static bool registered;
5349 if (registered) {
5350 return;
5351 }
5352 registered = true;
5353
5354 unixctl_command_register(
5355 "ofproto/trace",
5356 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5357 1, 3, ofproto_unixctl_trace, NULL);
5358 unixctl_command_register(
5359 "ofproto/trace-packet-out",
5360 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5361 2, 6, ofproto_unixctl_trace_actions, NULL);
5362 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5363 ofproto_unixctl_fdb_flush, NULL);
5364 unixctl_command_register("fdb/show", "bridge", 1, 1,
5365 ofproto_unixctl_fdb_show, NULL);
5366 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5367 ofproto_unixctl_mcast_snooping_flush, NULL);
5368 unixctl_command_register("mdb/show", "bridge", 1, 1,
5369 ofproto_unixctl_mcast_snooping_show, NULL);
5370 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5371 ofproto_unixctl_dpif_dump_dps, NULL);
5372 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5373 NULL);
5374 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5375 ofproto_unixctl_dpif_dump_flows, NULL);
5376
5377 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5378 disable_tnl_push_pop, NULL);
5379 }
5380
5381 /* Returns true if 'table' is the table used for internal rules,
5382 * false otherwise. */
5383 bool
5384 table_is_internal(uint8_t table_id)
5385 {
5386 return table_id == TBL_INTERNAL;
5387 }
5388 \f
5389
5390 static odp_port_t
5391 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5392 {
5393 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
5394 return ofport ? ofport->odp_port : ODPP_NONE;
5395 }
5396
5397 struct ofport_dpif *
5398 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5399 {
5400 struct ofport_dpif *port;
5401
5402 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5403 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5404 &backer->odp_to_ofport_map) {
5405 if (port->odp_port == odp_port) {
5406 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5407 return port;
5408 }
5409 }
5410
5411 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5412 return NULL;
5413 }
5414
5415 static ofp_port_t
5416 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5417 {
5418 struct ofport_dpif *port;
5419
5420 port = odp_port_to_ofport(ofproto->backer, odp_port);
5421 if (port && &ofproto->up == port->up.ofproto) {
5422 return port->up.ofp_port;
5423 } else {
5424 return OFPP_NONE;
5425 }
5426 }
5427
5428 int
5429 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5430 const struct match *match, int priority,
5431 uint16_t idle_timeout,
5432 const struct ofpbuf *ofpacts,
5433 struct rule **rulep)
5434 {
5435 struct ofproto_flow_mod ofm;
5436 struct rule_dpif *rule;
5437 int error;
5438
5439 ofm.fm = (struct ofputil_flow_mod) {
5440 .match = *match,
5441 .priority = priority,
5442 .table_id = TBL_INTERNAL,
5443 .command = OFPFC_ADD,
5444 .idle_timeout = idle_timeout,
5445 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5446 .ofpacts = ofpacts->data,
5447 .ofpacts_len = ofpacts->size,
5448 .delete_reason = OVS_OFPRR_NONE,
5449 };
5450
5451 error = ofproto_flow_mod(&ofproto->up, &ofm);
5452 if (error) {
5453 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5454 ofperr_to_string(error));
5455 *rulep = NULL;
5456 return error;
5457 }
5458
5459 rule = rule_dpif_lookup_in_table(ofproto,
5460 ofproto_dpif_get_tables_version(ofproto),
5461 TBL_INTERNAL, &ofm.fm.match.flow,
5462 &ofm.fm.match.wc);
5463 if (rule) {
5464 *rulep = &rule->up;
5465 } else {
5466 OVS_NOT_REACHED();
5467 }
5468 return 0;
5469 }
5470
5471 int
5472 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5473 struct match *match, int priority)
5474 {
5475 struct ofproto_flow_mod ofm;
5476 int error;
5477
5478 ofm.fm = (struct ofputil_flow_mod) {
5479 .match = *match,
5480 .priority = priority,
5481 .table_id = TBL_INTERNAL,
5482 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5483 .command = OFPFC_DELETE_STRICT,
5484 };
5485
5486 error = ofproto_flow_mod(&ofproto->up, &ofm);
5487 if (error) {
5488 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5489 ofperr_to_string(error));
5490 return error;
5491 }
5492
5493 return 0;
5494 }
5495
5496 const struct uuid *
5497 ofproto_dpif_get_uuid(const struct ofproto_dpif *ofproto)
5498 {
5499 return &ofproto->uuid;
5500 }
5501
5502 const struct ofproto_class ofproto_dpif_class = {
5503 init,
5504 enumerate_types,
5505 enumerate_names,
5506 del,
5507 port_open_type,
5508 type_run,
5509 type_wait,
5510 alloc,
5511 construct,
5512 destruct,
5513 dealloc,
5514 run,
5515 ofproto_dpif_wait,
5516 NULL, /* get_memory_usage. */
5517 type_get_memory_usage,
5518 flush,
5519 query_tables,
5520 set_tables_version,
5521 port_alloc,
5522 port_construct,
5523 port_destruct,
5524 port_dealloc,
5525 port_modified,
5526 port_reconfigured,
5527 port_query_by_name,
5528 port_add,
5529 port_del,
5530 port_get_stats,
5531 port_dump_start,
5532 port_dump_next,
5533 port_dump_done,
5534 port_poll,
5535 port_poll_wait,
5536 port_is_lacp_current,
5537 port_get_lacp_stats,
5538 NULL, /* rule_choose_table */
5539 rule_alloc,
5540 rule_construct,
5541 rule_insert,
5542 NULL, /* rule_delete */
5543 rule_destruct,
5544 rule_dealloc,
5545 rule_get_stats,
5546 rule_execute,
5547 set_frag_handling,
5548 packet_out,
5549 nxt_resume,
5550 set_netflow,
5551 get_netflow_ids,
5552 set_sflow,
5553 set_ipfix,
5554 get_ipfix_stats,
5555 set_cfm,
5556 cfm_status_changed,
5557 get_cfm_status,
5558 set_lldp,
5559 get_lldp_status,
5560 set_aa,
5561 aa_mapping_set,
5562 aa_mapping_unset,
5563 aa_vlan_get_queued,
5564 aa_vlan_get_queue_size,
5565 set_bfd,
5566 bfd_status_changed,
5567 get_bfd_status,
5568 set_stp,
5569 get_stp_status,
5570 set_stp_port,
5571 get_stp_port_status,
5572 get_stp_port_stats,
5573 set_rstp,
5574 get_rstp_status,
5575 set_rstp_port,
5576 get_rstp_port_status,
5577 set_queues,
5578 bundle_set,
5579 bundle_remove,
5580 mirror_set__,
5581 mirror_get_stats__,
5582 set_flood_vlans,
5583 is_mirror_output_bundle,
5584 forward_bpdu_changed,
5585 set_mac_table_config,
5586 set_mcast_snooping,
5587 set_mcast_snooping_port,
5588 NULL, /* meter_get_features */
5589 NULL, /* meter_set */
5590 NULL, /* meter_get */
5591 NULL, /* meter_del */
5592 group_alloc, /* group_alloc */
5593 group_construct, /* group_construct */
5594 group_destruct, /* group_destruct */
5595 group_dealloc, /* group_dealloc */
5596 group_modify, /* group_modify */
5597 group_get_stats, /* group_get_stats */
5598 get_datapath_version, /* get_datapath_version */
5599 };