1 /* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <asm/param.h>
25 #include <linux/genetlink.h>
26 #include <linux/rtnetlink.h>
30 #include <sys/types.h>
36 #include "command-line.h"
40 #include "dynamic-string.h"
41 #include "fatal-signal.h"
43 #include "leak-checker.h"
46 #include "netlink-socket.h"
48 #include "openvswitch/brcompat-netlink.h"
49 #include "ovsdb-idl.h"
51 #include "poll-loop.h"
53 #include "rtnetlink.h"
54 #include "rtnetlink-link.h"
61 #include "vswitchd/vswitch-idl.h"
63 VLOG_DEFINE_THIS_MODULE(brcompatd);
66 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
68 /* Actions to modify bridge compatibility configuration. */
76 static const char *parse_options(int argc, char *argv[]);
77 static void usage(void) NO_RETURN;
79 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
81 /* Maximum number of milliseconds to wait before pruning port entries that
82 * no longer exist. If set to zero, ports are never pruned. */
83 static int prune_timeout = 5000;
85 /* Shell command to execute (via popen()) to send a control command to the
86 * running ovs-vswitchd process. The string must contain one instance of %s,
87 * which is replaced by the control command. */
88 static char *appctl_command;
90 /* Netlink socket to bridge compatibility kernel module. */
91 static struct nl_sock *brc_sock;
93 /* The Generic Netlink family number used for bridge compatibility. */
94 static int brc_family;
96 static const struct nl_policy brc_multicast_policy[] = {
97 [BRC_GENL_A_MC_GROUP] = {.type = NL_A_U32 }
101 lookup_brc_multicast_group(int *multicast_group)
103 struct nl_sock *sock;
104 struct ofpbuf request, *reply;
105 struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
108 retval = nl_sock_create(NETLINK_GENERIC, &sock);
112 ofpbuf_init(&request, 0);
113 nl_msg_put_genlmsghdr(&request, 0, brc_family,
114 NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
115 retval = nl_sock_transact(sock, &request, &reply);
116 ofpbuf_uninit(&request);
118 nl_sock_destroy(sock);
121 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
122 brc_multicast_policy, attrs,
123 ARRAY_SIZE(brc_multicast_policy))) {
124 nl_sock_destroy(sock);
125 ofpbuf_delete(reply);
128 *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
129 nl_sock_destroy(sock);
130 ofpbuf_delete(reply);
135 /* Opens a socket for brcompat notifications. Returns 0 if successful,
136 * otherwise a positive errno value. */
138 brc_open(struct nl_sock **sock)
140 int multicast_group = 0;
143 retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
148 retval = lookup_brc_multicast_group(&multicast_group);
153 retval = nl_sock_create(NETLINK_GENERIC, sock);
158 retval = nl_sock_join_mcgroup(*sock, multicast_group);
160 nl_sock_destroy(*sock);
166 static const struct nl_policy brc_dp_policy[] = {
167 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
170 static struct ovsrec_bridge *
171 find_bridge(const struct ovsrec_open_vswitch *ovs, const char *br_name)
175 for (i = 0; i < ovs->n_bridges; i++) {
176 if (!strcmp(br_name, ovs->bridges[i]->name)) {
177 return ovs->bridges[i];
185 execute_appctl_command(const char *unixctl_command, char **output)
187 char *stdout_log, *stderr_log;
193 argv[2] = xasprintf(appctl_command, unixctl_command);
196 /* Run process and log status. */
197 error = process_run_capture(argv, &stdout_log, &stderr_log, 65536,
200 VLOG_ERR("failed to execute %s command via ovs-appctl: %s",
201 unixctl_command, strerror(error));
203 char *msg = process_status_msg(status);
204 VLOG_ERR("ovs-appctl exited with error (%s)", msg);
209 /* Deal with stdout_log. */
211 *output = stdout_log;
216 /* Deal with stderr_log */
217 if (stderr_log && *stderr_log) {
218 VLOG_INFO("ovs-appctl wrote to stderr:\n%s", stderr_log);
228 do_get_bridge_parts(const struct ovsrec_bridge *br, struct sset *parts,
229 int vlan, bool break_down_bonds)
233 for (i = 0; i < br->n_ports; i++) {
234 const struct ovsrec_port *port = br->ports[i];
237 int port_vlan = port->n_tag ? *port->tag : 0;
238 if (vlan != port_vlan) {
242 if (break_down_bonds) {
243 for (j = 0; j < port->n_interfaces; j++) {
244 const struct ovsrec_interface *iface = port->interfaces[j];
245 sset_add(parts, iface->name);
248 sset_add(parts, port->name);
253 /* Add all the interfaces for 'bridge' to 'ifaces', breaking bonded interfaces
254 * down into their constituent parts.
256 * If 'vlan' < 0, all interfaces on 'bridge' are reported. If 'vlan' == 0,
257 * then only interfaces for trunk ports or ports with implicit VLAN 0 are
258 * reported. If 'vlan' > 0, only interfaces with implicit VLAN 'vlan' are
261 get_bridge_ifaces(const struct ovsrec_bridge *br, struct sset *ifaces,
264 do_get_bridge_parts(br, ifaces, vlan, true);
267 /* Add all the ports for 'bridge' to 'ports'. Bonded ports are reported under
268 * the bond name, not broken down into their constituent interfaces.
270 * If 'vlan' < 0, all ports on 'bridge' are reported. If 'vlan' == 0, then
271 * only trunk ports or ports with implicit VLAN 0 are reported. If 'vlan' > 0,
272 * only port with implicit VLAN 'vlan' are reported. */
274 get_bridge_ports(const struct ovsrec_bridge *br, struct sset *ports,
277 do_get_bridge_parts(br, ports, vlan, false);
280 static struct ovsdb_idl_txn *
281 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
283 return ovsdb_idl_txn_get(&ovs->header_);
287 port_is_fake_bridge(const struct ovsrec_port *port)
289 return (port->fake_bridge
291 && *port->tag >= 1 && *port->tag <= 4095);
295 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
296 struct ovsrec_bridge *bridge)
298 struct ovsrec_bridge **bridges;
301 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
302 for (i = 0; i < ovs->n_bridges; i++) {
303 bridges[i] = ovs->bridges[i];
305 bridges[ovs->n_bridges] = bridge;
306 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
311 where_uuid_equals(const struct uuid *uuid)
316 json_string_create("_uuid"),
317 json_string_create("=="),
319 json_string_create("uuid"),
320 json_string_create_nocopy(
321 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
324 /* Commits 'txn'. If 'wait_for_reload' is true, also waits for Open vSwitch to
325 reload the configuration before returning.
327 Returns EAGAIN if the caller should try the operation again, 0 on success,
328 otherwise a positive errno value. */
330 commit_txn(struct ovsdb_idl_txn *txn, bool wait_for_reload)
332 struct ovsdb_idl *idl = ovsdb_idl_txn_get_idl (txn);
333 enum ovsdb_idl_txn_status status;
334 int64_t next_cfg = 0;
336 if (wait_for_reload) {
337 const struct ovsrec_open_vswitch *ovs = ovsrec_open_vswitch_first(idl);
338 struct json *where = where_uuid_equals(&ovs->header_.uuid);
339 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
342 status = ovsdb_idl_txn_commit_block(txn);
343 if (wait_for_reload && status == TXN_SUCCESS) {
344 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
346 ovsdb_idl_txn_destroy(txn);
353 VLOG_ERR_RL(&rl, "OVSDB transaction unexpectedly aborted");
360 if (wait_for_reload) {
362 /* We can't use 'ovs' any longer because ovsdb_idl_run() can
364 const struct ovsrec_open_vswitch *ovs2;
367 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs2, idl) {
368 if (ovs2->cur_cfg >= next_cfg) {
380 VLOG_ERR_RL(&rl, "OVSDB transaction needs retry");
384 VLOG_ERR_RL(&rl, "OVSDB transaction failed: %s",
385 ovsdb_idl_txn_get_error(txn));
394 add_bridge(struct ovsdb_idl *idl, const struct ovsrec_open_vswitch *ovs,
397 struct ovsrec_bridge *br;
398 struct ovsrec_port *port;
399 struct ovsrec_interface *iface;
400 struct ovsdb_idl_txn *txn;
402 if (find_bridge(ovs, br_name)) {
403 VLOG_WARN("addbr %s: bridge %s exists", br_name, br_name);
405 } else if (netdev_exists(br_name)) {
408 for (i = 0; i < ovs->n_bridges; i++) {
410 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
412 for (j = 0; j < br_cfg->n_ports; j++) {
413 if (port_is_fake_bridge(br_cfg->ports[j])) {
414 VLOG_WARN("addbr %s: %s exists as a fake bridge",
421 VLOG_WARN("addbr %s: cannot create bridge %s because a network "
422 "device named %s already exists",
423 br_name, br_name, br_name);
427 txn = ovsdb_idl_txn_create(idl);
429 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: addbr %s", br_name);
431 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
432 ovsrec_interface_set_name(iface, br_name);
434 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
435 ovsrec_port_set_name(port, br_name);
436 ovsrec_port_set_interfaces(port, &iface, 1);
438 br = ovsrec_bridge_insert(txn_from_openvswitch(ovs));
439 ovsrec_bridge_set_name(br, br_name);
440 ovsrec_bridge_set_ports(br, &port, 1);
442 ovs_insert_bridge(ovs, br);
444 return commit_txn(txn, true);
448 add_port(const struct ovsrec_open_vswitch *ovs,
449 const struct ovsrec_bridge *br, const char *port_name)
451 struct ovsrec_interface *iface;
452 struct ovsrec_port *port;
453 struct ovsrec_port **ports;
456 /* xxx Check conflicts? */
457 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
458 ovsrec_interface_set_name(iface, port_name);
460 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
461 ovsrec_port_set_name(port, port_name);
462 ovsrec_port_set_interfaces(port, &iface, 1);
464 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
465 for (i = 0; i < br->n_ports; i++) {
466 ports[i] = br->ports[i];
468 ports[br->n_ports] = port;
469 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
473 /* Deletes 'port' from 'br'.
475 * After calling this function, 'port' must not be referenced again. */
477 del_port(const struct ovsrec_bridge *br, const struct ovsrec_port *port)
479 struct ovsrec_port **ports;
482 /* Remove 'port' from the bridge's list of ports. */
483 ports = xmalloc(sizeof *br->ports * br->n_ports);
484 for (i = n = 0; i < br->n_ports; i++) {
485 if (br->ports[i] != port) {
486 ports[n++] = br->ports[i];
489 ovsrec_bridge_set_ports(br, ports, n);
493 /* Delete 'iface' from 'port' (which must be within 'br'). If 'iface' was
494 * 'port''s only interface, delete 'port' from 'br' also.
496 * After calling this function, 'iface' must not be referenced again. */
498 del_interface(const struct ovsrec_bridge *br,
499 const struct ovsrec_port *port,
500 const struct ovsrec_interface *iface)
502 if (port->n_interfaces == 1) {
505 struct ovsrec_interface **ifaces;
508 ifaces = xmalloc(sizeof *port->interfaces * port->n_interfaces);
509 for (i = n = 0; i < port->n_interfaces; i++) {
510 if (port->interfaces[i] != iface) {
511 ifaces[n++] = port->interfaces[i];
514 ovsrec_port_set_interfaces(port, ifaces, n);
519 /* Find and return a port within 'br' named 'port_name'. */
520 static const struct ovsrec_port *
521 find_port(const struct ovsrec_bridge *br, const char *port_name)
525 for (i = 0; i < br->n_ports; i++) {
526 struct ovsrec_port *port = br->ports[i];
527 if (!strcmp(port_name, port->name)) {
534 /* Find and return an interface within 'br' named 'iface_name'. */
535 static const struct ovsrec_interface *
536 find_interface(const struct ovsrec_bridge *br, const char *iface_name,
537 struct ovsrec_port **portp)
541 for (i = 0; i < br->n_ports; i++) {
542 struct ovsrec_port *port = br->ports[i];
545 for (j = 0; j < port->n_interfaces; j++) {
546 struct ovsrec_interface *iface = port->interfaces[j];
547 if (!strcmp(iface->name, iface_name)) {
559 del_bridge(struct ovsdb_idl *idl,
560 const struct ovsrec_open_vswitch *ovs, const char *br_name)
562 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
563 struct ovsrec_bridge **bridges;
564 struct ovsdb_idl_txn *txn;
568 VLOG_WARN("delbr %s: no bridge named %s", br_name, br_name);
572 txn = ovsdb_idl_txn_create(idl);
574 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: delbr %s", br_name);
576 /* Remove 'br' from the vswitch's list of bridges. */
577 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
578 for (i = n = 0; i < ovs->n_bridges; i++) {
579 if (ovs->bridges[i] != br) {
580 bridges[n++] = ovs->bridges[i];
583 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
586 return commit_txn(txn, true);
590 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
591 const char **port_name, uint64_t *count, uint64_t *skip)
593 static const struct nl_policy policy[] = {
594 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING, .optional = true },
595 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
596 [BRC_GENL_A_FDB_COUNT] = { .type = NL_A_U64, .optional = true },
597 [BRC_GENL_A_FDB_SKIP] = { .type = NL_A_U64, .optional = true },
599 struct nlattr *attrs[ARRAY_SIZE(policy)];
601 if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
602 attrs, ARRAY_SIZE(policy))
603 || (br_name && !attrs[BRC_GENL_A_DP_NAME])
604 || (port_name && !attrs[BRC_GENL_A_PORT_NAME])
605 || (count && !attrs[BRC_GENL_A_FDB_COUNT])
606 || (skip && !attrs[BRC_GENL_A_FDB_SKIP])) {
610 *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
612 *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
615 *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
618 *count = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_COUNT]);
621 *skip = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_SKIP]);
626 /* Composes and returns a reply to a request made by the datapath with Netlink
627 * sequence number 'seq' and error code 'error'. The caller may add additional
628 * attributes to the message, then it may send it with send_reply(). */
629 static struct ofpbuf *
630 compose_reply(uint32_t seq, int error)
632 struct ofpbuf *reply = ofpbuf_new(4096);
633 nl_msg_put_genlmsghdr(reply, 32, brc_family, NLM_F_REQUEST,
634 BRC_GENL_C_DP_RESULT, 1);
635 ((struct nlmsghdr *) reply->data)->nlmsg_seq = seq;
636 nl_msg_put_u32(reply, BRC_GENL_A_ERR_CODE, error);
640 /* Sends 'reply' to the datapath and frees it. */
642 send_reply(struct ofpbuf *reply)
644 int retval = nl_sock_send(brc_sock, reply, false);
646 VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
649 ofpbuf_delete(reply);
652 /* Composes and sends a reply to a request made by the datapath with Netlink
653 * sequence number 'seq' and error code 'error'. */
655 send_simple_reply(uint32_t seq, int error)
657 send_reply(compose_reply(seq, error));
661 handle_bridge_cmd(struct ovsdb_idl *idl,
662 const struct ovsrec_open_vswitch *ovs,
663 struct ofpbuf *buffer, bool add)
669 error = parse_command(buffer, &seq, &br_name, NULL, NULL, NULL);
674 retval = (add ? add_bridge : del_bridge)(idl, ovs, br_name);
675 VLOG_INFO_RL(&rl, "%sbr %s: %s",
676 add ? "add" : "del", br_name, strerror(retval));
677 } while (retval == EAGAIN);
679 send_simple_reply(seq, error);
684 static const struct nl_policy brc_port_policy[] = {
685 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
686 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
690 handle_port_cmd(struct ovsdb_idl *idl,
691 const struct ovsrec_open_vswitch *ovs,
692 struct ofpbuf *buffer, bool add)
694 const char *cmd_name = add ? "add-if" : "del-if";
695 const char *br_name, *port_name;
699 error = parse_command(buffer, &seq, &br_name, &port_name, NULL, NULL);
701 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
704 VLOG_WARN("%s %s %s: no bridge named %s",
705 cmd_name, br_name, port_name, br_name);
707 } else if (!netdev_exists(port_name)) {
708 VLOG_WARN("%s %s %s: no network device named %s",
709 cmd_name, br_name, port_name, port_name);
713 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
716 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: add-if %s",
718 add_port(ovs, br, port_name);
720 const struct ovsrec_port *port = find_port(br, port_name);
722 ovsdb_idl_txn_add_comment(txn,
723 "ovs-brcompatd: del-if %s",
729 error = commit_txn(txn, true);
730 VLOG_INFO_RL(&rl, "%s %s %s: %s",
731 cmd_name, br_name, port_name, strerror(error));
732 } while (error == EAGAIN);
734 send_simple_reply(seq, error);
740 /* The caller is responsible for freeing '*ovs_name' if the call is
743 linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch *ovs,
744 const char *linux_name,
745 const struct ovsrec_bridge **ovs_bridge,
748 *ovs_bridge = find_bridge(ovs, linux_name);
750 /* Bridge name is the same. We are interested in VLAN 0. */
754 /* No such Open vSwitch bridge 'linux_name', but there might be an
755 * internal port named 'linux_name' on some other bridge
756 * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
757 * port 'linux_name' on the bridge named 'ovs_bridge'. */
760 for (i = 0; i < ovs->n_bridges; i++) {
761 const struct ovsrec_bridge *br = ovs->bridges[i];
763 for (j = 0; j < br->n_ports; j++) {
764 const struct ovsrec_port *port = br->ports[j];
766 if (!strcmp(port->name, linux_name)) {
768 *br_vlan = port->n_tag ? *port->tag : -1;
779 handle_fdb_query_cmd(const struct ovsrec_open_vswitch *ovs,
780 struct ofpbuf *buffer)
782 /* This structure is copied directly from the Linux 2.6.30 header files.
783 * It would be more straightforward to #include <linux/if_bridge.h>, but
784 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
785 * with old header files won't have it. */
790 __u32 ageing_timer_value;
799 struct mac *local_macs;
803 /* Impedance matching between the vswitchd and Linux kernel notions of what
804 * a bridge is. The kernel only handles a single VLAN per bridge, but
805 * vswitchd can deal with all the VLANs on a single bridge. We have to
806 * pretend that the former is the case even though the latter is the
808 const char *linux_name; /* Name used by brctl. */
809 const struct ovsrec_bridge *ovs_bridge; /* Bridge used by ovs-vswitchd. */
810 int br_vlan; /* VLAN tag. */
813 struct ofpbuf query_data;
814 const char *iface_name;
815 struct ofpbuf *reply;
816 char *unixctl_command;
817 uint64_t count, skip;
823 /* Parse the command received from brcompat_mod. */
824 error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
829 /* Figure out vswitchd bridge and VLAN. */
830 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
831 &ovs_bridge, &br_vlan);
833 send_simple_reply(seq, error);
837 /* Fetch the forwarding database using ovs-appctl. */
838 unixctl_command = xasprintf("fdb/show %s", ovs_bridge->name);
839 error = execute_appctl_command(unixctl_command, &output);
840 free(unixctl_command);
842 send_simple_reply(seq, error);
846 /* Fetch the MAC address for each interface on the bridge, so that we can
847 * fill in the is_local field in the response. */
849 get_bridge_ifaces(ovs_bridge, &ifaces, br_vlan);
850 local_macs = xmalloc(sset_count(&ifaces) * sizeof *local_macs);
852 SSET_FOR_EACH (iface_name, &ifaces) {
853 struct mac *mac = &local_macs[n_local_macs];
854 struct netdev *netdev;
856 error = netdev_open_default(iface_name, &netdev);
858 if (!netdev_get_etheraddr(netdev, mac->addr)) {
861 netdev_close(netdev);
864 sset_destroy(&ifaces);
866 /* Parse the response from ovs-appctl and convert it to binary format to
867 * pass back to the kernel. */
868 ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
870 strtok_r(output, "\n", &save_ptr); /* Skip header line. */
872 struct __fdb_entry *entry;
874 uint8_t mac[ETH_ADDR_LEN];
878 line = strtok_r(NULL, "\n", &save_ptr);
883 if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
884 &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
885 != 2 + ETH_ADDR_SCAN_COUNT + 1) {
886 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
887 VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
891 if (vlan != br_vlan) {
900 /* Is this the MAC address of an interface on the bridge? */
902 for (i = 0; i < n_local_macs; i++) {
903 if (eth_addr_equals(local_macs[i].addr, mac)) {
909 entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
910 memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
911 entry->port_no = port & 0xff;
912 entry->is_local = is_local;
913 entry->ageing_timer_value = age * HZ;
914 entry->port_hi = (port & 0xff00) >> 8;
921 /* Compose and send reply to datapath. */
922 reply = compose_reply(seq, 0);
923 nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
924 query_data.data, query_data.size);
928 ofpbuf_uninit(&query_data);
935 send_ifindex_reply(uint32_t seq, struct sset *ifaces)
937 struct ofpbuf *reply;
942 /* Convert 'ifaces' into ifindexes. */
944 indices = xmalloc(sset_count(ifaces) * sizeof *indices);
945 SSET_FOR_EACH (iface, ifaces) {
946 int ifindex = if_nametoindex(iface);
948 indices[n_indices++] = ifindex;
952 /* Compose and send reply. */
953 reply = compose_reply(seq, 0);
954 nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
955 indices, n_indices * sizeof *indices);
963 handle_get_bridges_cmd(const struct ovsrec_open_vswitch *ovs,
964 struct ofpbuf *buffer)
973 /* Parse Netlink command.
975 * The command doesn't actually have any arguments, but we need the
976 * sequence number to send the reply. */
977 error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
982 /* Get all the real bridges and all the fake ones. */
984 for (i = 0; i < ovs->n_bridges; i++) {
985 const struct ovsrec_bridge *br = ovs->bridges[i];
987 sset_add(&bridges, br->name);
988 for (j = 0; j < br->n_ports; j++) {
989 const struct ovsrec_port *port = br->ports[j];
991 if (port->fake_bridge) {
992 sset_add(&bridges, port->name);
997 send_ifindex_reply(seq, &bridges);
998 sset_destroy(&bridges);
1004 handle_get_ports_cmd(const struct ovsrec_open_vswitch *ovs,
1005 struct ofpbuf *buffer)
1009 const char *linux_name;
1010 const struct ovsrec_bridge *ovs_bridge;
1017 /* Parse Netlink command. */
1018 error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
1023 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
1024 &ovs_bridge, &br_vlan);
1026 send_simple_reply(seq, error);
1031 get_bridge_ports(ovs_bridge, &ports, br_vlan);
1032 sset_find_and_delete(&ports, linux_name);
1033 send_ifindex_reply(seq, &ports); /* XXX bonds won't show up */
1034 sset_destroy(&ports);
1039 static struct ofpbuf *
1040 brc_recv_update__(void)
1043 struct ofpbuf *buffer;
1046 retval = nl_sock_recv(brc_sock, &buffer, false);
1049 if (nl_msg_nlmsgerr(buffer, NULL)
1050 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE) {
1062 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
1065 ofpbuf_delete(buffer);
1070 brc_recv_update(struct ovsdb_idl *idl)
1072 struct ofpbuf *buffer;
1073 struct genlmsghdr *genlmsghdr;
1074 const struct ovsrec_open_vswitch *ovs;
1076 buffer = brc_recv_update__();
1081 genlmsghdr = nl_msg_genlmsghdr(buffer);
1083 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
1087 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
1088 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
1089 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
1093 /* Get the Open vSwitch configuration. Just drop the request on the floor
1094 * if a valid configuration doesn't exist. (We could check this earlier,
1095 * but we want to drain pending Netlink messages even when there is no Open
1096 * vSwitch configuration.) */
1097 ovs = ovsrec_open_vswitch_first(idl);
1099 VLOG_WARN_RL(&rl, "could not find valid configuration to update");
1103 switch (genlmsghdr->cmd) {
1104 case BRC_GENL_C_DP_ADD:
1105 handle_bridge_cmd(idl, ovs, buffer, true);
1108 case BRC_GENL_C_DP_DEL:
1109 handle_bridge_cmd(idl, ovs, buffer, false);
1112 case BRC_GENL_C_PORT_ADD:
1113 handle_port_cmd(idl, ovs, buffer, true);
1116 case BRC_GENL_C_PORT_DEL:
1117 handle_port_cmd(idl, ovs, buffer, false);
1120 case BRC_GENL_C_FDB_QUERY:
1121 handle_fdb_query_cmd(ovs, buffer);
1124 case BRC_GENL_C_GET_BRIDGES:
1125 handle_get_bridges_cmd(ovs, buffer);
1128 case BRC_GENL_C_GET_PORTS:
1129 handle_get_ports_cmd(ovs, buffer);
1133 VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
1139 ofpbuf_delete(buffer);
1144 netdev_changed_cb(const struct rtnetlink_link_change *change, void *idl_)
1146 struct ovsdb_idl *idl = idl_;
1147 const struct ovsrec_open_vswitch *ovs;
1148 const struct ovsrec_interface *iface;
1149 struct ovsdb_idl_txn *txn;
1150 struct ovsrec_port *port;
1151 struct ovsrec_bridge *br;
1152 char br_name[IFNAMSIZ];
1153 const char *port_name;
1156 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
1160 if (change->nlmsg_type != RTM_DELLINK || !change->master_ifindex) {
1164 ovs = ovsrec_open_vswitch_first(idl);
1169 port_name = change->ifname;
1170 if (!if_indextoname(change->master_ifindex, br_name)) {
1174 if (netdev_exists(port_name)) {
1175 /* A network device by that name exists even though the kernel
1176 * told us it had disappeared. Probably, what happened was
1179 * 1. Device destroyed.
1180 * 2. Notification sent to us.
1181 * 3. New device created with same name as old one.
1182 * 4. ovs-brcompatd notified, removes device from bridge.
1184 * There's no a priori reason that in this situation that the
1185 * new device with the same name should remain in the bridge;
1186 * on the contrary, that would be unexpected. *But* there is
1187 * one important situation where, if we do this, bad things
1188 * happen. This is the case of XenServer Tools version 5.0.0,
1189 * which on boot of a Windows VM cause something like this to
1190 * happen on the Xen host:
1192 * i. Create tap1.0 and vif1.0.
1193 * ii. Delete tap1.0.
1194 * iii. Delete vif1.0.
1195 * iv. Re-create vif1.0.
1197 * (XenServer Tools 5.5.0 does not exhibit this behavior, and
1198 * neither does a VM without Tools installed at all.)
1200 * Steps iii and iv happen within a few seconds of each other.
1201 * Step iv causes /etc/xensource/scripts/vif to run, which in
1202 * turn calls ovs-cfg-mod to add the new device to the bridge.
1203 * If step iv happens after step 4 (in our first list of
1204 * steps), then all is well, but if it happens between 3 and 4
1205 * (which can easily happen if ovs-brcompatd has to wait to
1206 * lock the configuration file), then we will remove the new
1207 * incarnation from the bridge instead of the old one!
1209 * So, to avoid this problem, we do nothing here. This is
1210 * strictly incorrect except for this one particular case, and
1211 * perhaps that will bite us someday. If that happens, then we
1212 * will have to somehow track network devices by ifindex, since
1213 * a new device will have a new ifindex even if it has the same
1214 * name as an old device.
1216 VLOG_INFO("kernel reported network device %s removed but "
1217 "a device by that name exists (XS Tools 5.0.0?)",
1222 VLOG_INFO("network device %s destroyed, removing from bridge %s",
1223 port_name, br_name);
1225 br = find_bridge(ovs, br_name);
1227 VLOG_WARN("no bridge named %s from which to remove %s",
1228 br_name, port_name);
1232 iface = find_interface(br, port_name, &port);
1237 txn = ovsdb_idl_txn_create(idl);
1238 del_interface(br, port, iface);
1239 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: destroy port %s",
1241 commit_txn(txn, false);
1245 main(int argc, char *argv[])
1247 extern struct vlog_module VLM_reconnect;
1248 struct rtnetlink_notifier link_notifier;
1249 struct unixctl_server *unixctl;
1251 struct ovsdb_idl *idl;
1254 proctitle_init(argc, argv);
1255 set_program_name(argv[0]);
1256 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
1257 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
1259 remote = parse_options(argc, argv);
1260 signal(SIGPIPE, SIG_IGN);
1266 retval = unixctl_server_create(NULL, &unixctl);
1271 if (brc_open(&brc_sock)) {
1272 VLOG_FATAL("could not open brcompat socket. Check "
1273 "\"brcompat\" kernel module.");
1277 if (prune_timeout) {
1278 rtnetlink_link_notifier_register(&link_notifier,
1279 netdev_changed_cb, NULL);
1282 daemonize_complete();
1284 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
1287 const struct ovsrec_open_vswitch *ovs;
1291 unixctl_server_run(unixctl);
1292 rtnetlink_link_notifier_run();
1293 brc_recv_update(idl);
1295 ovs = ovsrec_open_vswitch_first(idl);
1296 if (!ovs && ovsdb_idl_has_ever_connected(idl)) {
1297 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1298 VLOG_WARN_RL(&rl, "%s: database does not contain any Open vSwitch "
1299 "configuration", remote);
1303 /* If 'prune_timeout' is non-zero, we actively prune from the
1304 * configuration of port entries that are no longer valid. We
1307 * 1) The kernel explicitly notifies us of removed ports
1308 * through the RTNL messages.
1310 * 2) We periodically check all ports associated with bridges
1311 * to see if they no longer exist.
1313 if (ovs && prune_timeout) {
1314 rtnetlink_link_notifier_run();
1315 poll_timer_wait(prune_timeout);
1318 nl_sock_wait(brc_sock, POLLIN);
1319 ovsdb_idl_wait(idl);
1320 unixctl_server_wait(unixctl);
1321 rtnetlink_link_notifier_wait();
1326 if (prune_timeout) {
1327 rtnetlink_link_notifier_unregister(&link_notifier);
1329 ovsdb_idl_destroy(idl);
1335 validate_appctl_command(void)
1341 for (p = strchr(appctl_command, '%'); p; p = strchr(p + 2, '%')) {
1343 /* Nothing to do. */
1344 } else if (p[1] == 's') {
1347 VLOG_FATAL("only '%%s' and '%%%%' allowed in --appctl-command");
1351 VLOG_FATAL("'%%s' must appear exactly once in --appctl-command");
1356 parse_options(int argc, char *argv[])
1362 LEAK_CHECKER_OPTION_ENUMS,
1365 static struct option long_options[] = {
1366 {"help", no_argument, NULL, 'h'},
1367 {"version", no_argument, NULL, 'V'},
1368 {"prune-timeout", required_argument, NULL, OPT_PRUNE_TIMEOUT},
1369 {"appctl-command", required_argument, NULL, OPT_APPCTL_COMMAND},
1370 DAEMON_LONG_OPTIONS,
1372 LEAK_CHECKER_LONG_OPTIONS,
1375 char *short_options = long_options_to_short_options(long_options);
1377 appctl_command = xasprintf("%s/ovs-appctl %%s", ovs_bindir());
1381 c = getopt_long(argc, argv, short_options, long_options, NULL);
1392 OVS_PRINT_VERSION(0, 0);
1395 case OPT_PRUNE_TIMEOUT:
1396 prune_timeout = atoi(optarg) * 1000;
1399 case OPT_APPCTL_COMMAND:
1400 appctl_command = optarg;
1403 VLOG_OPTION_HANDLERS
1404 DAEMON_OPTION_HANDLERS
1405 LEAK_CHECKER_OPTION_HANDLERS
1414 free(short_options);
1416 validate_appctl_command();
1422 VLOG_FATAL("database socket is non-option argument; "
1423 "use --help for usage");
1432 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
1433 "usage: %s [OPTIONS] CONFIG\n"
1434 "CONFIG is the configuration file used by ovs-vswitchd.\n",
1435 program_name, program_name);
1436 printf("\nConfiguration options:\n"
1437 " --appctl-command=COMMAND shell command to run ovs-appctl\n"
1438 " --prune-timeout=SECS wait at most SECS before pruning ports\n"
1442 printf("\nOther options:\n"
1443 " -h, --help display this help message\n"
1444 " -V, --version display version information\n");
1445 leak_checker_usage();
1446 printf("\nThe default appctl command is:\n%s\n", appctl_command);