]> git.proxmox.com Git - ovs.git/blame - vswitchd/bridge.c
bridge: Separate mirroring logic from forwarding logic.
[ovs.git] / vswitchd / bridge.c
CommitLineData
b0ec0f27 1/* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks
c93b1d6a 2 *
a14bc59f
BP
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
064af421 6 *
a14bc59f 7 * http://www.apache.org/licenses/LICENSE-2.0
064af421 8 *
a14bc59f
BP
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
064af421
BP
14 */
15
16#include <config.h>
17#include "bridge.h"
10a24935 18#include "byte-order.h"
064af421
BP
19#include <assert.h>
20#include <errno.h>
21#include <arpa/inet.h>
22#include <ctype.h>
23#include <inttypes.h>
9d82ec47 24#include <sys/socket.h>
064af421
BP
25#include <net/if.h>
26#include <openflow/openflow.h>
27#include <signal.h>
28#include <stdlib.h>
29#include <strings.h>
30#include <sys/stat.h>
31#include <sys/socket.h>
7e40e21d 32#include <sys/types.h>
064af421
BP
33#include <unistd.h>
34#include "bitmap.h"
f620b43a 35#include "bond.h"
b31bcf60 36#include "cfm.h"
cf3fad8a 37#include "classifier.h"
064af421 38#include "coverage.h"
a7ff9bd7 39#include "daemon.h"
064af421
BP
40#include "dirs.h"
41#include "dpif.h"
42#include "dynamic-string.h"
43#include "flow.h"
44#include "hash.h"
d9a8717a 45#include "hmap.h"
cd11000b 46#include "jsonrpc.h"
6aa74308 47#include "lacp.h"
064af421
BP
48#include "list.h"
49#include "mac-learning.h"
50#include "netdev.h"
cdee00fd 51#include "netlink.h"
064af421
BP
52#include "odp-util.h"
53#include "ofp-print.h"
54#include "ofpbuf.h"
d65349ea 55#include "ofproto/netflow.h"
8cd4882f 56#include "ofproto/ofproto.h"
dd0d105c 57#include "ovsdb-data.h"
da285df4 58#include "packets.h"
064af421 59#include "poll-loop.h"
064af421 60#include "process.h"
76343538 61#include "sha1.h"
6c88d577 62#include "shash.h"
064af421 63#include "socket-util.h"
fe55ad15 64#include "stream-ssl.h"
b3c01ed3 65#include "sset.h"
064af421 66#include "svec.h"
ce887677 67#include "system-stats.h"
064af421
BP
68#include "timeval.h"
69#include "util.h"
da285df4 70#include "unixctl.h"
064af421 71#include "vconn.h"
b36682d8 72#include "vswitchd/vswitch-idl.h"
064af421 73#include "xenserver.h"
5136ce49 74#include "vlog.h"
72b06300 75#include "sflow_api.h"
064af421 76
d98e6007 77VLOG_DEFINE_THIS_MODULE(bridge);
064af421 78
d76f09ea
BP
79COVERAGE_DEFINE(bridge_flush);
80COVERAGE_DEFINE(bridge_process_flow);
81COVERAGE_DEFINE(bridge_reconfigure);
82
064af421 83struct dst {
d55c2566 84 struct iface *iface;
064af421 85 uint16_t vlan;
064af421
BP
86};
87
0c62a7ad
BP
88struct dst_set {
89 struct dst builtin[32];
90 struct dst *dsts;
91 size_t n, allocated;
92};
93
94static void dst_set_init(struct dst_set *);
95static void dst_set_add(struct dst_set *, const struct dst *);
96static void dst_set_free(struct dst_set *);
97
064af421 98struct iface {
0c6aea3f 99 /* These members are always valid. */
83db7968 100 struct list port_elem; /* Element in struct port's "ifaces" list. */
064af421 101 struct port *port; /* Containing port. */
064af421 102 char *name; /* Host network device name. */
064af421 103 tag_type tag; /* Tag associated with this interface. */
064af421 104
0c6aea3f 105 /* These members are valid only after bridge_reconfigure() causes them to
1e0b752d 106 * be initialized. */
d9a8717a 107 struct hmap_node dp_ifidx_node; /* In struct bridge's "ifaces" hmap. */
0c6aea3f
BP
108 int dp_ifidx; /* Index within kernel datapath. */
109 struct netdev *netdev; /* Network device. */
6cefe1da 110 const char *type; /* Usually same as cfg->type. */
76343538 111 const struct ovsrec_interface *cfg;
064af421
BP
112};
113
064af421
BP
114#define MAX_MIRRORS 32
115typedef uint32_t mirror_mask_t;
116#define MIRROR_MASK_C(X) UINT32_C(X)
117BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
118struct mirror {
119 struct bridge *bridge;
120 size_t idx;
121 char *name;
dd0d105c 122 struct uuid uuid; /* UUID of this "mirror" record in database. */
064af421
BP
123
124 /* Selection criteria. */
b3c01ed3
BP
125 struct sset src_ports; /* Source port names. */
126 struct sset dst_ports; /* Destination port names. */
064af421
BP
127 int *vlans;
128 size_t n_vlans;
129
130 /* Output. */
131 struct port *out_port;
132 int out_vlan;
133};
134
135#define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
136struct port {
137 struct bridge *bridge;
8052fb14
BP
138 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
139 char *name;
140
064af421 141 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
3e9c481c
BP
142 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
143 * NULL if all VLANs are trunked. */
1e0b752d 144 const struct ovsrec_port *cfg;
064af421
BP
145
146 /* An ordinary bridge port has 1 interface.
147 * A bridge port for bonding has at least 2 interfaces. */
83db7968 148 struct list ifaces; /* List of "struct iface"s. */
064af421
BP
149
150 /* Bonding info. */
f620b43a 151 struct bond *bond;
064af421
BP
152
153 /* Port mirroring info. */
154 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
155 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
156 bool is_mirror_output_port; /* Does port mirroring send frames here? */
064af421
BP
157};
158
064af421
BP
159struct bridge {
160 struct list node; /* Node in global list of bridges. */
161 char *name; /* User-specified arbitrary name. */
93dfc067 162 struct mac_learning *ml; /* MAC learning table. */
abe457eb 163 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
064af421 164 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
1e0b752d 165 const struct ovsrec_bridge *cfg;
064af421 166
064af421
BP
167 /* OpenFlow switch processing. */
168 struct ofproto *ofproto; /* OpenFlow switch. */
169
170 /* Kernel datapath information. */
c228a364 171 struct dpif *dpif; /* Datapath. */
cbbb244d 172 struct hmap ifaces; /* "struct iface"s indexed by dp_ifidx. */
064af421
BP
173
174 /* Bridge ports. */
8052fb14 175 struct hmap ports; /* "struct port"s indexed by name. */
4a1ee6ae 176 struct shash iface_by_name; /* "struct iface"s indexed by name. */
064af421
BP
177
178 /* Bonding. */
179 bool has_bonded_ports;
064af421
BP
180
181 /* Flow tracking. */
182 bool flush;
183
064af421
BP
184 /* Port mirroring. */
185 struct mirror *mirrors[MAX_MIRRORS];
064af421
BP
186};
187
188/* List of all bridges. */
189static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
190
c5187f17
BP
191/* OVSDB IDL used to obtain configuration. */
192static struct ovsdb_idl *idl;
193
cd0cd65f
BP
194/* Each time this timer expires, the bridge fetches systems and interface
195 * statistics and pushes them into the database. */
196#define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
197static long long int stats_timer = LLONG_MIN;
018f1525 198
6586adf5
EJ
199/* Stores the time after which CFM statistics may be written to the database.
200 * Only updated when changes to the database require rate limiting. */
201#define CFM_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
202static long long int cfm_limiter = LLONG_MIN;
203
1a6f1e2a 204static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
064af421
BP
205static void bridge_destroy(struct bridge *);
206static struct bridge *bridge_lookup(const char *name);
8ca79daa 207static unixctl_cb_func bridge_unixctl_dump_flows;
fa05809b 208static unixctl_cb_func bridge_unixctl_reconnect;
064af421 209static int bridge_run_one(struct bridge *);
1a048029 210static size_t bridge_get_controllers(const struct bridge *br,
76ce9432 211 struct ovsrec_controller ***controllersp);
1a048029
JP
212static void bridge_reconfigure_one(struct bridge *);
213static void bridge_reconfigure_remotes(struct bridge *,
11bbff1b
BP
214 const struct sockaddr_in *managers,
215 size_t n_managers);
76343538 216static void bridge_get_all_ifaces(const struct bridge *, struct shash *ifaces);
064af421
BP
217static void bridge_fetch_dp_ifaces(struct bridge *);
218static void bridge_flush(struct bridge *);
219static void bridge_pick_local_hw_addr(struct bridge *,
220 uint8_t ea[ETH_ADDR_LEN],
07c318f4 221 struct iface **hw_addr_iface);
064af421
BP
222static uint64_t bridge_pick_datapath_id(struct bridge *,
223 const uint8_t bridge_ea[ETH_ADDR_LEN],
07c318f4 224 struct iface *hw_addr_iface);
064af421
BP
225static uint64_t dpid_from_hash(const void *, size_t nbytes);
226
8ca79daa 227static unixctl_cb_func bridge_unixctl_fdb_show;
20c8e971 228static unixctl_cb_func cfm_unixctl_show;
e8fe3026 229static unixctl_cb_func qos_unixctl_show;
8c4c1387 230
21dcb94f
EJ
231static void port_run(struct port *);
232static void port_wait(struct port *);
76343538
BP
233static struct port *port_create(struct bridge *, const char *name);
234static void port_reconfigure(struct port *, const struct ovsrec_port *);
4a1ee6ae 235static void port_del_ifaces(struct port *, const struct ovsrec_port *);
064af421
BP
236static void port_destroy(struct port *);
237static struct port *port_lookup(const struct bridge *, const char *name);
83db7968 238static struct iface *port_get_an_iface(const struct port *);
064af421
BP
239static struct port *port_from_dp_ifidx(const struct bridge *,
240 uint16_t dp_ifidx);
f620b43a
BP
241static void port_reconfigure_bond(struct port *);
242static void port_send_learning_packets(struct port *);
064af421 243
dd0d105c 244static void mirror_create(struct bridge *, struct ovsrec_mirror *);
064af421
BP
245static void mirror_destroy(struct mirror *);
246static void mirror_reconfigure(struct bridge *);
37e7f427 247static void mirror_reconfigure_one(struct mirror *, struct ovsrec_mirror *);
064af421
BP
248static bool vlan_is_mirrored(const struct mirror *, int vlan);
249
d295e8e9 250static struct iface *iface_create(struct port *port,
a740f0de 251 const struct ovsrec_interface *if_cfg);
064af421
BP
252static void iface_destroy(struct iface *);
253static struct iface *iface_lookup(const struct bridge *, const char *name);
e8fe3026 254static struct iface *iface_find(const char *name);
064af421
BP
255static struct iface *iface_from_dp_ifidx(const struct bridge *,
256 uint16_t dp_ifidx);
52df17e7 257static void iface_set_mac(struct iface *);
bcd49a45 258static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
c1c9c9c4 259static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
b31bcf60 260static void iface_update_cfm(struct iface *);
6586adf5 261static bool iface_refresh_cfm_stats(struct iface *iface);
0b8024eb 262static bool iface_get_carrier(const struct iface *);
064af421 263
43776b8f
BP
264static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
265 struct shash *);
ea763e0e
EJ
266static void shash_to_ovs_idl_map(struct shash *,
267 char ***keys, char ***values, size_t *n);
268
064af421
BP
269/* Hooks into ofproto processing. */
270static struct ofhooks bridge_ofhooks;
271\f
272/* Public functions. */
273
c5187f17
BP
274/* Initializes the bridge module, configuring it to obtain its configuration
275 * from an OVSDB server accessed over 'remote', which should be a string in a
276 * form acceptable to ovsdb_idl_create(). */
064af421 277void
c5187f17
BP
278bridge_init(const char *remote)
279{
280 /* Create connection to database. */
ef73f86c 281 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
c5187f17 282
ef73f86c
BP
283 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
284 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
e85bbd75
BP
285 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
286
287 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
288
289 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
290 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
291
ef73f86c
BP
292 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
293 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
e85bbd75
BP
294 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
295
c5187f17
BP
296 /* Register unixctl commands. */
297 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL);
20c8e971 298 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
e8fe3026 299 unixctl_command_register("qos/show", qos_unixctl_show, NULL);
c5187f17
BP
300 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
301 NULL);
fa05809b
BP
302 unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
303 NULL);
c5187f17
BP
304 bond_init();
305}
306
ee45ad81
BP
307void
308bridge_exit(void)
309{
310 struct bridge *br, *next_br;
311
312 LIST_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
313 bridge_destroy(br);
314 }
315 ovsdb_idl_destroy(idl);
316}
317
c5187f17
BP
318/* Performs configuration that is only necessary once at ovs-vswitchd startup,
319 * but for which the ovs-vswitchd configuration 'cfg' is required. */
320static void
321bridge_configure_once(const struct ovsrec_open_vswitch *cfg)
064af421 322{
c5187f17 323 static bool already_configured_once;
9759479a 324 struct sset bridge_names;
d0c23a1a
BP
325 struct sset dpif_names, dpif_types;
326 const char *type;
d3d22744 327 size_t i;
da285df4 328
c5187f17
BP
329 /* Only do this once per ovs-vswitchd run. */
330 if (already_configured_once) {
331 return;
332 }
333 already_configured_once = true;
8c4c1387 334
cd0cd65f 335 stats_timer = time_msec() + STATS_INTERVAL;
018f1525 336
c5187f17 337 /* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
9759479a 338 sset_init(&bridge_names);
76343538 339 for (i = 0; i < cfg->n_bridges; i++) {
9759479a 340 sset_add(&bridge_names, cfg->bridges[i]->name);
76343538 341 }
76343538 342
c5187f17
BP
343 /* Iterate over all system dpifs and delete any of them that do not appear
344 * in 'cfg'. */
d0c23a1a
BP
345 sset_init(&dpif_names);
346 sset_init(&dpif_types);
1a6f1e2a 347 dp_enumerate_types(&dpif_types);
d0c23a1a
BP
348 SSET_FOR_EACH (type, &dpif_types) {
349 const char *name;
064af421 350
d0c23a1a 351 dp_enumerate_names(type, &dpif_names);
d3d22744 352
3d8c9535 353 /* Delete each dpif whose name is not in 'bridge_names'. */
d0c23a1a 354 SSET_FOR_EACH (name, &dpif_names) {
9759479a 355 if (!sset_contains(&bridge_names, name)) {
3d8c9535
BP
356 struct dpif *dpif;
357 int retval;
358
d0c23a1a 359 retval = dpif_open(name, type, &dpif);
3d8c9535
BP
360 if (!retval) {
361 dpif_delete(dpif);
362 dpif_close(dpif);
d3d22744 363 }
064af421 364 }
064af421
BP
365 }
366 }
9759479a 367 sset_destroy(&bridge_names);
d0c23a1a
BP
368 sset_destroy(&dpif_names);
369 sset_destroy(&dpif_types);
064af421
BP
370}
371
64d64dd7 372/* Callback for iterate_and_prune_ifaces(). */
0c6aea3f 373static bool
64d64dd7 374check_iface(struct bridge *br, struct iface *iface, void *aux OVS_UNUSED)
0c6aea3f 375{
149f577a 376 if (!iface->netdev) {
64d64dd7
BP
377 /* We already reported a related error, don't bother duplicating it. */
378 return false;
0c6aea3f 379 }
149f577a 380
64d64dd7 381 if (iface->dp_ifidx < 0) {
6ae39834
BP
382 VLOG_ERR("%s interface not in %s, dropping",
383 iface->name, dpif_name(br->dpif));
384 return false;
385 }
64d64dd7
BP
386
387 VLOG_DBG("%s has interface %s on port %d", dpif_name(br->dpif),
388 iface->name, iface->dp_ifidx);
389 return true;
6ae39834
BP
390}
391
64d64dd7 392/* Callback for iterate_and_prune_ifaces(). */
795fe1fb 393static bool
67a4917b
BP
394set_iface_properties(struct bridge *br OVS_UNUSED, struct iface *iface,
395 void *aux OVS_UNUSED)
795fe1fb 396{
4d678233 397 /* Set policing attributes. */
76343538
BP
398 netdev_set_policing(iface->netdev,
399 iface->cfg->ingress_policing_rate,
400 iface->cfg->ingress_policing_burst);
4d678233
BP
401
402 /* Set MAC address of internal interfaces other than the local
403 * interface. */
6cefe1da 404 if (iface->dp_ifidx != ODPP_LOCAL && !strcmp(iface->type, "internal")) {
4d678233
BP
405 iface_set_mac(iface);
406 }
407
795fe1fb
BP
408 return true;
409}
410
6ae39834
BP
411/* Calls 'cb' for each interfaces in 'br', passing along the 'aux' argument.
412 * Deletes from 'br' all the interfaces for which 'cb' returns false, and then
413 * deletes from 'br' any ports that no longer have any interfaces. */
414static void
415iterate_and_prune_ifaces(struct bridge *br,
416 bool (*cb)(struct bridge *, struct iface *,
417 void *aux),
418 void *aux)
419{
8052fb14 420 struct port *port, *next_port;
6ae39834 421
8052fb14
BP
422 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
423 struct iface *iface, *next_iface;
83db7968 424
8052fb14 425 LIST_FOR_EACH_SAFE (iface, next_iface, port_elem, &port->ifaces) {
83db7968 426 if (!cb(br, iface, aux)) {
bcd49a45 427 iface_set_ofport(iface->cfg, -1);
6ae39834
BP
428 iface_destroy(iface);
429 }
430 }
431
402f2858 432 if (list_is_empty(&port->ifaces)) {
cca46d33 433 VLOG_WARN("%s port has no interfaces, dropping", port->name);
6ae39834
BP
434 port_destroy(port);
435 }
436 }
437}
438
cd11000b
BP
439/* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
440 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
441 * responsible for freeing '*managersp' (with free()).
442 *
443 * You may be asking yourself "why does ovs-vswitchd care?", because
444 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
445 * should not be and in fact is not directly involved in that. But
446 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
447 * it has to tell in-band control where the managers are to enable that.
94db5407 448 * (Thus, only managers connected in-band are collected.)
cd11000b
BP
449 */
450static void
94db5407
BP
451collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
452 struct sockaddr_in **managersp, size_t *n_managersp)
cd11000b
BP
453{
454 struct sockaddr_in *managers = NULL;
455 size_t n_managers = 0;
b3c01ed3 456 struct sset targets;
94db5407
BP
457 size_t i;
458
289df16d
AE
459 /* Collect all of the potential targets from the "targets" columns of the
460 * rows pointed to by "manager_options", excluding any that are
461 * out-of-band. */
b3c01ed3 462 sset_init(&targets);
94db5407
BP
463 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
464 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
465
466 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
b3c01ed3 467 sset_find_and_delete(&targets, m->target);
94db5407 468 } else {
b3c01ed3 469 sset_add(&targets, m->target);
94db5407
BP
470 }
471 }
cd11000b 472
94db5407 473 /* Now extract the targets' IP addresses. */
b3c01ed3
BP
474 if (!sset_is_empty(&targets)) {
475 const char *target;
cd11000b 476
b3c01ed3
BP
477 managers = xmalloc(sset_count(&targets) * sizeof *managers);
478 SSET_FOR_EACH (target, &targets) {
94db5407 479 struct sockaddr_in *sin = &managers[n_managers];
cd11000b 480
94db5407
BP
481 if ((!strncmp(target, "tcp:", 4)
482 && inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
483 (!strncmp(target, "ssl:", 4)
484 && inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
cd11000b
BP
485 n_managers++;
486 }
487 }
488 }
b3c01ed3 489 sset_destroy(&targets);
cd11000b
BP
490
491 *managersp = managers;
492 *n_managersp = n_managers;
493}
494
c5187f17 495static void
76343538 496bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
064af421 497{
76343538
BP
498 struct shash old_br, new_br;
499 struct shash_node *node;
064af421 500 struct bridge *br, *next;
cd11000b
BP
501 struct sockaddr_in *managers;
502 size_t n_managers;
6ae39834 503 size_t i;
72b06300 504 int sflow_bridge_number;
064af421
BP
505
506 COVERAGE_INC(bridge_reconfigure);
507
94db5407 508 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
cd11000b 509
632d136c 510 /* Collect old and new bridges. */
76343538
BP
511 shash_init(&old_br);
512 shash_init(&new_br);
4e8e4213 513 LIST_FOR_EACH (br, node, &all_bridges) {
76343538
BP
514 shash_add(&old_br, br->name, br);
515 }
516 for (i = 0; i < ovs_cfg->n_bridges; i++) {
517 const struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
518 if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
519 VLOG_WARN("more than one bridge named %s", br_cfg->name);
520 }
064af421 521 }
064af421
BP
522
523 /* Get rid of deleted bridges and add new bridges. */
4e8e4213 524 LIST_FOR_EACH_SAFE (br, next, node, &all_bridges) {
76343538
BP
525 struct ovsrec_bridge *br_cfg = shash_find_data(&new_br, br->name);
526 if (br_cfg) {
527 br->cfg = br_cfg;
528 } else {
064af421
BP
529 bridge_destroy(br);
530 }
531 }
76343538
BP
532 SHASH_FOR_EACH (node, &new_br) {
533 const char *br_name = node->name;
534 const struct ovsrec_bridge *br_cfg = node->data;
1a6f1e2a
JG
535 br = shash_find_data(&old_br, br_name);
536 if (br) {
537 /* If the bridge datapath type has changed, we need to tear it
538 * down and recreate. */
539 if (strcmp(br->cfg->datapath_type, br_cfg->datapath_type)) {
540 bridge_destroy(br);
541 bridge_create(br_cfg);
fa33d64a 542 }
1a6f1e2a
JG
543 } else {
544 bridge_create(br_cfg);
064af421
BP
545 }
546 }
76343538
BP
547 shash_destroy(&old_br);
548 shash_destroy(&new_br);
064af421 549
064af421 550 /* Reconfigure all bridges. */
4e8e4213 551 LIST_FOR_EACH (br, node, &all_bridges) {
1a048029 552 bridge_reconfigure_one(br);
064af421
BP
553 }
554
555 /* Add and delete ports on all datapaths.
556 *
557 * The kernel will reject any attempt to add a given port to a datapath if
558 * that port already belongs to a different datapath, so we must do all
559 * port deletions before any port additions. */
4e8e4213 560 LIST_FOR_EACH (br, node, &all_bridges) {
b0ec0f27 561 struct dpif_port_dump dump;
76343538 562 struct shash want_ifaces;
4c738a8d 563 struct dpif_port dpif_port;
064af421 564
064af421 565 bridge_get_all_ifaces(br, &want_ifaces);
4c738a8d
BP
566 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
567 if (!shash_find(&want_ifaces, dpif_port.name)
568 && strcmp(dpif_port.name, br->name)) {
569 int retval = dpif_port_del(br->dpif, dpif_port.port_no);
064af421 570 if (retval) {
cca46d33
BP
571 VLOG_WARN("failed to remove %s interface from %s: %s",
572 dpif_port.name, dpif_name(br->dpif),
573 strerror(retval));
064af421
BP
574 }
575 }
576 }
76343538 577 shash_destroy(&want_ifaces);
064af421 578 }
4e8e4213 579 LIST_FOR_EACH (br, node, &all_bridges) {
76343538 580 struct shash cur_ifaces, want_ifaces;
b0ec0f27 581 struct dpif_port_dump dump;
4c738a8d 582 struct dpif_port dpif_port;
064af421 583
76343538 584 /* Get the set of interfaces currently in this datapath. */
76343538 585 shash_init(&cur_ifaces);
4c738a8d 586 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
b0ec0f27 587 struct dpif_port *port_info = xmalloc(sizeof *port_info);
4c738a8d
BP
588 dpif_port_clone(port_info, &dpif_port);
589 shash_add(&cur_ifaces, dpif_port.name, port_info);
064af421 590 }
064af421 591
76343538
BP
592 /* Get the set of interfaces we want on this datapath. */
593 bridge_get_all_ifaces(br, &want_ifaces);
6c88d577 594
3a6ccc8c 595 hmap_clear(&br->ifaces);
76343538
BP
596 SHASH_FOR_EACH (node, &want_ifaces) {
597 const char *if_name = node->name;
598 struct iface *iface = node->data;
b0ec0f27
BP
599 struct dpif_port *dpif_port;
600 const char *type;
3a6ccc8c
BP
601 int error;
602
b0ec0f27
BP
603 type = iface ? iface->type : "internal";
604 dpif_port = shash_find_data(&cur_ifaces, if_name);
605
3a6ccc8c
BP
606 /* If we have a port or a netdev already, and it's not the type we
607 * want, then delete the port (if any) and close the netdev (if
608 * any). */
c3827f61
BP
609 if ((dpif_port && strcmp(dpif_port->type, type))
610 || (iface && iface->netdev
611 && strcmp(type, netdev_get_type(iface->netdev)))) {
3a6ccc8c 612 if (dpif_port) {
b0ec0f27 613 error = ofproto_port_del(br->ofproto, dpif_port->port_no);
3a6ccc8c
BP
614 if (error) {
615 continue;
616 }
617 dpif_port = NULL;
76343538 618 }
3a6ccc8c
BP
619 if (iface) {
620 netdev_close(iface->netdev);
621 iface->netdev = NULL;
622 }
623 }
76343538 624
c3827f61
BP
625 /* If the port doesn't exist or we don't have the netdev open,
626 * we need to do more work. */
627 if (!dpif_port || (iface && !iface->netdev)) {
628 struct netdev_options options;
629 struct netdev *netdev;
630 struct shash args;
631
632 /* First open the network device. */
633 options.name = if_name;
634 options.type = type;
635 options.args = &args;
636 options.ethertype = NETDEV_ETH_TYPE_NONE;
637
638 shash_init(&args);
639 if (iface) {
6b4186af 640 shash_from_ovs_idl_map(iface->cfg->key_options,
fe4838bc
JP
641 iface->cfg->value_options,
642 iface->cfg->n_options, &args);
82057f51 643 }
c3827f61
BP
644 error = netdev_open(&options, &netdev);
645 shash_destroy(&args);
82057f51 646
c3827f61
BP
647 if (error) {
648 VLOG_WARN("could not open network device %s (%s)",
649 if_name, strerror(error));
3a6ccc8c 650 continue;
76343538 651 }
3a6ccc8c 652
c3827f61
BP
653 /* Then add the port if we haven't already. */
654 if (!dpif_port) {
655 error = dpif_port_add(br->dpif, netdev, NULL);
3a6ccc8c 656 if (error) {
c3827f61
BP
657 netdev_close(netdev);
658 if (error == EFBIG) {
659 VLOG_ERR("ran out of valid port numbers on %s",
660 dpif_name(br->dpif));
661 break;
662 } else {
cca46d33
BP
663 VLOG_WARN("failed to add %s interface to %s: %s",
664 if_name, dpif_name(br->dpif),
665 strerror(error));
c3827f61
BP
666 continue;
667 }
3a6ccc8c
BP
668 }
669 }
c3827f61
BP
670
671 /* Update 'iface'. */
672 if (iface) {
673 iface->netdev = netdev;
c3827f61
BP
674 }
675 } else if (iface && iface->netdev) {
676 struct shash args;
677
678 shash_init(&args);
6b4186af 679 shash_from_ovs_idl_map(iface->cfg->key_options,
fe4838bc
JP
680 iface->cfg->value_options,
681 iface->cfg->n_options, &args);
6d9e6eb4 682 netdev_set_config(iface->netdev, &args);
c3827f61 683 shash_destroy(&args);
3a6ccc8c 684 }
064af421 685 }
76343538 686 shash_destroy(&want_ifaces);
b0ec0f27
BP
687
688 SHASH_FOR_EACH (node, &cur_ifaces) {
689 struct dpif_port *port_info = node->data;
4c738a8d 690 dpif_port_destroy(port_info);
b0ec0f27
BP
691 free(port_info);
692 }
693 shash_destroy(&cur_ifaces);
064af421 694 }
72b06300 695 sflow_bridge_number = 0;
4e8e4213 696 LIST_FOR_EACH (br, node, &all_bridges) {
090f7f50 697 uint8_t ea[ETH_ADDR_LEN];
064af421 698 uint64_t dpid;
07c318f4
BP
699 struct iface *local_iface;
700 struct iface *hw_addr_iface;
093e47f4 701 char *dpid_string;
064af421 702
064af421 703 bridge_fetch_dp_ifaces(br);
064af421 704
f620b43a
BP
705 /* Delete interfaces that cannot be opened.
706 *
707 * From this point forward we are guaranteed that every "struct iface"
708 * has nonnull 'netdev' and correct 'dp_ifidx'. */
64d64dd7 709 iterate_and_prune_ifaces(br, check_iface, NULL);
064af421
BP
710
711 /* Pick local port hardware address, datapath ID. */
07c318f4 712 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
27b3a6e0 713 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
064af421 714 if (local_iface) {
07c318f4 715 int error = netdev_set_etheraddr(local_iface->netdev, ea);
064af421
BP
716 if (error) {
717 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
718 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
719 "Ethernet address: %s",
720 br->name, strerror(error));
721 }
722 }
abe457eb 723 memcpy(br->ea, ea, ETH_ADDR_LEN);
064af421 724
07c318f4 725 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
064af421
BP
726 ofproto_set_datapath_id(br->ofproto, dpid);
727
fc09119a 728 dpid_string = xasprintf("%016"PRIx64, dpid);
093e47f4
BP
729 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
730 free(dpid_string);
731
064af421 732 /* Set NetFlow configuration on this bridge. */
76343538
BP
733 if (br->cfg->netflow) {
734 struct ovsrec_netflow *nf_cfg = br->cfg->netflow;
735 struct netflow_options opts;
736
737 memset(&opts, 0, sizeof opts);
738
739 dpif_get_netflow_ids(br->dpif, &opts.engine_type, &opts.engine_id);
740 if (nf_cfg->engine_type) {
cd746526 741 opts.engine_type = *nf_cfg->engine_type;
76343538
BP
742 }
743 if (nf_cfg->engine_id) {
cd746526 744 opts.engine_id = *nf_cfg->engine_id;
76343538
BP
745 }
746
747 opts.active_timeout = nf_cfg->active_timeout;
748 if (!opts.active_timeout) {
749 opts.active_timeout = -1;
750 } else if (opts.active_timeout < 0) {
e9e2856e
JG
751 VLOG_WARN("bridge %s: active timeout interval set to negative "
752 "value, using default instead (%d seconds)", br->name,
753 NF_ACTIVE_TIMEOUT_DEFAULT);
754 opts.active_timeout = -1;
76343538
BP
755 }
756
757 opts.add_id_to_iface = nf_cfg->add_id_to_interface;
758 if (opts.add_id_to_iface) {
759 if (opts.engine_id > 0x7f) {
760 VLOG_WARN("bridge %s: netflow port mangling may conflict "
761 "with another vswitch, choose an engine id less "
762 "than 128", br->name);
763 }
8052fb14 764 if (hmap_count(&br->ports) > 508) {
76343538
BP
765 VLOG_WARN("bridge %s: netflow port mangling will conflict "
766 "with another port when more than 508 ports are "
767 "used", br->name);
768 }
769 }
770
81e2083f
BP
771 sset_init(&opts.collectors);
772 sset_add_array(&opts.collectors,
773 nf_cfg->targets, nf_cfg->n_targets);
76343538 774 if (ofproto_set_netflow(br->ofproto, &opts)) {
d295e8e9 775 VLOG_ERR("bridge %s: problem setting netflow collectors",
76343538
BP
776 br->name);
777 }
81e2083f 778 sset_destroy(&opts.collectors);
76343538
BP
779 } else {
780 ofproto_set_netflow(br->ofproto, NULL);
781 }
064af421 782
a4af0040
JP
783 /* Set sFlow configuration on this bridge. */
784 if (br->cfg->sflow) {
d49d354b 785 const struct ovsrec_sflow *sflow_cfg = br->cfg->sflow;
76ce9432 786 struct ovsrec_controller **controllers;
72b06300 787 struct ofproto_sflow_options oso;
76ce9432 788 size_t n_controllers;
72b06300 789
a4af0040
JP
790 memset(&oso, 0, sizeof oso);
791
81e2083f
BP
792 sset_init(&oso.targets);
793 sset_add_array(&oso.targets,
794 sflow_cfg->targets, sflow_cfg->n_targets);
72b06300
BP
795
796 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
a4af0040
JP
797 if (sflow_cfg->sampling) {
798 oso.sampling_rate = *sflow_cfg->sampling;
72b06300
BP
799 }
800
801 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
a4af0040
JP
802 if (sflow_cfg->polling) {
803 oso.polling_interval = *sflow_cfg->polling;
72b06300
BP
804 }
805
806 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
a4af0040
JP
807 if (sflow_cfg->header) {
808 oso.header_len = *sflow_cfg->header;
72b06300
BP
809 }
810
811 oso.sub_id = sflow_bridge_number++;
a4af0040
JP
812 oso.agent_device = sflow_cfg->agent;
813
76ce9432 814 oso.control_ip = NULL;
1a048029 815 n_controllers = bridge_get_controllers(br, &controllers);
76ce9432
BP
816 for (i = 0; i < n_controllers; i++) {
817 if (controllers[i]->local_ip) {
818 oso.control_ip = controllers[i]->local_ip;
819 break;
820 }
821 }
72b06300
BP
822 ofproto_set_sflow(br->ofproto, &oso);
823
81e2083f 824 sset_destroy(&oso.targets);
72b06300
BP
825 } else {
826 ofproto_set_sflow(br->ofproto, NULL);
827 }
828
064af421
BP
829 /* Update the controller and related settings. It would be more
830 * straightforward to call this from bridge_reconfigure_one(), but we
831 * can't do it there for two reasons. First, and most importantly, at
832 * that point we don't know the dp_ifidx of any interfaces that have
833 * been added to the bridge (because we haven't actually added them to
834 * the datapath). Second, at that point we haven't set the datapath ID
835 * yet; when a controller is configured, resetting the datapath ID will
836 * immediately disconnect from the controller, so it's better to set
837 * the datapath ID before the controller. */
1a048029 838 bridge_reconfigure_remotes(br, managers, n_managers);
064af421 839 }
4e8e4213 840 LIST_FOR_EACH (br, node, &all_bridges) {
8052fb14
BP
841 struct port *port;
842
f620b43a 843 br->has_bonded_ports = false;
8052fb14 844 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968 845 struct iface *iface;
52df17e7 846
f620b43a 847 port_reconfigure_bond(port);
c1c9c9c4 848
83db7968
BP
849 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
850 iface_update_qos(iface, port->cfg->qos);
c1c9c9c4 851 }
064af421
BP
852 }
853 }
4e8e4213 854 LIST_FOR_EACH (br, node, &all_bridges) {
4d678233 855 iterate_and_prune_ifaces(br, set_iface_properties, NULL);
064af421 856 }
093e47f4 857
b31bcf60
EJ
858 LIST_FOR_EACH (br, node, &all_bridges) {
859 struct iface *iface;
860 HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
861 iface_update_cfm(iface);
862 }
863 }
864
cd11000b 865 free(managers);
a7ff9bd7
BP
866
867 /* ovs-vswitchd has completed initialization, so allow the process that
868 * forked us to exit successfully. */
869 daemonize_complete();
093e47f4
BP
870}
871
872static const char *
af6278e1
BP
873get_ovsrec_key_value(const struct ovsdb_idl_row *row,
874 const struct ovsdb_idl_column *column,
875 const char *key)
093e47f4 876{
af6278e1
BP
877 const struct ovsdb_datum *datum;
878 union ovsdb_atom atom;
879 unsigned int idx;
093e47f4 880
af6278e1
BP
881 datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
882 atom.string = (char *) key;
883 idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
884 return idx == UINT_MAX ? NULL : datum->values[idx].string;
064af421
BP
885}
886
c8143c88
BP
887static const char *
888bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
889{
af6278e1
BP
890 return get_ovsrec_key_value(&br_cfg->header_,
891 &ovsrec_bridge_col_other_config, key);
c8143c88
BP
892}
893
064af421
BP
894static void
895bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
07c318f4 896 struct iface **hw_addr_iface)
064af421 897{
093e47f4 898 const char *hwaddr;
8052fb14 899 struct port *port;
064af421
BP
900 int error;
901
07c318f4 902 *hw_addr_iface = NULL;
064af421
BP
903
904 /* Did the user request a particular MAC? */
093e47f4
BP
905 hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
906 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
064af421
BP
907 if (eth_addr_is_multicast(ea)) {
908 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
909 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
910 } else if (eth_addr_is_zero(ea)) {
911 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
912 } else {
913 return;
914 }
915 }
916
141f4942
BP
917 /* Otherwise choose the minimum non-local MAC address among all of the
918 * interfaces. */
0b420b10 919 memset(ea, 0xff, ETH_ADDR_LEN);
8052fb14 920 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
58b7527e 921 uint8_t iface_ea[ETH_ADDR_LEN];
83db7968 922 struct iface *candidate;
58b7527e
BP
923 struct iface *iface;
924
925 /* Mirror output ports don't participate. */
064af421
BP
926 if (port->is_mirror_output_port) {
927 continue;
928 }
58b7527e
BP
929
930 /* Choose the MAC address to represent the port. */
83db7968 931 iface = NULL;
76343538 932 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
ba09980a
BP
933 /* Find the interface with this Ethernet address (if any) so that
934 * we can provide the correct devname to the caller. */
83db7968 935 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
ba09980a 936 uint8_t candidate_ea[ETH_ADDR_LEN];
8fef8c71 937 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
ba09980a
BP
938 && eth_addr_equals(iface_ea, candidate_ea)) {
939 iface = candidate;
940 }
941 }
58b7527e
BP
942 } else {
943 /* Choose the interface whose MAC address will represent the port.
944 * The Linux kernel bonding code always chooses the MAC address of
945 * the first slave added to a bond, and the Fedora networking
946 * scripts always add slaves to a bond in alphabetical order, so
947 * for compatibility we choose the interface with the name that is
948 * first in alphabetical order. */
83db7968
BP
949 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
950 if (!iface || strcmp(candidate->name, iface->name) < 0) {
58b7527e
BP
951 iface = candidate;
952 }
953 }
954
955 /* The local port doesn't count (since we're trying to choose its
141f4942
BP
956 * MAC address anyway). */
957 if (iface->dp_ifidx == ODPP_LOCAL) {
064af421
BP
958 continue;
959 }
58b7527e
BP
960
961 /* Grab MAC. */
07c318f4 962 error = netdev_get_etheraddr(iface->netdev, iface_ea);
58b7527e 963 if (error) {
064af421
BP
964 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
965 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
966 iface->name, strerror(error));
58b7527e 967 continue;
064af421
BP
968 }
969 }
58b7527e
BP
970
971 /* Compare against our current choice. */
972 if (!eth_addr_is_multicast(iface_ea) &&
141f4942 973 !eth_addr_is_local(iface_ea) &&
58b7527e
BP
974 !eth_addr_is_reserved(iface_ea) &&
975 !eth_addr_is_zero(iface_ea) &&
130f6e5f 976 eth_addr_compare_3way(iface_ea, ea) < 0)
58b7527e 977 {
0babc06f 978 memcpy(ea, iface_ea, ETH_ADDR_LEN);
8fef8c71 979 *hw_addr_iface = iface;
58b7527e 980 }
064af421 981 }
141f4942 982 if (eth_addr_is_multicast(ea)) {
064af421 983 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
07c318f4 984 *hw_addr_iface = NULL;
064af421
BP
985 VLOG_WARN("bridge %s: using default bridge Ethernet "
986 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
987 } else {
988 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
989 br->name, ETH_ADDR_ARGS(ea));
990 }
991}
992
993/* Choose and returns the datapath ID for bridge 'br' given that the bridge
994 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
07c318f4
BP
995 * an interface on 'br', then that interface must be passed in as
996 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
997 * 'hw_addr_iface' must be passed in as a null pointer. */
064af421
BP
998static uint64_t
999bridge_pick_datapath_id(struct bridge *br,
1000 const uint8_t bridge_ea[ETH_ADDR_LEN],
07c318f4 1001 struct iface *hw_addr_iface)
064af421
BP
1002{
1003 /*
1004 * The procedure for choosing a bridge MAC address will, in the most
1005 * ordinary case, also choose a unique MAC that we can use as a datapath
1006 * ID. In some special cases, though, multiple bridges will end up with
1007 * the same MAC address. This is OK for the bridges, but it will confuse
1008 * the OpenFlow controller, because each datapath needs a unique datapath
1009 * ID.
1010 *
1011 * Datapath IDs must be unique. It is also very desirable that they be
1012 * stable from one run to the next, so that policy set on a datapath
1013 * "sticks".
1014 */
093e47f4 1015 const char *datapath_id;
064af421
BP
1016 uint64_t dpid;
1017
093e47f4
BP
1018 datapath_id = bridge_get_other_config(br->cfg, "datapath-id");
1019 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
064af421
BP
1020 return dpid;
1021 }
1022
07c318f4 1023 if (hw_addr_iface) {
064af421 1024 int vlan;
b2f17b15 1025 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
064af421
BP
1026 /*
1027 * A bridge whose MAC address is taken from a VLAN network device
1028 * (that is, a network device created with vconfig(8) or similar
1029 * tool) will have the same MAC address as a bridge on the VLAN
1030 * device's physical network device.
1031 *
1032 * Handle this case by hashing the physical network device MAC
1033 * along with the VLAN identifier.
1034 */
1035 uint8_t buf[ETH_ADDR_LEN + 2];
1036 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
1037 buf[ETH_ADDR_LEN] = vlan >> 8;
1038 buf[ETH_ADDR_LEN + 1] = vlan;
1039 return dpid_from_hash(buf, sizeof buf);
1040 } else {
1041 /*
1042 * Assume that this bridge's MAC address is unique, since it
1043 * doesn't fit any of the cases we handle specially.
1044 */
1045 }
1046 } else {
1047 /*
1048 * A purely internal bridge, that is, one that has no non-virtual
1049 * network devices on it at all, is more difficult because it has no
1050 * natural unique identifier at all.
1051 *
1052 * When the host is a XenServer, we handle this case by hashing the
1053 * host's UUID with the name of the bridge. Names of bridges are
1054 * persistent across XenServer reboots, although they can be reused if
1055 * an internal network is destroyed and then a new one is later
1056 * created, so this is fairly effective.
1057 *
1058 * When the host is not a XenServer, we punt by using a random MAC
1059 * address on each run.
1060 */
1061 const char *host_uuid = xenserver_get_host_uuid();
1062 if (host_uuid) {
1063 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1064 dpid = dpid_from_hash(combined, strlen(combined));
1065 free(combined);
1066 return dpid;
1067 }
1068 }
1069
1070 return eth_addr_to_uint64(bridge_ea);
1071}
1072
1073static uint64_t
1074dpid_from_hash(const void *data, size_t n)
1075{
5eccf359 1076 uint8_t hash[SHA1_DIGEST_SIZE];
064af421
BP
1077
1078 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
5eccf359 1079 sha1_bytes(data, n, hash);
064af421
BP
1080 eth_addr_mark_random(hash);
1081 return eth_addr_to_uint64(hash);
1082}
1083
ea83a2fc 1084static void
ea763e0e 1085iface_refresh_status(struct iface *iface)
ea83a2fc 1086{
ea763e0e
EJ
1087 struct shash sh;
1088
e210037e
AE
1089 enum netdev_flags flags;
1090 uint32_t current;
1091 int64_t bps;
1092 int mtu;
1093 int64_t mtu_64;
1094 int error;
1095
ea763e0e
EJ
1096 shash_init(&sh);
1097
1098 if (!netdev_get_status(iface->netdev, &sh)) {
1099 size_t n;
1100 char **keys, **values;
ea83a2fc 1101
ea763e0e
EJ
1102 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1103 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1104
1105 free(keys);
1106 free(values);
1107 } else {
1108 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1109 }
1110
1111 shash_destroy_free_data(&sh);
e210037e
AE
1112
1113 error = netdev_get_flags(iface->netdev, &flags);
1114 if (!error) {
1115 ovsrec_interface_set_admin_state(iface->cfg, flags & NETDEV_UP ? "up" : "down");
1116 }
1117 else {
1118 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1119 }
1120
1121 error = netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
1122 if (!error) {
1123 ovsrec_interface_set_duplex(iface->cfg,
1124 netdev_features_is_full_duplex(current)
1125 ? "full" : "half");
1126 /* warning: uint64_t -> int64_t conversion */
1127 bps = netdev_features_to_bps(current);
1128 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1129 }
1130 else {
1131 ovsrec_interface_set_duplex(iface->cfg, NULL);
1132 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1133 }
1134
1135
1136 ovsrec_interface_set_link_state(iface->cfg,
0b8024eb 1137 iface_get_carrier(iface) ? "up" : "down");
e210037e
AE
1138
1139 error = netdev_get_mtu(iface->netdev, &mtu);
f915f1a8 1140 if (!error && mtu != INT_MAX) {
e210037e
AE
1141 mtu_64 = mtu;
1142 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1143 }
1144 else {
1145 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1146 }
ea83a2fc
EJ
1147}
1148
6586adf5
EJ
1149/* Writes 'iface''s CFM statistics to the database. Returns true if anything
1150 * changed, false otherwise. */
1151static bool
b31bcf60
EJ
1152iface_refresh_cfm_stats(struct iface *iface)
1153{
b31bcf60 1154 const struct ovsrec_monitor *mon;
e7934396 1155 const struct cfm *cfm;
6586adf5 1156 bool changed = false;
e7934396 1157 size_t i;
b31bcf60
EJ
1158
1159 mon = iface->cfg->monitor;
e7934396 1160 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
b31bcf60
EJ
1161
1162 if (!cfm || !mon) {
6586adf5 1163 return false;
b31bcf60
EJ
1164 }
1165
1166 for (i = 0; i < mon->n_remote_mps; i++) {
1167 const struct ovsrec_maintenance_point *mp;
1168 const struct remote_mp *rmp;
1169
1170 mp = mon->remote_mps[i];
1171 rmp = cfm_get_remote_mp(cfm, mp->mpid);
1172
6586adf5
EJ
1173 if (mp->n_fault != 1 || mp->fault[0] != rmp->fault) {
1174 ovsrec_maintenance_point_set_fault(mp, &rmp->fault, 1);
1175 changed = true;
1176 }
b31bcf60
EJ
1177 }
1178
6586adf5
EJ
1179 if (mon->n_fault != 1 || mon->fault[0] != cfm->fault) {
1180 ovsrec_monitor_set_fault(mon, &cfm->fault, 1);
1181 changed = true;
1182 }
1183
1184 return changed;
b31bcf60
EJ
1185}
1186
018f1525
BP
1187static void
1188iface_refresh_stats(struct iface *iface)
1189{
1190 struct iface_stat {
1191 char *name;
1192 int offset;
1193 };
1194 static const struct iface_stat iface_stats[] = {
1195 { "rx_packets", offsetof(struct netdev_stats, rx_packets) },
1196 { "tx_packets", offsetof(struct netdev_stats, tx_packets) },
1197 { "rx_bytes", offsetof(struct netdev_stats, rx_bytes) },
1198 { "tx_bytes", offsetof(struct netdev_stats, tx_bytes) },
1199 { "rx_dropped", offsetof(struct netdev_stats, rx_dropped) },
1200 { "tx_dropped", offsetof(struct netdev_stats, tx_dropped) },
1201 { "rx_errors", offsetof(struct netdev_stats, rx_errors) },
1202 { "tx_errors", offsetof(struct netdev_stats, tx_errors) },
1203 { "rx_frame_err", offsetof(struct netdev_stats, rx_frame_errors) },
1204 { "rx_over_err", offsetof(struct netdev_stats, rx_over_errors) },
1205 { "rx_crc_err", offsetof(struct netdev_stats, rx_crc_errors) },
1206 { "collisions", offsetof(struct netdev_stats, collisions) },
1207 };
1208 enum { N_STATS = ARRAY_SIZE(iface_stats) };
1209 const struct iface_stat *s;
1210
1211 char *keys[N_STATS];
1212 int64_t values[N_STATS];
1213 int n;
1214
1215 struct netdev_stats stats;
1216
1217 /* Intentionally ignore return value, since errors will set 'stats' to
1218 * all-1s, and we will deal with that correctly below. */
1219 netdev_get_stats(iface->netdev, &stats);
1220
1221 n = 0;
1222 for (s = iface_stats; s < &iface_stats[N_STATS]; s++) {
1223 uint64_t value = *(uint64_t *) (((char *) &stats) + s->offset);
1224 if (value != UINT64_MAX) {
1225 keys[n] = s->name;
1226 values[n] = value;
1227 n++;
1228 }
1229 }
1230
1231 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
1232}
1233
ce887677
BP
1234static void
1235refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1236{
1237 struct ovsdb_datum datum;
1238 struct shash stats;
1239
1240 shash_init(&stats);
1241 get_system_stats(&stats);
1242
1243 ovsdb_datum_from_shash(&datum, &stats);
1244 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1245 &datum);
1246}
1247
bffc0589
AE
1248static inline const char *
1249nx_role_to_str(enum nx_role role)
1250{
1251 switch (role) {
1252 case NX_ROLE_OTHER:
1253 return "other";
1254 case NX_ROLE_MASTER:
1255 return "master";
1256 case NX_ROLE_SLAVE:
1257 return "slave";
1258 default:
1259 return "*** INVALID ROLE ***";
1260 }
1261}
1262
1263static void
1264bridge_refresh_controller_status(const struct bridge *br)
1265{
1266 struct shash info;
1267 const struct ovsrec_controller *cfg;
1268
1269 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1270
1271 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
eb9b8307
AE
1272 struct ofproto_controller_info *cinfo =
1273 shash_find_data(&info, cfg->target);
1274
1275 if (cinfo) {
1276 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1277 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1278 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1279 (char **) cinfo->pairs.values,
1280 cinfo->pairs.n);
1281 } else {
1282 ovsrec_controller_set_is_connected(cfg, false);
1283 ovsrec_controller_set_role(cfg, NULL);
1284 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1285 }
bffc0589
AE
1286 }
1287
1288 ofproto_free_ofproto_controller_info(&info);
1289}
1290
c5187f17 1291void
064af421
BP
1292bridge_run(void)
1293{
d54ff998
BP
1294 const struct ovsrec_open_vswitch *cfg;
1295
c5187f17 1296 bool datapath_destroyed;
d54ff998 1297 bool database_changed;
c5187f17 1298 struct bridge *br;
064af421 1299
c5187f17
BP
1300 /* Let each bridge do the work that it needs to do. */
1301 datapath_destroyed = false;
4e8e4213 1302 LIST_FOR_EACH (br, node, &all_bridges) {
064af421
BP
1303 int error = bridge_run_one(br);
1304 if (error) {
1305 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1306 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
1307 "forcing reconfiguration", br->name);
c5187f17
BP
1308 datapath_destroyed = true;
1309 }
1310 }
1311
1312 /* (Re)configure if necessary. */
d54ff998
BP
1313 database_changed = ovsdb_idl_run(idl);
1314 cfg = ovsrec_open_vswitch_first(idl);
d6da96ce
BP
1315#ifdef HAVE_OPENSSL
1316 /* Re-configure SSL. We do this on every trip through the main loop,
1317 * instead of just when the database changes, because the contents of the
1318 * key and certificate files can change without the database changing.
1319 *
1320 * We do this before bridge_reconfigure() because that function might
1321 * initiate SSL connections and thus requires SSL to be configured. */
1322 if (cfg && cfg->ssl) {
1323 const struct ovsrec_ssl *ssl = cfg->ssl;
1324
1325 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1326 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1327 }
1328#endif
d54ff998 1329 if (database_changed || datapath_destroyed) {
c5187f17
BP
1330 if (cfg) {
1331 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1332
1333 bridge_configure_once(cfg);
1334 bridge_reconfigure(cfg);
1335
1336 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1337 ovsdb_idl_txn_commit(txn);
1338 ovsdb_idl_txn_destroy(txn); /* XXX */
1e0b752d
BP
1339 } else {
1340 /* We still need to reconfigure to avoid dangling pointers to
1341 * now-destroyed ovsrec structures inside bridge data. */
1342 static const struct ovsrec_open_vswitch null_cfg;
1343
1344 bridge_reconfigure(&null_cfg);
064af421
BP
1345 }
1346 }
018f1525 1347
cd0cd65f
BP
1348 /* Refresh system and interface stats if necessary. */
1349 if (time_msec() >= stats_timer) {
ce887677
BP
1350 if (cfg) {
1351 struct ovsdb_idl_txn *txn;
018f1525 1352
ce887677 1353 txn = ovsdb_idl_txn_create(idl);
4e8e4213 1354 LIST_FOR_EACH (br, node, &all_bridges) {
8052fb14 1355 struct port *port;
018f1525 1356
8052fb14 1357 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968 1358 struct iface *iface;
018f1525 1359
83db7968 1360 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
ce887677 1361 iface_refresh_stats(iface);
ea763e0e 1362 iface_refresh_status(iface);
ce887677 1363 }
018f1525 1364 }
bffc0589 1365 bridge_refresh_controller_status(br);
018f1525 1366 }
ce887677
BP
1367 refresh_system_stats(cfg);
1368 ovsdb_idl_txn_commit(txn);
1369 ovsdb_idl_txn_destroy(txn); /* XXX */
018f1525 1370 }
018f1525 1371
cd0cd65f 1372 stats_timer = time_msec() + STATS_INTERVAL;
018f1525 1373 }
6586adf5
EJ
1374
1375 if (time_msec() >= cfm_limiter) {
1376 struct ovsdb_idl_txn *txn;
1377 bool changed = false;
1378
1379 txn = ovsdb_idl_txn_create(idl);
1380 LIST_FOR_EACH (br, node, &all_bridges) {
1381 struct port *port;
1382
1383 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1384 struct iface *iface;
1385
1386 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1387 changed = iface_refresh_cfm_stats(iface) || changed;
1388 }
1389 }
1390 }
1391
1392 if (changed) {
1393 cfm_limiter = time_msec() + CFM_LIMIT_INTERVAL;
1394 }
1395
1396 ovsdb_idl_txn_commit(txn);
1397 ovsdb_idl_txn_destroy(txn);
1398 }
064af421
BP
1399}
1400
1401void
1402bridge_wait(void)
1403{
1404 struct bridge *br;
1405
4e8e4213 1406 LIST_FOR_EACH (br, node, &all_bridges) {
8052fb14 1407 struct port *port;
21dcb94f 1408
064af421 1409 ofproto_wait(br->ofproto);
93dfc067 1410 mac_learning_wait(br->ml);
8052fb14
BP
1411 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1412 port_wait(port);
b31bcf60 1413 }
064af421 1414 }
c5187f17 1415 ovsdb_idl_wait(idl);
cd0cd65f 1416 poll_timer_wait_until(stats_timer);
6586adf5
EJ
1417
1418 if (cfm_limiter > time_msec()) {
1419 poll_timer_wait_until(cfm_limiter);
1420 }
064af421
BP
1421}
1422
1423/* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
1424 * configuration changes. */
1425static void
1426bridge_flush(struct bridge *br)
1427{
1428 COVERAGE_INC(bridge_flush);
1429 br->flush = true;
064af421
BP
1430}
1431\f
8c4c1387
BP
1432/* Bridge unixctl user interface functions. */
1433static void
8ca79daa 1434bridge_unixctl_fdb_show(struct unixctl_conn *conn,
c69ee87c 1435 const char *args, void *aux OVS_UNUSED)
8c4c1387
BP
1436{
1437 struct ds ds = DS_EMPTY_INITIALIZER;
1438 const struct bridge *br;
93dfc067 1439 const struct mac_entry *e;
8c4c1387
BP
1440
1441 br = bridge_lookup(args);
1442 if (!br) {
1443 unixctl_command_reply(conn, 501, "no such bridge");
1444 return;
1445 }
1446
1447 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4e8e4213 1448 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
1648ddd7 1449 struct port *port = e->port.p;
93dfc067 1450 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
1648ddd7 1451 port_get_an_iface(port)->dp_ifidx,
93dfc067 1452 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
8c4c1387
BP
1453 }
1454 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1455 ds_destroy(&ds);
1456}
1457\f
20c8e971
EJ
1458/* CFM unixctl user interface functions. */
1459static void
1460cfm_unixctl_show(struct unixctl_conn *conn,
1461 const char *args, void *aux OVS_UNUSED)
1462{
1463 struct ds ds = DS_EMPTY_INITIALIZER;
1464 struct iface *iface;
1465 const struct cfm *cfm;
1466
1467 iface = iface_find(args);
1468 if (!iface) {
1469 unixctl_command_reply(conn, 501, "no such interface");
1470 return;
1471 }
1472
1473 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1474
1475 if (!cfm) {
1476 unixctl_command_reply(conn, 501, "CFM not enabled");
1477 return;
1478 }
1479
1480 cfm_dump_ds(cfm, &ds);
1481 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1482 ds_destroy(&ds);
1483}
1484\f
e8fe3026
EJ
1485/* QoS unixctl user interface functions. */
1486
1487struct qos_unixctl_show_cbdata {
1488 struct ds *ds;
1489 struct iface *iface;
1490};
1491
1492static void
1493qos_unixctl_show_cb(unsigned int queue_id,
1494 const struct shash *details,
1495 void *aux)
1496{
1497 struct qos_unixctl_show_cbdata *data = aux;
1498 struct ds *ds = data->ds;
1499 struct iface *iface = data->iface;
1500 struct netdev_queue_stats stats;
1501 struct shash_node *node;
1502 int error;
1503
1504 ds_put_cstr(ds, "\n");
1505 if (queue_id) {
1506 ds_put_format(ds, "Queue %u:\n", queue_id);
1507 } else {
1508 ds_put_cstr(ds, "Default:\n");
1509 }
1510
1511 SHASH_FOR_EACH (node, details) {
1512 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
1513 }
1514
1515 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
1516 if (!error) {
1517 if (stats.tx_packets != UINT64_MAX) {
1518 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
1519 }
1520
1521 if (stats.tx_bytes != UINT64_MAX) {
1522 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
1523 }
1524
1525 if (stats.tx_errors != UINT64_MAX) {
1526 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
1527 }
1528 } else {
1529 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
1530 queue_id, strerror(error));
1531 }
1532}
1533
1534static void
1535qos_unixctl_show(struct unixctl_conn *conn,
1536 const char *args, void *aux OVS_UNUSED)
1537{
1538 struct ds ds = DS_EMPTY_INITIALIZER;
1539 struct shash sh = SHASH_INITIALIZER(&sh);
1540 struct iface *iface;
1541 const char *type;
1542 struct shash_node *node;
1543 struct qos_unixctl_show_cbdata data;
1544 int error;
1545
1546 iface = iface_find(args);
1547 if (!iface) {
1548 unixctl_command_reply(conn, 501, "no such interface");
1549 return;
1550 }
1551
1552 netdev_get_qos(iface->netdev, &type, &sh);
1553
1554 if (*type != '\0') {
1555 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
1556
1557 SHASH_FOR_EACH (node, &sh) {
1558 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
1559 }
1560
1561 data.ds = &ds;
1562 data.iface = iface;
1563 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
1564
1565 if (error) {
1566 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
1567 }
1568 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1569 } else {
1570 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
1571 unixctl_command_reply(conn, 501, ds_cstr(&ds));
1572 }
1573
1574 shash_destroy_free_data(&sh);
1575 ds_destroy(&ds);
1576}
1577\f
064af421 1578/* Bridge reconfiguration functions. */
064af421 1579static struct bridge *
1a6f1e2a 1580bridge_create(const struct ovsrec_bridge *br_cfg)
064af421
BP
1581{
1582 struct bridge *br;
1583 int error;
1584
1a6f1e2a 1585 assert(!bridge_lookup(br_cfg->name));
ec6fde61 1586 br = xzalloc(sizeof *br);
064af421 1587
1a6f1e2a
JG
1588 error = dpif_create_and_open(br_cfg->name, br_cfg->datapath_type,
1589 &br->dpif);
efacbce6 1590 if (error) {
064af421
BP
1591 free(br);
1592 return NULL;
1593 }
1594
1a6f1e2a
JG
1595 error = ofproto_create(br_cfg->name, br_cfg->datapath_type, &bridge_ofhooks,
1596 br, &br->ofproto);
064af421 1597 if (error) {
1a6f1e2a
JG
1598 VLOG_ERR("failed to create switch %s: %s", br_cfg->name,
1599 strerror(error));
c228a364
BP
1600 dpif_delete(br->dpif);
1601 dpif_close(br->dpif);
064af421
BP
1602 free(br);
1603 return NULL;
1604 }
1605
1a6f1e2a
JG
1606 br->name = xstrdup(br_cfg->name);
1607 br->cfg = br_cfg;
064af421 1608 br->ml = mac_learning_create();
70150daf 1609 eth_addr_nicira_random(br->default_ea);
064af421 1610
8052fb14 1611 hmap_init(&br->ports);
d9a8717a 1612 hmap_init(&br->ifaces);
4a1ee6ae
BP
1613 shash_init(&br->iface_by_name);
1614
064af421 1615 br->flush = false;
064af421
BP
1616
1617 list_push_back(&all_bridges, &br->node);
1618
c228a364 1619 VLOG_INFO("created bridge %s on %s", br->name, dpif_name(br->dpif));
064af421
BP
1620
1621 return br;
1622}
1623
1624static void
1625bridge_destroy(struct bridge *br)
1626{
1627 if (br) {
8052fb14 1628 struct port *port, *next;
064af421
BP
1629 int error;
1630
8052fb14
BP
1631 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1632 port_destroy(port);
064af421
BP
1633 }
1634 list_remove(&br->node);
6d6ab93e 1635 ofproto_destroy(br->ofproto);
c228a364 1636 error = dpif_delete(br->dpif);
064af421 1637 if (error && error != ENOENT) {
b29ba128 1638 VLOG_ERR("failed to delete %s: %s",
c228a364 1639 dpif_name(br->dpif), strerror(error));
064af421 1640 }
c228a364 1641 dpif_close(br->dpif);
064af421 1642 mac_learning_destroy(br->ml);
d9a8717a 1643 hmap_destroy(&br->ifaces);
8052fb14 1644 hmap_destroy(&br->ports);
4a1ee6ae 1645 shash_destroy(&br->iface_by_name);
064af421
BP
1646 free(br->name);
1647 free(br);
1648 }
1649}
1650
1651static struct bridge *
1652bridge_lookup(const char *name)
1653{
1654 struct bridge *br;
1655
4e8e4213 1656 LIST_FOR_EACH (br, node, &all_bridges) {
064af421
BP
1657 if (!strcmp(br->name, name)) {
1658 return br;
1659 }
1660 }
1661 return NULL;
1662}
1663
4f2cad2c
JP
1664/* Handle requests for a listing of all flows known by the OpenFlow
1665 * stack, including those normally hidden. */
1666static void
8ca79daa 1667bridge_unixctl_dump_flows(struct unixctl_conn *conn,
c69ee87c 1668 const char *args, void *aux OVS_UNUSED)
4f2cad2c
JP
1669{
1670 struct bridge *br;
1671 struct ds results;
d295e8e9 1672
4f2cad2c
JP
1673 br = bridge_lookup(args);
1674 if (!br) {
1675 unixctl_command_reply(conn, 501, "Unknown bridge");
1676 return;
1677 }
1678
1679 ds_init(&results);
1680 ofproto_get_all_flows(br->ofproto, &results);
1681
1682 unixctl_command_reply(conn, 200, ds_cstr(&results));
1683 ds_destroy(&results);
1684}
1685
fa05809b
BP
1686/* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
1687 * connections and reconnect. If BRIDGE is not specified, then all bridges
1688 * drop their controller connections and reconnect. */
1689static void
1690bridge_unixctl_reconnect(struct unixctl_conn *conn,
1691 const char *args, void *aux OVS_UNUSED)
1692{
1693 struct bridge *br;
1694 if (args[0] != '\0') {
1695 br = bridge_lookup(args);
1696 if (!br) {
1697 unixctl_command_reply(conn, 501, "Unknown bridge");
1698 return;
1699 }
1700 ofproto_reconnect_controllers(br->ofproto);
1701 } else {
4e8e4213 1702 LIST_FOR_EACH (br, node, &all_bridges) {
fa05809b
BP
1703 ofproto_reconnect_controllers(br->ofproto);
1704 }
1705 }
1706 unixctl_command_reply(conn, 200, NULL);
1707}
1708
064af421
BP
1709static int
1710bridge_run_one(struct bridge *br)
1711{
8052fb14 1712 struct port *port;
064af421
BP
1713 int error;
1714
1715 error = ofproto_run1(br->ofproto);
1716 if (error) {
1717 return error;
1718 }
1719
93dfc067 1720 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
21dcb94f 1721
8052fb14
BP
1722 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1723 port_run(port);
21dcb94f 1724 }
064af421
BP
1725
1726 error = ofproto_run2(br->ofproto, br->flush);
1727 br->flush = false;
1728
1729 return error;
1730}
1731
76ce9432 1732static size_t
1a048029 1733bridge_get_controllers(const struct bridge *br,
76ce9432 1734 struct ovsrec_controller ***controllersp)
064af421 1735{
76ce9432
BP
1736 struct ovsrec_controller **controllers;
1737 size_t n_controllers;
064af421 1738
1a048029
JP
1739 controllers = br->cfg->controller;
1740 n_controllers = br->cfg->n_controller;
76343538 1741
76ce9432
BP
1742 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
1743 controllers = NULL;
1744 n_controllers = 0;
064af421 1745 }
76343538 1746
76ce9432
BP
1747 if (controllersp) {
1748 *controllersp = controllers;
1749 }
1750 return n_controllers;
064af421
BP
1751}
1752
1753static void
1a048029 1754bridge_reconfigure_one(struct bridge *br)
064af421 1755{
8052fb14 1756 enum ofproto_fail_mode fail_mode;
8052fb14 1757 struct port *port, *next;
76343538 1758 struct shash_node *node;
8052fb14 1759 struct shash new_ports;
6ae39834 1760 size_t i;
064af421 1761
064af421 1762 /* Collect new ports. */
76343538
BP
1763 shash_init(&new_ports);
1764 for (i = 0; i < br->cfg->n_ports; i++) {
1765 const char *name = br->cfg->ports[i]->name;
1766 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
1767 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1768 br->name, name);
1769 }
1770 }
e073f944
BP
1771
1772 /* If we have a controller, then we need a local port. Complain if the
1773 * user didn't specify one.
1774 *
1775 * XXX perhaps we should synthesize a port ourselves in this case. */
1a048029 1776 if (bridge_get_controllers(br, NULL)) {
72865317
BP
1777 char local_name[IF_NAMESIZE];
1778 int error;
1779
1780 error = dpif_port_get_name(br->dpif, ODPP_LOCAL,
1781 local_name, sizeof local_name);
e073f944
BP
1782 if (!error && !shash_find(&new_ports, local_name)) {
1783 VLOG_WARN("bridge %s: controller specified but no local port "
1784 "(port named %s) defined",
1785 br->name, local_name);
72865317 1786 }
064af421 1787 }
064af421 1788
4a1ee6ae
BP
1789 /* Get rid of deleted ports.
1790 * Get rid of deleted interfaces on ports that still exist. */
8052fb14 1791 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
4a1ee6ae
BP
1792 const struct ovsrec_port *port_cfg;
1793
8052fb14 1794 port_cfg = shash_find_data(&new_ports, port->name);
4a1ee6ae
BP
1795 if (!port_cfg) {
1796 port_destroy(port);
1797 } else {
1798 port_del_ifaces(port, port_cfg);
064af421
BP
1799 }
1800 }
4a1ee6ae
BP
1801
1802 /* Create new ports.
1803 * Add new interfaces to existing ports.
1804 * Reconfigure existing ports. */
76343538 1805 SHASH_FOR_EACH (node, &new_ports) {
8052fb14 1806 struct port *port = port_lookup(br, node->name);
76343538
BP
1807 if (!port) {
1808 port = port_create(br, node->name);
064af421 1809 }
ceb4559f 1810
76343538 1811 port_reconfigure(port, node->data);
402f2858 1812 if (list_is_empty(&port->ifaces)) {
ceb4559f
JG
1813 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1814 br->name, port->name);
1815 port_destroy(port);
1816 }
064af421 1817 }
76343538 1818 shash_destroy(&new_ports);
064af421 1819
31681a5d
JP
1820 /* Set the fail-mode */
1821 fail_mode = !br->cfg->fail_mode
1822 || !strcmp(br->cfg->fail_mode, "standalone")
1823 ? OFPROTO_FAIL_STANDALONE
1824 : OFPROTO_FAIL_SECURE;
7d674866
BP
1825 if (ofproto_get_fail_mode(br->ofproto) != fail_mode
1826 && !ofproto_has_primary_controller(br->ofproto)) {
abdfe474
JP
1827 ofproto_flush_flows(br->ofproto);
1828 }
31681a5d
JP
1829 ofproto_set_fail_mode(br->ofproto, fail_mode);
1830
064af421
BP
1831 /* Delete all flows if we're switching from connected to standalone or vice
1832 * versa. (XXX Should we delete all flows if we are switching from one
1833 * controller to another?) */
1834
5a243150 1835 /* Configure OpenFlow controller connection snooping. */
81e2083f
BP
1836 if (!ofproto_has_snoops(br->ofproto)) {
1837 struct sset snoops;
1838
1839 sset_init(&snoops);
1840 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
1841 ovs_rundir(), br->name));
76343538 1842 ofproto_set_snoops(br->ofproto, &snoops);
81e2083f 1843 sset_destroy(&snoops);
76343538 1844 }
76343538 1845
064af421
BP
1846 mirror_reconfigure(br);
1847}
1848
7d674866
BP
1849/* Initializes 'oc' appropriately as a management service controller for
1850 * 'br'.
1851 *
1852 * The caller must free oc->target when it is no longer needed. */
1853static void
1854bridge_ofproto_controller_for_mgmt(const struct bridge *br,
1855 struct ofproto_controller *oc)
1856{
b43c6fe2 1857 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
7d674866
BP
1858 oc->max_backoff = 0;
1859 oc->probe_interval = 60;
1860 oc->band = OFPROTO_OUT_OF_BAND;
7d674866
BP
1861 oc->rate_limit = 0;
1862 oc->burst_limit = 0;
1863}
1864
1865/* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
1866static void
1867bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
1868 struct ofproto_controller *oc)
1869{
1870 oc->target = c->target;
1871 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
1872 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
1873 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
1874 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
7d674866
BP
1875 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
1876 oc->burst_limit = (c->controller_burst_limit
1877 ? *c->controller_burst_limit : 0);
1878}
1879
1880/* Configures the IP stack for 'br''s local interface properly according to the
1881 * configuration in 'c'. */
1882static void
1883bridge_configure_local_iface_netdev(struct bridge *br,
1884 struct ovsrec_controller *c)
1885{
1886 struct netdev *netdev;
1887 struct in_addr mask, gateway;
1888
1889 struct iface *local_iface;
1890 struct in_addr ip;
1891
7d674866 1892 /* If there's no local interface or no IP address, give up. */
27b3a6e0 1893 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
7d674866
BP
1894 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
1895 return;
1896 }
1897
1898 /* Bring up the local interface. */
1899 netdev = local_iface->netdev;
1900 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1901
1902 /* Configure the IP address and netmask. */
1903 if (!c->local_netmask
1904 || !inet_aton(c->local_netmask, &mask)
1905 || !mask.s_addr) {
1906 mask.s_addr = guess_netmask(ip.s_addr);
1907 }
1908 if (!netdev_set_in4(netdev, ip, mask)) {
1909 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
1910 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
1911 }
1912
1913 /* Configure the default gateway. */
1914 if (c->local_gateway
1915 && inet_aton(c->local_gateway, &gateway)
1916 && gateway.s_addr) {
1917 if (!netdev_add_router(netdev, gateway)) {
1918 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1919 br->name, IP_ARGS(&gateway.s_addr));
1920 }
1921 }
1922}
1923
064af421 1924static void
1a048029 1925bridge_reconfigure_remotes(struct bridge *br,
11bbff1b
BP
1926 const struct sockaddr_in *managers,
1927 size_t n_managers)
064af421 1928{
b1da6250
BP
1929 const char *disable_ib_str, *queue_id_str;
1930 bool disable_in_band = false;
1931 int queue_id;
1932
76ce9432
BP
1933 struct ovsrec_controller **controllers;
1934 size_t n_controllers;
7d674866
BP
1935 bool had_primary;
1936
1937 struct ofproto_controller *ocs;
1938 size_t n_ocs;
1939 size_t i;
064af421 1940
8731b2b6
JP
1941 /* Check if we should disable in-band control on this bridge. */
1942 disable_ib_str = bridge_get_other_config(br->cfg, "disable-in-band");
1943 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
1944 disable_in_band = true;
1945 }
1946
b1da6250
BP
1947 /* Set OpenFlow queue ID for in-band control. */
1948 queue_id_str = bridge_get_other_config(br->cfg, "in-band-queue");
1949 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
1950 ofproto_set_in_band_queue(br->ofproto, queue_id);
1951
8731b2b6
JP
1952 if (disable_in_band) {
1953 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
1954 } else {
1955 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
1956 }
7d674866 1957 had_primary = ofproto_has_primary_controller(br->ofproto);
cd11000b 1958
1a048029 1959 n_controllers = bridge_get_controllers(br, &controllers);
064af421 1960
7d674866
BP
1961 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
1962 n_ocs = 0;
064af421 1963
7d674866
BP
1964 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
1965 for (i = 0; i < n_controllers; i++) {
1966 struct ovsrec_controller *c = controllers[i];
76ce9432 1967
7d674866
BP
1968 if (!strncmp(c->target, "punix:", 6)
1969 || !strncmp(c->target, "unix:", 5)) {
1970 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
76ce9432 1971
7d674866
BP
1972 /* Prevent remote ovsdb-server users from accessing arbitrary Unix
1973 * domain sockets and overwriting arbitrary local files. */
1974 VLOG_ERR_RL(&rl, "%s: not adding Unix domain socket controller "
1975 "\"%s\" due to possibility for remote exploit",
1976 dpif_name(br->dpif), c->target);
1977 continue;
1978 }
76ce9432 1979
7d674866 1980 bridge_configure_local_iface_netdev(br, c);
8731b2b6
JP
1981 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
1982 if (disable_in_band) {
1983 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
1984 }
1985 n_ocs++;
7d674866 1986 }
76ce9432 1987
7d674866
BP
1988 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
1989 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
1990 free(ocs);
064af421 1991
7d674866
BP
1992 if (had_primary != ofproto_has_primary_controller(br->ofproto)) {
1993 ofproto_flush_flows(br->ofproto);
1994 }
76ce9432 1995
7d674866
BP
1996 /* If there are no controllers and the bridge is in standalone
1997 * mode, set up a flow that matches every packet and directs
1998 * them to OFPP_NORMAL (which goes to us). Otherwise, the
1999 * switch is in secure mode and we won't pass any traffic until
2000 * a controller has been defined and it tells us to do so. */
2001 if (!n_controllers
2002 && ofproto_get_fail_mode(br->ofproto) == OFPROTO_FAIL_STANDALONE) {
2003 union ofp_action action;
cf3fad8a 2004 struct cls_rule rule;
76ce9432 2005
7d674866
BP
2006 memset(&action, 0, sizeof action);
2007 action.type = htons(OFPAT_OUTPUT);
2008 action.output.len = htons(sizeof action);
2009 action.output.port = htons(OFPP_NORMAL);
cf3fad8a 2010 cls_rule_init_catchall(&rule, 0);
fa8b054f 2011 ofproto_add_flow(br->ofproto, &rule, &action, 1);
064af421 2012 }
064af421
BP
2013}
2014
2015static void
76343538 2016bridge_get_all_ifaces(const struct bridge *br, struct shash *ifaces)
064af421 2017{
8052fb14 2018 struct port *port;
064af421 2019
76343538 2020 shash_init(ifaces);
8052fb14 2021 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968
BP
2022 struct iface *iface;
2023
2024 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
76343538 2025 shash_add_once(ifaces, iface->name, iface);
064af421 2026 }
402f2858 2027 if (!list_is_short(&port->ifaces) && port->cfg->bond_fake_iface) {
76343538 2028 shash_add_once(ifaces, port->name, NULL);
35c979bf 2029 }
064af421 2030 }
064af421
BP
2031}
2032
2033/* For robustness, in case the administrator moves around datapath ports behind
2034 * our back, we re-check all the datapath port numbers here.
2035 *
2036 * This function will set the 'dp_ifidx' members of interfaces that have
2037 * disappeared to -1, so only call this function from a context where those
2038 * 'struct iface's will be removed from the bridge. Otherwise, the -1
2039 * 'dp_ifidx'es will cause trouble later when we try to send them to the
2040 * datapath, which doesn't support UINT16_MAX+1 ports. */
2041static void
2042bridge_fetch_dp_ifaces(struct bridge *br)
2043{
b0ec0f27 2044 struct dpif_port_dump dump;
4c738a8d 2045 struct dpif_port dpif_port;
8052fb14 2046 struct port *port;
064af421
BP
2047
2048 /* Reset all interface numbers. */
8052fb14 2049 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968
BP
2050 struct iface *iface;
2051
2052 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
064af421
BP
2053 iface->dp_ifidx = -1;
2054 }
2055 }
d9a8717a 2056 hmap_clear(&br->ifaces);
064af421 2057
4c738a8d
BP
2058 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
2059 struct iface *iface = iface_lookup(br, dpif_port.name);
064af421
BP
2060 if (iface) {
2061 if (iface->dp_ifidx >= 0) {
b29ba128 2062 VLOG_WARN("%s reported interface %s twice",
4c738a8d
BP
2063 dpif_name(br->dpif), dpif_port.name);
2064 } else if (iface_from_dp_ifidx(br, dpif_port.port_no)) {
b29ba128 2065 VLOG_WARN("%s reported interface %"PRIu16" twice",
4c738a8d 2066 dpif_name(br->dpif), dpif_port.port_no);
064af421 2067 } else {
4c738a8d 2068 iface->dp_ifidx = dpif_port.port_no;
d9a8717a
BP
2069 hmap_insert(&br->ifaces, &iface->dp_ifidx_node,
2070 hash_int(iface->dp_ifidx, 0));
064af421 2071 }
093e47f4 2072
bcd49a45
BP
2073 iface_set_ofport(iface->cfg,
2074 (iface->dp_ifidx >= 0
2075 ? odp_port_to_ofp_port(iface->dp_ifidx)
2076 : -1));
064af421
BP
2077 }
2078 }
064af421
BP
2079}
2080\f
2081/* Bridge packet processing functions. */
2082
064af421 2083static bool
0c62a7ad 2084set_dst(struct dst *dst, const struct flow *flow,
064af421
BP
2085 const struct port *in_port, const struct port *out_port,
2086 tag_type *tags)
2087{
d55c2566
BP
2088 dst->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
2089 : in_port->vlan >= 0 ? in_port->vlan
2090 : flow->vlan_tci == 0 ? OFP_VLAN_NONE
2091 : vlan_tci_to_vid(flow->vlan_tci));
207079c4 2092
d55c2566
BP
2093 dst->iface = (!out_port->bond
2094 ? port_get_an_iface(out_port)
2095 : bond_choose_output_slave(out_port->bond, flow,
2096 dst->vlan, tags));
2097
2098 return dst->iface != NULL;
064af421
BP
2099}
2100
2101static void
2102swap_dst(struct dst *p, struct dst *q)
2103{
2104 struct dst tmp = *p;
2105 *p = *q;
2106 *q = tmp;
2107}
2108
2109/* Moves all the dsts with vlan == 'vlan' to the front of the 'n_dsts' in
2110 * 'dsts'. (This may help performance by reducing the number of VLAN changes
2111 * that we push to the datapath. We could in fact fully sort the array by
2112 * vlan, but in most cases there are at most two different vlan tags so that's
2113 * possibly overkill.) */
2114static void
0c62a7ad 2115partition_dsts(struct dst_set *set, int vlan)
064af421 2116{
0c62a7ad
BP
2117 struct dst *first = set->dsts;
2118 struct dst *last = set->dsts + set->n;
064af421
BP
2119
2120 while (first != last) {
2121 /* Invariants:
2122 * - All dsts < first have vlan == 'vlan'.
2123 * - All dsts >= last have vlan != 'vlan'.
2124 * - first < last. */
2125 while (first->vlan == vlan) {
2126 if (++first == last) {
2127 return;
2128 }
2129 }
2130
2131 /* Same invariants, plus one additional:
2132 * - first->vlan != vlan.
2133 */
2134 while (last[-1].vlan != vlan) {
2135 if (--last == first) {
2136 return;
2137 }
2138 }
2139
2140 /* Same invariants, plus one additional:
2141 * - last[-1].vlan == vlan.*/
2142 swap_dst(first++, --last);
2143 }
2144}
2145
2146static int
2147mirror_mask_ffs(mirror_mask_t mask)
2148{
2149 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
2150 return ffs(mask);
2151}
2152
0c62a7ad
BP
2153static void
2154dst_set_init(struct dst_set *set)
2155{
2156 set->dsts = set->builtin;
2157 set->n = 0;
2158 set->allocated = ARRAY_SIZE(set->builtin);
2159}
2160
2161static void
2162dst_set_add(struct dst_set *set, const struct dst *dst)
2163{
2164 if (set->n >= set->allocated) {
2165 size_t new_allocated;
2166 struct dst *new_dsts;
2167
2168 new_allocated = set->allocated * 2;
2169 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
2170 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
2171
2172 dst_set_free(set);
2173
2174 set->dsts = new_dsts;
2175 set->allocated = new_allocated;
2176 }
2177 set->dsts[set->n++] = *dst;
2178}
2179
2180static void
2181dst_set_free(struct dst_set *set)
2182{
2183 if (set->dsts != set->builtin) {
2184 free(set->dsts);
2185 }
2186}
2187
064af421 2188static bool
0c62a7ad 2189dst_is_duplicate(const struct dst_set *set, const struct dst *test)
064af421
BP
2190{
2191 size_t i;
0c62a7ad
BP
2192 for (i = 0; i < set->n; i++) {
2193 if (set->dsts[i].vlan == test->vlan
d55c2566 2194 && set->dsts[i].iface == test->iface) {
064af421
BP
2195 return true;
2196 }
2197 }
2198 return false;
2199}
2200
2201static bool
2202port_trunks_vlan(const struct port *port, uint16_t vlan)
2203{
3e9c481c
BP
2204 return (port->vlan < 0
2205 && (!port->trunks || bitmap_is_set(port->trunks, vlan)));
064af421
BP
2206}
2207
2208static bool
2209port_includes_vlan(const struct port *port, uint16_t vlan)
2210{
2211 return vlan == port->vlan || port_trunks_vlan(port, vlan);
2212}
2213
a4e2e1f2
EJ
2214static bool
2215port_is_floodable(const struct port *port)
2216{
83db7968 2217 struct iface *iface;
a4e2e1f2 2218
83db7968 2219 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
a4e2e1f2 2220 if (!ofproto_port_is_floodable(port->bridge->ofproto,
83db7968 2221 iface->dp_ifidx)) {
a4e2e1f2
EJ
2222 return false;
2223 }
2224 }
2225 return true;
2226}
2227
f620b43a 2228/* Returns an arbitrary interface within 'port'. */
83db7968
BP
2229static struct iface *
2230port_get_an_iface(const struct port *port)
2231{
2232 return CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
2233}
2234
0c62a7ad 2235static void
ae412e7d 2236compose_dsts(const struct bridge *br, const struct flow *flow, uint16_t vlan,
064af421 2237 const struct port *in_port, const struct port *out_port,
0c62a7ad 2238 struct dst_set *set, tag_type *tags, uint16_t *nf_output_iface)
064af421 2239{
0c62a7ad 2240 struct dst dst;
66642cb4 2241
064af421 2242 if (out_port == FLOOD_PORT) {
8052fb14
BP
2243 struct port *port;
2244
2245 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
a4e2e1f2
EJ
2246 if (port != in_port
2247 && port_is_floodable(port)
2248 && port_includes_vlan(port, vlan)
064af421 2249 && !port->is_mirror_output_port
0c62a7ad 2250 && set_dst(&dst, flow, in_port, port, tags)) {
0c62a7ad 2251 dst_set_add(set, &dst);
064af421
BP
2252 }
2253 }
6a07af36 2254 *nf_output_iface = NF_OUT_FLOOD;
0c62a7ad
BP
2255 } else if (out_port && set_dst(&dst, flow, in_port, out_port, tags)) {
2256 dst_set_add(set, &dst);
d55c2566 2257 *nf_output_iface = dst.iface->dp_ifidx;
c38e3405
BP
2258 }
2259}
2260
2261static void
2262compose_mirror_dsts(const struct bridge *br, const struct flow *flow,
2263 uint16_t vlan, const struct port *in_port,
2264 struct dst_set *set, tag_type *tags)
2265{
2266 mirror_mask_t mirrors;
2267 int flow_vlan;
2268 size_t i;
2269
2270 mirrors = in_port->src_mirrors;
2271 for (i = 0; i < set->n; i++) {
2272 mirrors |= set->dsts[i].iface->port->dst_mirrors;
2273 }
2274
2275 if (!mirrors) {
2276 return;
2277 }
2278
2279 flow_vlan = vlan_tci_to_vid(flow->vlan_tci);
2280 if (flow_vlan == 0) {
2281 flow_vlan = OFP_VLAN_NONE;
064af421
BP
2282 }
2283
2284 while (mirrors) {
2285 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
2286 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
c38e3405
BP
2287 struct dst dst;
2288
064af421 2289 if (m->out_port) {
0c62a7ad
BP
2290 if (set_dst(&dst, flow, in_port, m->out_port, tags)
2291 && !dst_is_duplicate(set, &dst)) {
2292 dst_set_add(set, &dst);
064af421
BP
2293 }
2294 } else {
8052fb14
BP
2295 struct port *port;
2296
2297 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
064af421 2298 if (port_includes_vlan(port, m->out_vlan)
0c62a7ad 2299 && set_dst(&dst, flow, in_port, port, tags))
064af421
BP
2300 {
2301 if (port->vlan < 0) {
0c62a7ad 2302 dst.vlan = m->out_vlan;
064af421 2303 }
0c62a7ad 2304 if (dst_is_duplicate(set, &dst)) {
274de4d2
BP
2305 continue;
2306 }
43aa5f47
JG
2307
2308 /* Use the vlan tag on the original flow instead of
2309 * the one passed in the vlan parameter. This ensures
2310 * that we compare the vlan from before any implicit
2311 * tagging tags place. This is necessary because
2312 * dst->vlan is the final vlan, after removing implicit
2313 * tags. */
0c62a7ad 2314 if (port == in_port && dst.vlan == flow_vlan) {
064af421
BP
2315 /* Don't send out input port on same VLAN. */
2316 continue;
2317 }
0c62a7ad 2318 dst_set_add(set, &dst);
064af421
BP
2319 }
2320 }
2321 }
2322 }
2323 mirrors &= mirrors - 1;
2324 }
064af421
BP
2325}
2326
064af421 2327static void
ae412e7d 2328compose_actions(struct bridge *br, const struct flow *flow, uint16_t vlan,
064af421 2329 const struct port *in_port, const struct port *out_port,
cdee00fd 2330 tag_type *tags, struct ofpbuf *actions,
6a07af36 2331 uint16_t *nf_output_iface)
064af421 2332{
0c62a7ad 2333 struct dst_set set;
064af421 2334 uint16_t cur_vlan;
0c62a7ad 2335 size_t i;
064af421 2336
0c62a7ad
BP
2337 dst_set_init(&set);
2338 compose_dsts(br, flow, vlan, in_port, out_port, &set, tags,
2339 nf_output_iface);
c38e3405 2340 compose_mirror_dsts(br, flow, vlan, in_port, &set, tags);
064af421 2341
66642cb4
BP
2342 cur_vlan = vlan_tci_to_vid(flow->vlan_tci);
2343 if (cur_vlan == 0) {
2344 cur_vlan = OFP_VLAN_NONE;
2345 }
c38e3405
BP
2346
2347 partition_dsts(&set, cur_vlan);
2348
0c62a7ad
BP
2349 for (i = 0; i < set.n; i++) {
2350 const struct dst *dst = &set.dsts[i];
2351 if (dst->vlan != cur_vlan) {
2352 if (dst->vlan == OFP_VLAN_NONE) {
7aec165d 2353 nl_msg_put_flag(actions, ODP_ACTION_ATTR_STRIP_VLAN);
064af421 2354 } else {
cdee00fd 2355 ovs_be16 tci;
0c62a7ad 2356 tci = htons(dst->vlan & VLAN_VID_MASK);
cdee00fd 2357 tci |= flow->vlan_tci & htons(VLAN_PCP_MASK);
7aec165d 2358 nl_msg_put_be16(actions, ODP_ACTION_ATTR_SET_DL_TCI, tci);
064af421 2359 }
0c62a7ad 2360 cur_vlan = dst->vlan;
064af421 2361 }
d55c2566 2362 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->iface->dp_ifidx);
064af421 2363 }
0c62a7ad 2364 dst_set_free(&set);
064af421
BP
2365}
2366
e96a4d80
JG
2367/* Returns the effective vlan of a packet, taking into account both the
2368 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2369 * the packet is untagged and -1 indicates it has an invalid header and
2370 * should be dropped. */
ae412e7d 2371static int flow_get_vlan(struct bridge *br, const struct flow *flow,
e96a4d80
JG
2372 struct port *in_port, bool have_packet)
2373{
66642cb4 2374 int vlan = vlan_tci_to_vid(flow->vlan_tci);
e96a4d80
JG
2375 if (in_port->vlan >= 0) {
2376 if (vlan) {
e96a4d80
JG
2377 if (have_packet) {
2378 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
66642cb4 2379 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
e96a4d80
JG
2380 "packet received on port %s configured with "
2381 "implicit VLAN %"PRIu16,
66642cb4 2382 br->name, vlan, in_port->name, in_port->vlan);
e96a4d80
JG
2383 }
2384 return -1;
2385 }
2386 vlan = in_port->vlan;
2387 } else {
2388 if (!port_includes_vlan(in_port, vlan)) {
2389 if (have_packet) {
2390 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2391 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2392 "packet received on port %s not configured for "
2393 "trunking VLAN %d",
2394 br->name, vlan, in_port->name, vlan);
2395 }
2396 return -1;
2397 }
2398 }
2399
2400 return vlan;
2401}
2402
7febb910
JG
2403/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2404 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2405 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2406static bool
ae412e7d 2407is_gratuitous_arp(const struct flow *flow)
7febb910
JG
2408{
2409 return (flow->dl_type == htons(ETH_TYPE_ARP)
2410 && eth_addr_is_broadcast(flow->dl_dst)
2411 && (flow->nw_proto == ARP_OP_REPLY
2412 || (flow->nw_proto == ARP_OP_REQUEST
2413 && flow->nw_src == flow->nw_dst)));
2414}
2415
e96a4d80 2416static void
ae412e7d 2417update_learning_table(struct bridge *br, const struct flow *flow, int vlan,
e96a4d80
JG
2418 struct port *in_port)
2419{
db8077c3
BP
2420 struct mac_entry *mac;
2421
2422 if (!mac_learning_may_learn(br->ml, flow->dl_src, vlan)) {
2423 return;
2424 }
2425
2426 mac = mac_learning_insert(br->ml, flow->dl_src, vlan);
2427 if (is_gratuitous_arp(flow)) {
2428 /* We don't want to learn from gratuitous ARP packets that are
2429 * reflected back over bond slaves so we lock the learning table. */
402f2858 2430 if (!in_port->bond) {
db8077c3
BP
2431 mac_entry_set_grat_arp_lock(mac);
2432 } else if (mac_entry_is_grat_arp_locked(mac)) {
2433 return;
2434 }
2435 }
2436
1648ddd7 2437 if (mac_entry_is_new(mac) || mac->port.p != in_port) {
e96a4d80
JG
2438 /* The log messages here could actually be useful in debugging,
2439 * so keep the rate limit relatively high. */
db8077c3 2440 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
e96a4d80
JG
2441 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2442 "on port %s in VLAN %d",
2443 br->name, ETH_ADDR_ARGS(flow->dl_src),
2444 in_port->name, vlan);
db8077c3 2445
1648ddd7 2446 mac->port.p = in_port;
db8077c3 2447 ofproto_revalidate(br->ofproto, mac_learning_changed(br->ml, mac));
e96a4d80
JG
2448 }
2449}
2450
14a34d00
BP
2451/* Determines whether packets in 'flow' within 'br' should be forwarded or
2452 * dropped. Returns true if they may be forwarded, false if they should be
2453 * dropped.
2454 *
2455 * If 'have_packet' is true, it indicates that the caller is processing a
2456 * received packet. If 'have_packet' is false, then the caller is just
2457 * revalidating an existing flow because configuration has changed. Either
2458 * way, 'have_packet' only affects logging (there is no point in logging errors
2459 * during revalidation).
2460 *
2461 * Sets '*in_portp' to the input port. This will be a null pointer if
2462 * flow->in_port does not designate a known input port (in which case
2463 * is_admissible() returns false).
2464 *
2465 * When returning true, sets '*vlanp' to the effective VLAN of the input
2466 * packet, as returned by flow_get_vlan().
2467 *
2468 * May also add tags to '*tags', although the current implementation only does
2469 * so in one special case.
2470 */
064af421 2471static bool
ae412e7d 2472is_admissible(struct bridge *br, const struct flow *flow, bool have_packet,
14a34d00 2473 tag_type *tags, int *vlanp, struct port **in_portp)
064af421
BP
2474{
2475 struct iface *in_iface;
2476 struct port *in_port;
064af421
BP
2477 int vlan;
2478
2479 /* Find the interface and port structure for the received packet. */
2480 in_iface = iface_from_dp_ifidx(br, flow->in_port);
2481 if (!in_iface) {
2482 /* No interface? Something fishy... */
14a34d00 2483 if (have_packet) {
064af421
BP
2484 /* Odd. A few possible reasons here:
2485 *
2486 * - We deleted an interface but there are still a few packets
2487 * queued up from it.
2488 *
2489 * - Someone externally added an interface (e.g. with "ovs-dpctl
2490 * add-if") that we don't know about.
2491 *
2492 * - Packet arrived on the local port but the local port is not
2493 * one of our bridge ports.
2494 */
2495 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2496
2497 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
d295e8e9 2498 "interface %"PRIu16, br->name, flow->in_port);
064af421
BP
2499 }
2500
14a34d00
BP
2501 *in_portp = NULL;
2502 return false;
064af421 2503 }
14a34d00
BP
2504 *in_portp = in_port = in_iface->port;
2505 *vlanp = vlan = flow_get_vlan(br, flow, in_port, have_packet);
e96a4d80 2506 if (vlan < 0) {
14a34d00 2507 return false;
064af421
BP
2508 }
2509
064af421
BP
2510 /* Drop frames for reserved multicast addresses. */
2511 if (eth_addr_is_reserved(flow->dl_dst)) {
14a34d00 2512 return false;
064af421
BP
2513 }
2514
2515 /* Drop frames on ports reserved for mirroring. */
2516 if (in_port->is_mirror_output_port) {
14a34d00 2517 if (have_packet) {
f925807d
BP
2518 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2519 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2520 "%s, which is reserved exclusively for mirroring",
2521 br->name, in_port->name);
2522 }
14a34d00 2523 return false;
064af421
BP
2524 }
2525
f620b43a 2526 if (in_port->bond) {
db8077c3 2527 struct mac_entry *mac;
3a55ef14 2528
f620b43a
BP
2529 switch (bond_check_admissibility(in_port->bond, in_iface,
2530 flow->dl_dst, tags)) {
2531 case BV_ACCEPT:
2532 break;
2533
2534 case BV_DROP:
2535 return false;
3a55ef14 2536
f620b43a
BP
2537 case BV_DROP_IF_MOVED:
2538 mac = mac_learning_lookup(br->ml, flow->dl_src, vlan, NULL);
2539 if (mac && mac->port.p != in_port &&
2540 (!is_gratuitous_arp(flow)
2541 || mac_entry_is_grat_arp_locked(mac))) {
14a34d00 2542 return false;
f620b43a
BP
2543 }
2544 break;
3a55ef14 2545 }
064af421
BP
2546 }
2547
14a34d00
BP
2548 return true;
2549}
2550
2551/* If the composed actions may be applied to any packet in the given 'flow',
2552 * returns true. Otherwise, the actions should only be applied to 'packet', or
2553 * not at all, if 'packet' was NULL. */
2554static bool
ae412e7d 2555process_flow(struct bridge *br, const struct flow *flow,
cdee00fd 2556 const struct ofpbuf *packet, struct ofpbuf *actions,
14a34d00
BP
2557 tag_type *tags, uint16_t *nf_output_iface)
2558{
2559 struct port *in_port;
2560 struct port *out_port;
db8077c3 2561 struct mac_entry *mac;
14a34d00 2562 int vlan;
14a34d00
BP
2563
2564 /* Check whether we should drop packets in this flow. */
2565 if (!is_admissible(br, flow, packet != NULL, tags, &vlan, &in_port)) {
2566 out_port = NULL;
2567 goto done;
2568 }
2569
93dfc067
JG
2570 /* Learn source MAC (but don't try to learn from revalidation). */
2571 if (packet) {
e96a4d80 2572 update_learning_table(br, flow, vlan, in_port);
93dfc067
JG
2573 }
2574
2575 /* Determine output port. */
db8077c3 2576 mac = mac_learning_lookup(br->ml, flow->dl_dst, vlan, tags);
1648ddd7 2577 if (mac) {
bbb1951c 2578 out_port = mac->port.p;
e96a4d80 2579 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
93dfc067 2580 /* If we are revalidating but don't have a learning entry then
e96a4d80
JG
2581 * eject the flow. Installing a flow that floods packets opens
2582 * up a window of time where we could learn from a packet reflected
2583 * on a bond and blackhole packets before the learning table is
2584 * updated to reflect the correct port. */
93dfc067 2585 return false;
1609aa03
BP
2586 } else {
2587 out_port = FLOOD_PORT;
064af421
BP
2588 }
2589
ba54bf4f
BP
2590 /* Don't send packets out their input ports. */
2591 if (in_port == out_port) {
064af421
BP
2592 out_port = NULL;
2593 }
2594
2595done:
14a34d00
BP
2596 if (in_port) {
2597 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2598 nf_output_iface);
2599 }
064af421 2600
69d60f9f 2601 return true;
064af421
BP
2602}
2603
064af421 2604static bool
ae412e7d 2605bridge_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
cdee00fd 2606 struct ofpbuf *actions, tag_type *tags,
6a07af36 2607 uint16_t *nf_output_iface, void *br_)
064af421
BP
2608{
2609 struct bridge *br = br_;
2610
064af421 2611 COVERAGE_INC(bridge_process_flow);
ebe482fd
EJ
2612 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2613}
2614
2615static bool
2616bridge_special_ofhook_cb(const struct flow *flow,
2617 const struct ofpbuf *packet, void *br_)
2618{
2619 struct iface *iface;
2620 struct bridge *br = br_;
26d79bf2 2621
b31bcf60
EJ
2622 iface = iface_from_dp_ifidx(br, flow->in_port);
2623
e7934396 2624 if (flow->dl_type == htons(ETH_TYPE_LACP)) {
f620b43a
BP
2625 if (iface && iface->port->bond && packet) {
2626 bond_process_lacp(iface->port->bond, iface, packet);
c25c91fd
EJ
2627 }
2628 return false;
b31bcf60
EJ
2629 }
2630
ebe482fd 2631 return true;
064af421
BP
2632}
2633
2634static void
ae412e7d 2635bridge_account_flow_ofhook_cb(const struct flow *flow, tag_type tags,
cdee00fd 2636 const struct nlattr *actions,
cf22f8cb 2637 size_t actions_len,
7006c033 2638 uint64_t n_bytes, void *br_)
064af421
BP
2639{
2640 struct bridge *br = br_;
cdee00fd 2641 const struct nlattr *a;
db0e2ad1 2642 struct port *in_port;
26d79bf2 2643 tag_type dummy = 0;
cdee00fd 2644 unsigned int left;
db0e2ad1 2645 int vlan;
064af421 2646
26d79bf2
BP
2647 /* Feed information from the active flows back into the learning table to
2648 * ensure that table is always in sync with what is actually flowing
2649 * through the datapath.
2650 *
2651 * We test that 'tags' is nonzero to ensure that only flows that include an
2652 * OFPP_NORMAL action are used for learning. This works because
2653 * bridge_normal_ofhook_cb() always sets a nonzero tag value. */
2654 if (tags && is_admissible(br, flow, false, &dummy, &vlan, &in_port)) {
db0e2ad1 2655 update_learning_table(br, flow, vlan, in_port);
e96a4d80
JG
2656 }
2657
26d79bf2 2658 /* Account for bond slave utilization. */
064af421
BP
2659 if (!br->has_bonded_ports) {
2660 return;
2661 }
cdee00fd 2662 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
7aec165d 2663 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
cdee00fd 2664 struct port *out_port = port_from_dp_ifidx(br, nl_attr_get_u32(a));
f620b43a 2665 if (out_port && out_port->bond) {
e58de0e3
EJ
2666 uint16_t vlan = (flow->vlan_tci
2667 ? vlan_tci_to_vid(flow->vlan_tci)
2668 : OFP_VLAN_NONE);
f620b43a 2669 bond_account(out_port->bond, flow, vlan, n_bytes);
064af421
BP
2670 }
2671 }
2672 }
2673}
2674
2675static void
2676bridge_account_checkpoint_ofhook_cb(void *br_)
2677{
2678 struct bridge *br = br_;
8052fb14 2679 struct port *port;
064af421 2680
8052fb14 2681 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
f620b43a
BP
2682 if (port->bond) {
2683 bond_rebalance(port->bond,
2684 ofproto_get_revalidate_set(br->ofproto));
064af421
BP
2685 }
2686 }
2687}
2688
2689static struct ofhooks bridge_ofhooks = {
064af421 2690 bridge_normal_ofhook_cb,
ebe482fd 2691 bridge_special_ofhook_cb,
064af421
BP
2692 bridge_account_flow_ofhook_cb,
2693 bridge_account_checkpoint_ofhook_cb,
2694};
2695\f
f620b43a 2696/* Port functions. */
be02e7c3 2697
f620b43a
BP
2698static void
2699port_run(struct port *port)
2700{
2701 if (port->bond) {
2702 bond_run(port->bond,
2703 ofproto_get_revalidate_set(port->bridge->ofproto));
2704 if (bond_should_send_learning_packets(port->bond)) {
2705 port_send_learning_packets(port);
2706 }
be02e7c3 2707 }
be02e7c3
EJ
2708}
2709
f620b43a
BP
2710static void
2711port_wait(struct port *port)
064af421 2712{
f620b43a
BP
2713 if (port->bond) {
2714 bond_wait(port->bond);
064af421
BP
2715 }
2716}
2717
f620b43a
BP
2718static struct port *
2719port_create(struct bridge *br, const char *name)
064af421 2720{
f620b43a
BP
2721 struct port *port;
2722
2723 port = xzalloc(sizeof *port);
2724 port->bridge = br;
2725 port->vlan = -1;
2726 port->trunks = NULL;
2727 port->name = xstrdup(name);
2728 list_init(&port->ifaces);
2729
2730 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
2731
2732 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
2733 bridge_flush(br);
2734
2735 return port;
064af421
BP
2736}
2737
f620b43a
BP
2738static const char *
2739get_port_other_config(const struct ovsrec_port *port, const char *key,
2740 const char *default_value)
064af421 2741{
f620b43a
BP
2742 const char *value;
2743
2744 value = get_ovsrec_key_value(&port->header_, &ovsrec_port_col_other_config,
2745 key);
2746 return value ? value : default_value;
064af421
BP
2747}
2748
f620b43a
BP
2749static const char *
2750get_interface_other_config(const struct ovsrec_interface *iface,
2751 const char *key, const char *default_value)
064af421 2752{
f620b43a
BP
2753 const char *value;
2754
2755 value = get_ovsrec_key_value(&iface->header_,
2756 &ovsrec_interface_col_other_config, key);
2757 return value ? value : default_value;
064af421
BP
2758}
2759
2760static void
f620b43a 2761port_del_ifaces(struct port *port, const struct ovsrec_port *cfg)
064af421 2762{
f620b43a
BP
2763 struct iface *iface, *next;
2764 struct sset new_ifaces;
2765 size_t i;
064af421 2766
f620b43a
BP
2767 /* Collect list of new interfaces. */
2768 sset_init(&new_ifaces);
2769 for (i = 0; i < cfg->n_interfaces; i++) {
2770 const char *name = cfg->interfaces[i]->name;
2771 sset_add(&new_ifaces, name);
2772 }
064af421 2773
f620b43a
BP
2774 /* Get rid of deleted interfaces. */
2775 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2776 if (!sset_contains(&new_ifaces, iface->name)) {
2777 iface_destroy(iface);
064af421 2778 }
064af421 2779 }
f620b43a
BP
2780
2781 sset_destroy(&new_ifaces);
064af421
BP
2782}
2783
f620b43a
BP
2784/* Expires all MAC learning entries associated with 'port' and forces ofproto
2785 * to revalidate every flow. */
064af421 2786static void
f620b43a 2787port_flush_macs(struct port *port)
064af421 2788{
f620b43a
BP
2789 struct bridge *br = port->bridge;
2790 struct mac_learning *ml = br->ml;
2791 struct mac_entry *mac, *next_mac;
064af421 2792
f620b43a
BP
2793 bridge_flush(br);
2794 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2795 if (mac->port.p == port) {
2796 mac_learning_expire(ml, mac);
2797 }
064af421 2798 }
064af421
BP
2799}
2800
2801static void
f620b43a 2802port_reconfigure(struct port *port, const struct ovsrec_port *cfg)
064af421 2803{
f620b43a
BP
2804 struct sset new_ifaces;
2805 bool need_flush = false;
2806 unsigned long *trunks;
2807 int vlan;
064af421
BP
2808 size_t i;
2809
f620b43a 2810 port->cfg = cfg;
be02e7c3 2811
be02e7c3 2812
4a1ee6ae 2813 /* Add new interfaces and update 'cfg' member of existing ones. */
b3c01ed3 2814 sset_init(&new_ifaces);
4a1ee6ae
BP
2815 for (i = 0; i < cfg->n_interfaces; i++) {
2816 const struct ovsrec_interface *if_cfg = cfg->interfaces[i];
76343538
BP
2817 struct iface *iface;
2818
b3c01ed3 2819 if (!sset_add(&new_ifaces, if_cfg->name)) {
4a1ee6ae
BP
2820 VLOG_WARN("port %s: %s specified twice as port interface",
2821 port->name, if_cfg->name);
bcd49a45 2822 iface_set_ofport(if_cfg, -1);
4a1ee6ae
BP
2823 continue;
2824 }
2825
2826 iface = iface_lookup(port->bridge, if_cfg->name);
2827 if (iface) {
2828 if (iface->port != port) {
2829 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
2830 "removing from %s",
2831 port->bridge->name, if_cfg->name, iface->port->name);
2832 continue;
2833 }
149f577a 2834 iface->cfg = if_cfg;
4a1ee6ae 2835 } else {
6cefe1da 2836 iface = iface_create(port, if_cfg);
064af421 2837 }
6cefe1da
BP
2838
2839 /* Determine interface type. The local port always has type
2840 * "internal". Other ports take their type from the database and
2841 * default to "system" if none is specified. */
2842 iface->type = (!strcmp(if_cfg->name, port->bridge->name) ? "internal"
2843 : if_cfg->type[0] ? if_cfg->type
2844 : "system");
064af421 2845 }
b3c01ed3 2846 sset_destroy(&new_ifaces);
064af421
BP
2847
2848 /* Get VLAN tag. */
2849 vlan = -1;
76343538 2850 if (cfg->tag) {
402f2858 2851 if (list_is_short(&port->ifaces)) {
76343538 2852 vlan = *cfg->tag;
064af421
BP
2853 if (vlan >= 0 && vlan <= 4095) {
2854 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
76343538
BP
2855 } else {
2856 vlan = -1;
064af421
BP
2857 }
2858 } else {
2859 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
2860 * they even work as-is. But they have not been tested. */
2861 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
2862 port->name);
2863 }
2864 }
2865 if (port->vlan != vlan) {
2866 port->vlan = vlan;
d5346278 2867 need_flush = true;
064af421
BP
2868 }
2869
2870 /* Get trunked VLANs. */
2871 trunks = NULL;
3e9c481c 2872 if (vlan < 0 && cfg->n_trunks) {
76343538 2873 size_t n_errors;
064af421
BP
2874
2875 trunks = bitmap_allocate(4096);
064af421 2876 n_errors = 0;
76343538
BP
2877 for (i = 0; i < cfg->n_trunks; i++) {
2878 int trunk = cfg->trunks[i];
064af421
BP
2879 if (trunk >= 0) {
2880 bitmap_set1(trunks, trunk);
2881 } else {
2882 n_errors++;
2883 }
2884 }
2885 if (n_errors) {
2886 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
76343538 2887 port->name, cfg->n_trunks);
064af421 2888 }
76343538 2889 if (n_errors == cfg->n_trunks) {
3e9c481c 2890 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
76343538 2891 port->name);
3e9c481c
BP
2892 bitmap_free(trunks);
2893 trunks = NULL;
064af421 2894 }
3e9c481c
BP
2895 } else if (vlan >= 0 && cfg->n_trunks) {
2896 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
2897 port->name);
064af421
BP
2898 }
2899 if (trunks == NULL
2900 ? port->trunks != NULL
2901 : port->trunks == NULL || !bitmap_equal(trunks, port->trunks, 4096)) {
d5346278 2902 need_flush = true;
064af421
BP
2903 }
2904 bitmap_free(port->trunks);
2905 port->trunks = trunks;
d5346278
BP
2906
2907 if (need_flush) {
2908 port_flush_macs(port);
2909 }
064af421
BP
2910}
2911
2912static void
2913port_destroy(struct port *port)
2914{
2915 if (port) {
2916 struct bridge *br = port->bridge;
83db7968 2917 struct iface *iface, *next;
37e7f427 2918 int i;
064af421 2919
064af421
BP
2920 for (i = 0; i < MAX_MIRRORS; i++) {
2921 struct mirror *m = br->mirrors[i];
2922 if (m && m->out_port == port) {
2923 mirror_destroy(m);
2924 }
2925 }
2926
83db7968
BP
2927 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2928 iface_destroy(iface);
064af421
BP
2929 }
2930
8052fb14 2931 hmap_remove(&br->ports, &port->hmap_node);
064af421 2932
99707a7a
JP
2933 VLOG_INFO("destroyed port %s on bridge %s", port->name, br->name);
2934
d5346278
BP
2935 port_flush_macs(port);
2936
064af421
BP
2937 bitmap_free(port->trunks);
2938 free(port->name);
2939 free(port);
064af421
BP
2940 }
2941}
2942
2943static struct port *
2944port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
2945{
2946 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
2947 return iface ? iface->port : NULL;
2948}
2949
2950static struct port *
2951port_lookup(const struct bridge *br, const char *name)
2952{
8052fb14
BP
2953 struct port *port;
2954
2955 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
2956 &br->ports) {
2957 if (!strcmp(port->name, name)) {
2958 return port;
2959 }
2960 }
2961 return NULL;
064af421
BP
2962}
2963
7a673515
BP
2964static bool
2965enable_lacp(struct port *port, bool *activep)
2966{
2967 if (!port->cfg->lacp) {
2968 /* XXX when LACP implementation has been sufficiently tested, enable by
2969 * default and make active on bonded ports. */
2970 return false;
2971 } else if (!strcmp(port->cfg->lacp, "off")) {
2972 return false;
2973 } else if (!strcmp(port->cfg->lacp, "active")) {
2974 *activep = true;
2975 return true;
2976 } else if (!strcmp(port->cfg->lacp, "passive")) {
2977 *activep = false;
2978 return true;
2979 } else {
2980 VLOG_WARN("port %s: unknown LACP mode %s",
2981 port->name, port->cfg->lacp);
2982 return false;
2983 }
2984}
2985
f620b43a
BP
2986static struct lacp_settings *
2987port_reconfigure_bond_lacp(struct port *port, struct lacp_settings *s)
2988{
2989 if (!enable_lacp(port, &s->active)) {
2990 return NULL;
2991 }
2992
2993 s->name = port->name;
2994 memcpy(s->id, port->bridge->ea, ETH_ADDR_LEN);
2995 s->priority = atoi(get_port_other_config(port->cfg, "lacp-system-priority",
2996 "0"));
2997 s->fast = !strcmp(get_port_other_config(port->cfg, "lacp-time", "slow"),
2998 "fast");
2999
3000 if (s->priority <= 0 || s->priority > UINT16_MAX) {
3001 /* Prefer bondable links if unspecified. */
402f2858 3002 s->priority = UINT16_MAX - !list_is_short(&port->ifaces);
f620b43a
BP
3003 }
3004 return s;
3005}
3006
bb5bc6c0 3007static void
f620b43a 3008iface_reconfigure_bond(struct iface *iface)
bb5bc6c0
BP
3009{
3010 struct lacp_slave_settings s;
3011 int priority;
3012
3013 s.name = iface->name;
3014 s.id = iface->dp_ifidx;
3015 priority = atoi(get_interface_other_config(
3016 iface->cfg, "lacp-port-priority", "0"));
f620b43a
BP
3017 s.priority = (priority >= 0 && priority <= UINT16_MAX
3018 ? priority : UINT16_MAX);
3019 bond_slave_register(iface->port->bond, iface, iface->netdev, &s);
bb5bc6c0
BP
3020}
3021
c25c91fd 3022static void
f620b43a 3023port_reconfigure_bond(struct port *port)
c25c91fd 3024{
f620b43a
BP
3025 struct lacp_settings lacp_settings;
3026 struct bond_settings s;
3027 const char *detect_s;
7a673515 3028 struct iface *iface;
c25c91fd 3029
402f2858 3030 if (list_is_short(&port->ifaces)) {
f620b43a
BP
3031 /* Not a bonded port. */
3032 bond_destroy(port->bond);
3033 port->bond = NULL;
7a673515
BP
3034 return;
3035 }
c25c91fd 3036
f620b43a 3037 port->bridge->has_bonded_ports = true;
7a673515 3038
bb5bc6c0 3039 s.name = port->name;
f620b43a
BP
3040 s.balance = BM_SLB;
3041 if (port->cfg->bond_mode
3042 && !bond_mode_from_string(&s.balance, port->cfg->bond_mode)) {
3043 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
3044 port->name, port->cfg->bond_mode,
3045 bond_mode_to_string(s.balance));
3046 }
bb5bc6c0 3047
f620b43a
BP
3048 s.detect = BLSM_CARRIER;
3049 detect_s = get_port_other_config(port->cfg, "bond-detect-mode", NULL);
3050 if (detect_s && !bond_detect_mode_from_string(&s.detect, detect_s)) {
3051 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
3052 "defaulting to %s",
3053 port->name, detect_s, bond_detect_mode_to_string(s.detect));
3054 }
3055
3056 s.miimon_interval = atoi(
3057 get_port_other_config(port->cfg, "bond-miimon-interval", "200"));
3058 if (s.miimon_interval < 100) {
3059 s.miimon_interval = 100;
3060 }
3061
3062 s.up_delay = MAX(0, port->cfg->bond_updelay);
3063 s.down_delay = MAX(0, port->cfg->bond_downdelay);
3064 s.rebalance_interval = atoi(
3065 get_port_other_config(port->cfg, "bond-rebalance-interval", "10000"));
3066 if (s.rebalance_interval < 1000) {
3067 s.rebalance_interval = 1000;
3068 }
3069
3070 s.fake_iface = port->cfg->bond_fake_iface;
3071 s.lacp = port_reconfigure_bond_lacp(port, &lacp_settings);
3072
3073 if (!port->bond) {
3074 port->bond = bond_create(&s);
3075 } else {
3076 bond_reconfigure(port->bond, &s);
7a673515
BP
3077 }
3078
7a673515 3079 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
f620b43a 3080 iface_reconfigure_bond(iface);
c25c91fd 3081 }
c25c91fd
EJ
3082}
3083
064af421 3084static void
f620b43a 3085port_send_learning_packets(struct port *port)
064af421 3086{
f620b43a
BP
3087 struct bridge *br = port->bridge;
3088 int error, n_packets, n_errors;
3089 struct mac_entry *e;
064af421 3090
f620b43a
BP
3091 error = n_packets = n_errors = 0;
3092 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
3093 if (e->port.p != port) {
3094 int ret = bond_send_learning_packet(port->bond, e->mac, e->vlan);
3095 if (ret) {
3096 error = ret;
3097 n_errors++;
064af421 3098 }
f620b43a 3099 n_packets++;
55eccb86 3100 }
f620b43a 3101 }
0bb5c3ec 3102
f620b43a
BP
3103 if (n_errors) {
3104 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3105 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3106 "packets, last error was: %s",
3107 port->name, n_errors, n_packets, strerror(error));
3108 } else {
3109 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3110 port->name, n_packets);
064af421
BP
3111 }
3112}
064af421
BP
3113\f
3114/* Interface functions. */
3115
76343538 3116static struct iface *
a740f0de 3117iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
064af421 3118{
4a1ee6ae 3119 struct bridge *br = port->bridge;
064af421 3120 struct iface *iface;
a740f0de 3121 char *name = if_cfg->name;
064af421 3122
ec6fde61 3123 iface = xzalloc(sizeof *iface);
064af421 3124 iface->port = port;
064af421
BP
3125 iface->name = xstrdup(name);
3126 iface->dp_ifidx = -1;
3127 iface->tag = tag_create_random();
0c6aea3f 3128 iface->netdev = NULL;
149f577a 3129 iface->cfg = if_cfg;
064af421 3130
2457b24f
JG
3131 shash_add_assert(&br->iface_by_name, iface->name, iface);
3132
83db7968 3133 list_push_back(&port->ifaces, &iface->port_elem);
83db7968 3134
064af421
BP
3135 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
3136
4a1ee6ae 3137 bridge_flush(br);
76343538
BP
3138
3139 return iface;
064af421
BP
3140}
3141
3142static void
3143iface_destroy(struct iface *iface)
3144{
3145 if (iface) {
3146 struct port *port = iface->port;
3147 struct bridge *br = port->bridge;
c17f0d5e 3148
f620b43a
BP
3149 if (port->bond) {
3150 bond_slave_unregister(port->bond, iface);
7ca5f243
EJ
3151 }
3152
4a1ee6ae
BP
3153 shash_find_and_delete_assert(&br->iface_by_name, iface->name);
3154
064af421 3155 if (iface->dp_ifidx >= 0) {
d9a8717a 3156 hmap_remove(&br->ifaces, &iface->dp_ifidx_node);
064af421
BP
3157 }
3158
83db7968 3159 list_remove(&iface->port_elem);
064af421 3160
0c6aea3f 3161 netdev_close(iface->netdev);
064af421 3162
a740f0de
JG
3163 free(iface->name);
3164 free(iface);
3165
064af421
BP
3166 bridge_flush(port->bridge);
3167 }
3168}
3169
3170static struct iface *
3171iface_lookup(const struct bridge *br, const char *name)
3172{
4a1ee6ae 3173 return shash_find_data(&br->iface_by_name, name);
064af421
BP
3174}
3175
e8fe3026
EJ
3176static struct iface *
3177iface_find(const char *name)
3178{
3179 const struct bridge *br;
3180
3181 LIST_FOR_EACH (br, node, &all_bridges) {
3182 struct iface *iface = iface_lookup(br, name);
3183
3184 if (iface) {
3185 return iface;
3186 }
3187 }
3188 return NULL;
3189}
3190
064af421
BP
3191static struct iface *
3192iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3193{
d9a8717a
BP
3194 struct iface *iface;
3195
4e8e4213 3196 HMAP_FOR_EACH_IN_BUCKET (iface, dp_ifidx_node,
d9a8717a
BP
3197 hash_int(dp_ifidx, 0), &br->ifaces) {
3198 if (iface->dp_ifidx == dp_ifidx) {
3199 return iface;
3200 }
3201 }
3202 return NULL;
064af421 3203}
557d8e6c 3204
52df17e7
BP
3205/* Set Ethernet address of 'iface', if one is specified in the configuration
3206 * file. */
3207static void
3208iface_set_mac(struct iface *iface)
3209{
76343538 3210 uint8_t ea[ETH_ADDR_LEN];
52df17e7 3211
76343538 3212 if (iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
52df17e7
BP
3213 if (eth_addr_is_multicast(ea)) {
3214 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3215 iface->name);
3216 } else if (iface->dp_ifidx == ODPP_LOCAL) {
3217 VLOG_ERR("ignoring iface.%s.mac; use bridge.%s.mac instead",
3218 iface->name, iface->name);
3219 } else {
4d678233 3220 int error = netdev_set_etheraddr(iface->netdev, ea);
52df17e7
BP
3221 if (error) {
3222 VLOG_ERR("interface %s: setting MAC failed (%s)",
3223 iface->name, strerror(error));
3224 }
3225 }
3226 }
3227}
c1c9c9c4 3228
bcd49a45
BP
3229/* Sets the ofport column of 'if_cfg' to 'ofport'. */
3230static void
3231iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
3232{
3233 if (if_cfg) {
3234 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
3235 }
3236}
3237
43776b8f
BP
3238/* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
3239 *
3240 * The value strings in '*shash' are taken directly from values[], not copied,
3241 * so the caller should not modify or free them. */
c1c9c9c4
BP
3242static void
3243shash_from_ovs_idl_map(char **keys, char **values, size_t n,
3244 struct shash *shash)
3245{
3246 size_t i;
3247
3248 shash_init(shash);
3249 for (i = 0; i < n; i++) {
3250 shash_add(shash, keys[i], values[i]);
3251 }
3252}
3253
ea763e0e
EJ
3254/* Creates 'keys' and 'values' arrays from 'shash'.
3255 *
3256 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
3257 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
3258 * are populated with with strings taken directly from 'shash' and thus have
3259 * the same ownership of the key-value pairs in shash.
3260 */
3261static void
3262shash_to_ovs_idl_map(struct shash *shash,
3263 char ***keys, char ***values, size_t *n)
3264{
3265 size_t i, count;
3266 char **k, **v;
3267 struct shash_node *sn;
3268
3269 count = shash_count(shash);
3270
3271 k = xmalloc(count * sizeof *k);
3272 v = xmalloc(count * sizeof *v);
3273
3274 i = 0;
3275 SHASH_FOR_EACH(sn, shash) {
3276 k[i] = sn->name;
3277 v[i] = sn->data;
3278 i++;
3279 }
3280
3281 *n = count;
3282 *keys = k;
3283 *values = v;
3284}
3285
c1c9c9c4
BP
3286struct iface_delete_queues_cbdata {
3287 struct netdev *netdev;
44fca7f9 3288 const struct ovsdb_datum *queues;
c1c9c9c4
BP
3289};
3290
3291static bool
44fca7f9 3292queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
c1c9c9c4 3293{
44fca7f9 3294 union ovsdb_atom atom;
c1c9c9c4 3295
44fca7f9
BP
3296 atom.integer = target;
3297 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
c1c9c9c4
BP
3298}
3299
3300static void
3301iface_delete_queues(unsigned int queue_id,
3302 const struct shash *details OVS_UNUSED, void *cbdata_)
3303{
3304 struct iface_delete_queues_cbdata *cbdata = cbdata_;
3305
44fca7f9 3306 if (!queue_ids_include(cbdata->queues, queue_id)) {
c1c9c9c4
BP
3307 netdev_delete_queue(cbdata->netdev, queue_id);
3308 }
3309}
3310
3311static void
3312iface_update_qos(struct iface *iface, const struct ovsrec_qos *qos)
3313{
3314 if (!qos || qos->type[0] == '\0') {
3315 netdev_set_qos(iface->netdev, NULL, NULL);
3316 } else {
3317 struct iface_delete_queues_cbdata cbdata;
3318 struct shash details;
3319 size_t i;
3320
3321 /* Configure top-level Qos for 'iface'. */
3322 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
3323 qos->n_other_config, &details);
3324 netdev_set_qos(iface->netdev, qos->type, &details);
3325 shash_destroy(&details);
3326
3327 /* Deconfigure queues that were deleted. */
3328 cbdata.netdev = iface->netdev;
44fca7f9
BP
3329 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
3330 OVSDB_TYPE_UUID);
c1c9c9c4
BP
3331 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
3332
3333 /* Configure queues for 'iface'. */
3334 for (i = 0; i < qos->n_queues; i++) {
3335 const struct ovsrec_queue *queue = qos->value_queues[i];
3336 unsigned int queue_id = qos->key_queues[i];
3337
3338 shash_from_ovs_idl_map(queue->key_other_config,
3339 queue->value_other_config,
3340 queue->n_other_config, &details);
3341 netdev_set_queue(iface->netdev, queue_id, &details);
3342 shash_destroy(&details);
3343 }
3344 }
3345}
b31bcf60
EJ
3346
3347static void
3348iface_update_cfm(struct iface *iface)
3349{
3350 size_t i;
e7934396 3351 struct cfm cfm;
b31bcf60
EJ
3352 uint16_t *remote_mps;
3353 struct ovsrec_monitor *mon;
a58727fb 3354 uint8_t maid[CCM_MAID_LEN];
b31bcf60
EJ
3355
3356 mon = iface->cfg->monitor;
3357
3358 if (!mon) {
e7934396 3359 ofproto_iface_clear_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
b31bcf60
EJ
3360 return;
3361 }
3362
b31bcf60
EJ
3363 if (!cfm_generate_maid(mon->md_name, mon->ma_name, maid)) {
3364 VLOG_WARN("interface %s: Failed to generate MAID.", iface->name);
3365 return;
3366 }
3367
e7934396
BP
3368 cfm.mpid = mon->mpid;
3369 cfm.interval = mon->interval ? *mon->interval : 1000;
b31bcf60 3370
e7934396 3371 memcpy(cfm.maid, maid, sizeof cfm.maid);
b31bcf60
EJ
3372
3373 remote_mps = xzalloc(mon->n_remote_mps * sizeof *remote_mps);
3374 for(i = 0; i < mon->n_remote_mps; i++) {
3375 remote_mps[i] = mon->remote_mps[i]->mpid;
3376 }
b31bcf60 3377
e7934396
BP
3378 ofproto_iface_set_cfm(iface->port->bridge->ofproto, iface->dp_ifidx,
3379 &cfm, remote_mps, mon->n_remote_mps);
3380 free(remote_mps);
b31bcf60 3381}
0b8024eb
BP
3382
3383/* Read carrier or miimon status directly from 'iface''s netdev, according to
3384 * how 'iface''s port is configured.
3385 *
3386 * Returns true if 'iface' is up, false otherwise. */
3387static bool
3388iface_get_carrier(const struct iface *iface)
3389{
f620b43a
BP
3390 /* XXX */
3391 return netdev_get_carrier(iface->netdev);
0b8024eb 3392}
064af421
BP
3393\f
3394/* Port mirroring. */
3395
dd0d105c
BP
3396static struct mirror *
3397mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
3398{
3399 int i;
3400
3401 for (i = 0; i < MAX_MIRRORS; i++) {
3402 struct mirror *m = br->mirrors[i];
3403 if (m && uuid_equals(uuid, &m->uuid)) {
3404 return m;
3405 }
3406 }
3407 return NULL;
3408}
3409
064af421 3410static void
37e7f427 3411mirror_reconfigure(struct bridge *br)
064af421 3412{
f2d7fd66 3413 unsigned long *rspan_vlans;
8052fb14 3414 struct port *port;
37e7f427 3415 int i;
064af421 3416
dd0d105c 3417 /* Get rid of deleted mirrors. */
064af421 3418 for (i = 0; i < MAX_MIRRORS; i++) {
dd0d105c
BP
3419 struct mirror *m = br->mirrors[i];
3420 if (m) {
3421 const struct ovsdb_datum *mc;
3422 union ovsdb_atom atom;
3423
3424 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
3425 atom.uuid = br->mirrors[i]->uuid;
3426 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
3427 mirror_destroy(m);
3428 }
064af421
BP
3429 }
3430 }
3431
dd0d105c 3432 /* Add new mirrors and reconfigure existing ones. */
37e7f427
BP
3433 for (i = 0; i < br->cfg->n_mirrors; i++) {
3434 struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
dd0d105c
BP
3435 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
3436 if (m) {
3437 mirror_reconfigure_one(m, cfg);
3438 } else {
3439 mirror_create(br, cfg);
064af421
BP
3440 }
3441 }
3442
3443 /* Update port reserved status. */
8052fb14
BP
3444 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3445 port->is_mirror_output_port = false;
064af421
BP
3446 }
3447 for (i = 0; i < MAX_MIRRORS; i++) {
3448 struct mirror *m = br->mirrors[i];
3449 if (m && m->out_port) {
3450 m->out_port->is_mirror_output_port = true;
3451 }
3452 }
f2d7fd66 3453
8f30d09a 3454 /* Update flooded vlans (for RSPAN). */
f2d7fd66 3455 rspan_vlans = NULL;
37e7f427 3456 if (br->cfg->n_flood_vlans) {
f2d7fd66
JG
3457 rspan_vlans = bitmap_allocate(4096);
3458
37e7f427
BP
3459 for (i = 0; i < br->cfg->n_flood_vlans; i++) {
3460 int64_t vlan = br->cfg->flood_vlans[i];
3461 if (vlan >= 0 && vlan < 4096) {
f2d7fd66 3462 bitmap_set1(rspan_vlans, vlan);
37e7f427 3463 VLOG_INFO("bridge %s: disabling learning on vlan %"PRId64,
42061b2a 3464 br->name, vlan);
f2d7fd66 3465 } else {
37e7f427
BP
3466 VLOG_ERR("bridge %s: invalid value %"PRId64 "for flood VLAN",
3467 br->name, vlan);
f2d7fd66
JG
3468 }
3469 }
3470 }
8f30d09a 3471 if (mac_learning_set_flood_vlans(br->ml, rspan_vlans)) {
f2d7fd66 3472 bridge_flush(br);
d5346278 3473 mac_learning_flush(br->ml);
f2d7fd66 3474 }
064af421
BP
3475}
3476
dd0d105c
BP
3477static void
3478mirror_create(struct bridge *br, struct ovsrec_mirror *cfg)
064af421
BP
3479{
3480 struct mirror *m;
3481 size_t i;
3482
3483 for (i = 0; ; i++) {
3484 if (i >= MAX_MIRRORS) {
3485 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
dd0d105c
BP
3486 "cannot create %s", br->name, MAX_MIRRORS, cfg->name);
3487 return;
064af421
BP
3488 }
3489 if (!br->mirrors[i]) {
3490 break;
3491 }
3492 }
3493
dd0d105c 3494 VLOG_INFO("created port mirror %s on bridge %s", cfg->name, br->name);
064af421 3495 bridge_flush(br);
d5346278 3496 mac_learning_flush(br->ml);
064af421 3497
ec6fde61 3498 br->mirrors[i] = m = xzalloc(sizeof *m);
064af421
BP
3499 m->bridge = br;
3500 m->idx = i;
dd0d105c 3501 m->name = xstrdup(cfg->name);
b3c01ed3
BP
3502 sset_init(&m->src_ports);
3503 sset_init(&m->dst_ports);
064af421
BP
3504 m->vlans = NULL;
3505 m->n_vlans = 0;
3506 m->out_vlan = -1;
3507 m->out_port = NULL;
37e7f427 3508
dd0d105c 3509 mirror_reconfigure_one(m, cfg);
064af421
BP
3510}
3511
3512static void
3513mirror_destroy(struct mirror *m)
3514{
3515 if (m) {
3516 struct bridge *br = m->bridge;
8052fb14 3517 struct port *port;
064af421 3518
8052fb14
BP
3519 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3520 port->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3521 port->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
064af421
BP
3522 }
3523
b3c01ed3
BP
3524 sset_destroy(&m->src_ports);
3525 sset_destroy(&m->dst_ports);
064af421
BP
3526 free(m->vlans);
3527
3528 m->bridge->mirrors[m->idx] = NULL;
786880a5 3529 free(m->name);
064af421
BP
3530 free(m);
3531
3532 bridge_flush(br);
d5346278 3533 mac_learning_flush(br->ml);
064af421
BP
3534 }
3535}
3536
3537static void
37e7f427 3538mirror_collect_ports(struct mirror *m, struct ovsrec_port **ports, int n_ports,
b3c01ed3 3539 struct sset *names)
064af421 3540{
064af421
BP
3541 size_t i;
3542
37e7f427
BP
3543 for (i = 0; i < n_ports; i++) {
3544 const char *name = ports[i]->name;
064af421 3545 if (port_lookup(m->bridge, name)) {
b3c01ed3 3546 sset_add(names, name);
064af421 3547 } else {
37e7f427
BP
3548 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
3549 "port %s", m->bridge->name, m->name, name);
064af421
BP
3550 }
3551 }
064af421
BP
3552}
3553
3554static size_t
37e7f427
BP
3555mirror_collect_vlans(struct mirror *m, const struct ovsrec_mirror *cfg,
3556 int **vlans)
064af421 3557{
37e7f427
BP
3558 size_t n_vlans;
3559 size_t i;
064af421 3560
0b523ea7 3561 *vlans = xmalloc(sizeof **vlans * cfg->n_select_vlan);
064af421 3562 n_vlans = 0;
37e7f427
BP
3563 for (i = 0; i < cfg->n_select_vlan; i++) {
3564 int64_t vlan = cfg->select_vlan[i];
3565 if (vlan < 0 || vlan > 4095) {
3566 VLOG_WARN("bridge %s: mirror %s selects invalid VLAN %"PRId64,
3567 m->bridge->name, m->name, vlan);
064af421
BP
3568 } else {
3569 (*vlans)[n_vlans++] = vlan;
3570 }
3571 }
3572 return n_vlans;
3573}
3574
3575static bool
3576vlan_is_mirrored(const struct mirror *m, int vlan)
3577{
3578 size_t i;
3579
3580 for (i = 0; i < m->n_vlans; i++) {
3581 if (m->vlans[i] == vlan) {
3582 return true;
3583 }
3584 }
3585 return false;
3586}
3587
3588static bool
3589port_trunks_any_mirrored_vlan(const struct mirror *m, const struct port *p)
3590{
3591 size_t i;
3592
3593 for (i = 0; i < m->n_vlans; i++) {
3594 if (port_trunks_vlan(p, m->vlans[i])) {
3595 return true;
3596 }
3597 }
3598 return false;
3599}
3600
3601static void
37e7f427 3602mirror_reconfigure_one(struct mirror *m, struct ovsrec_mirror *cfg)
064af421 3603{
b3c01ed3 3604 struct sset src_ports, dst_ports;
064af421 3605 mirror_mask_t mirror_bit;
064af421 3606 struct port *out_port;
8052fb14 3607 struct port *port;
064af421
BP
3608 int out_vlan;
3609 size_t n_vlans;
3610 int *vlans;
064af421 3611
dd0d105c
BP
3612 /* Set name. */
3613 if (strcmp(cfg->name, m->name)) {
3614 free(m->name);
3615 m->name = xstrdup(cfg->name);
3616 }
3617
064af421 3618 /* Get output port. */
37e7f427
BP
3619 if (cfg->output_port) {
3620 out_port = port_lookup(m->bridge, cfg->output_port->name);
064af421 3621 if (!out_port) {
37e7f427
BP
3622 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
3623 m->bridge->name, m->name);
064af421 3624 mirror_destroy(m);
064af421
BP
3625 return;
3626 }
3627 out_vlan = -1;
3628
37e7f427
BP
3629 if (cfg->output_vlan) {
3630 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
3631 "output vlan; ignoring output vlan",
3632 m->bridge->name, m->name);
064af421 3633 }
37e7f427 3634 } else if (cfg->output_vlan) {
064af421 3635 out_port = NULL;
37e7f427 3636 out_vlan = *cfg->output_vlan;
064af421 3637 } else {
37e7f427
BP
3638 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
3639 m->bridge->name, m->name);
064af421 3640 mirror_destroy(m);
064af421
BP
3641 return;
3642 }
3643
b3c01ed3
BP
3644 sset_init(&src_ports);
3645 sset_init(&dst_ports);
939ff267 3646 if (cfg->select_all) {
8052fb14 3647 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
b3c01ed3
BP
3648 sset_add(&src_ports, port->name);
3649 sset_add(&dst_ports, port->name);
939ff267
BP
3650 }
3651 vlans = NULL;
3652 n_vlans = 0;
3653 } else {
3654 /* Get ports, and drop duplicates and ports that don't exist. */
3655 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
3656 &src_ports);
3657 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
3658 &dst_ports);
064af421 3659
939ff267
BP
3660 /* Get all the vlans, and drop duplicate and invalid vlans. */
3661 n_vlans = mirror_collect_vlans(m, cfg, &vlans);
37e7f427 3662 }
064af421
BP
3663
3664 /* Update mirror data. */
b3c01ed3
BP
3665 if (!sset_equals(&m->src_ports, &src_ports)
3666 || !sset_equals(&m->dst_ports, &dst_ports)
064af421
BP
3667 || m->n_vlans != n_vlans
3668 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
3669 || m->out_port != out_port
3670 || m->out_vlan != out_vlan) {
3671 bridge_flush(m->bridge);
d5346278 3672 mac_learning_flush(m->bridge->ml);
064af421 3673 }
b3c01ed3
BP
3674 sset_swap(&m->src_ports, &src_ports);
3675 sset_swap(&m->dst_ports, &dst_ports);
064af421
BP
3676 free(m->vlans);
3677 m->vlans = vlans;
3678 m->n_vlans = n_vlans;
3679 m->out_port = out_port;
3680 m->out_vlan = out_vlan;
3681
064af421
BP
3682 /* Update ports. */
3683 mirror_bit = MIRROR_MASK_C(1) << m->idx;
8052fb14 3684 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
b3c01ed3 3685 if (sset_contains(&m->src_ports, port->name)
064af421
BP
3686 || (m->n_vlans
3687 && (!port->vlan
3688 ? port_trunks_any_mirrored_vlan(m, port)
3689 : vlan_is_mirrored(m, port->vlan)))) {
3690 port->src_mirrors |= mirror_bit;
3691 } else {
3692 port->src_mirrors &= ~mirror_bit;
3693 }
3694
b3c01ed3 3695 if (sset_contains(&m->dst_ports, port->name)) {
064af421
BP
3696 port->dst_mirrors |= mirror_bit;
3697 } else {
3698 port->dst_mirrors &= ~mirror_bit;
3699 }
3700 }
3701
3702 /* Clean up. */
b3c01ed3
BP
3703 sset_destroy(&src_ports);
3704 sset_destroy(&dst_ports);
064af421 3705}