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