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