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