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