1 /* Copyright (c) 2008, 2009, 2010 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"
47 #include "openvswitch/brcompat-netlink.h"
48 #include "ovsdb-idl.h"
50 #include "poll-loop.h"
57 #include "vswitchd/vswitch-idl.h"
60 #define THIS_MODULE VLM_brcompatd
63 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
65 /* Actions to modify bridge compatibility configuration. */
73 static const char *parse_options(int argc, char *argv[]);
74 static void usage(void) NO_RETURN;
76 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
78 /* Maximum number of milliseconds to wait before pruning port entries that
79 * no longer exist. If set to zero, ports are never pruned. */
80 static int prune_timeout = 5000;
82 /* Shell command to execute (via popen()) to send a control command to the
83 * running ovs-vswitchd process. The string must contain one instance of %s,
84 * which is replaced by the control command. */
85 static char *appctl_command;
87 /* Netlink socket to listen for interface changes. */
88 static struct nl_sock *rtnl_sock;
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 }
100 static const struct nl_policy rtnlgrp_link_policy[] = {
101 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
102 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
106 lookup_brc_multicast_group(int *multicast_group)
108 struct nl_sock *sock;
109 struct ofpbuf request, *reply;
110 struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
113 retval = nl_sock_create(NETLINK_GENERIC, 0, 0, 0, &sock);
117 ofpbuf_init(&request, 0);
118 nl_msg_put_genlmsghdr(&request, sock, 0, brc_family,
119 NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
120 retval = nl_sock_transact(sock, &request, &reply);
121 ofpbuf_uninit(&request);
123 nl_sock_destroy(sock);
126 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
127 brc_multicast_policy, attrs,
128 ARRAY_SIZE(brc_multicast_policy))) {
129 nl_sock_destroy(sock);
130 ofpbuf_delete(reply);
133 *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
134 nl_sock_destroy(sock);
135 ofpbuf_delete(reply);
140 /* Opens a socket for brcompat notifications. Returns 0 if successful,
141 * otherwise a positive errno value. */
143 brc_open(struct nl_sock **sock)
145 int multicast_group = 0;
148 retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
153 retval = lookup_brc_multicast_group(&multicast_group);
158 retval = nl_sock_create(NETLINK_GENERIC, multicast_group, 0, 0, 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, &status);
199 VLOG_ERR("failed to execute %s command via ovs-appctl: %s",
200 unixctl_command, strerror(error));
202 char *msg = process_status_msg(status);
203 VLOG_ERR("ovs-appctl exited with error (%s)", msg);
208 /* Deal with stdout_log. */
210 *output = stdout_log;
215 /* Deal with stderr_log */
216 if (stderr_log && *stderr_log) {
217 VLOG_INFO("ovs-appctl wrote to stderr:\n%s", stderr_log);
227 do_get_bridge_parts(const struct ovsrec_bridge *br, struct svec *parts,
228 int vlan, bool break_down_bonds)
234 for (i = 0; i < br->n_ports; i++) {
235 const struct ovsrec_port *port = br->ports[i];
237 svec_add(&ports, port->name);
239 int port_vlan = port->n_tag ? *port->tag : 0;
240 if (vlan != port_vlan) {
244 if (break_down_bonds) {
245 for (j = 0; j < port->n_interfaces; j++) {
246 const struct ovsrec_interface *iface = port->interfaces[j];
247 svec_add(parts, iface->name);
250 svec_add(parts, port->name);
253 svec_destroy(&ports);
256 /* Add all the interfaces for 'bridge' to 'ifaces', breaking bonded interfaces
257 * down into their constituent parts.
259 * If 'vlan' < 0, all interfaces on 'bridge' are reported. If 'vlan' == 0,
260 * then only interfaces for trunk ports or ports with implicit VLAN 0 are
261 * reported. If 'vlan' > 0, only interfaces with implicit VLAN 'vlan' are
264 get_bridge_ifaces(const struct ovsrec_bridge *br, struct svec *ifaces,
267 do_get_bridge_parts(br, ifaces, vlan, true);
270 /* Add all the ports for 'bridge' to 'ports'. Bonded ports are reported under
271 * the bond name, not broken down into their constituent interfaces.
273 * If 'vlan' < 0, all ports on 'bridge' are reported. If 'vlan' == 0, then
274 * only trunk ports or ports with implicit VLAN 0 are reported. If 'vlan' > 0,
275 * only port with implicit VLAN 'vlan' are reported. */
277 get_bridge_ports(const struct ovsrec_bridge *br, struct svec *ports,
280 do_get_bridge_parts(br, ports, vlan, false);
284 /* Go through the configuration file and remove any ports that no longer
285 * exist associated with a bridge. */
290 struct svec bridges, delete;
292 if (cfg_lock(NULL, 0)) {
293 /* Couldn't lock config file. */
299 cfg_get_subsections(&bridges, "bridge");
300 for (i=0; i<bridges.n; i++) {
301 const char *br_name = bridges.names[i];
304 /* Check that each bridge interface exists. */
306 get_bridge_ifaces(br_name, &ifaces, -1);
307 for (j = 0; j < ifaces.n; j++) {
308 const char *iface_name = ifaces.names[j];
310 /* The local port and internal ports are created and destroyed by
311 * ovs-vswitchd itself, so don't bother checking for them at all.
312 * In practice, they might not exist if ovs-vswitchd hasn't
313 * finished reloading since the configuration file was updated. */
314 if (!strcmp(iface_name, br_name)
315 || cfg_get_bool(0, "iface.%s.internal", iface_name)) {
319 if (!netdev_exists(iface_name)) {
320 VLOG_INFO_RL(&rl, "removing dead interface %s from %s",
321 iface_name, br_name);
322 svec_add(&delete, iface_name);
325 svec_destroy(&ifaces);
327 svec_destroy(&bridges);
332 for (i = 0; i < delete.n; i++) {
333 cfg_del_match("bridge.*.port=%s", delete.names[i]);
334 cfg_del_match("bonding.*.slave=%s", delete.names[i]);
341 svec_destroy(&delete);
345 static struct ovsdb_idl_txn *
346 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
348 return ovsdb_idl_txn_get(&ovs->header_);
352 port_is_fake_bridge(const struct ovsrec_port *port)
354 return (port->fake_bridge
356 && *port->tag >= 1 && *port->tag <= 4095);
360 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
361 struct ovsrec_bridge *bridge)
363 struct ovsrec_bridge **bridges;
366 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
367 for (i = 0; i < ovs->n_bridges; i++) {
368 bridges[i] = ovs->bridges[i];
370 bridges[ovs->n_bridges] = bridge;
371 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
376 where_uuid_equals(const struct uuid *uuid)
381 json_string_create("_uuid"),
382 json_string_create("=="),
384 json_string_create("uuid"),
385 json_string_create_nocopy(
386 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
389 /* Commits 'txn'. If 'wait_for_reload' is true, also waits for Open vSwitch to
390 reload the configuration before returning.
392 Returns EAGAIN if the caller should try the operation again, 0 on success,
393 otherwise a positive errno value. */
395 commit_txn(struct ovsdb_idl_txn *txn, bool wait_for_reload)
397 struct ovsdb_idl *idl = ovsdb_idl_txn_get_idl (txn);
398 enum ovsdb_idl_txn_status status;
399 int64_t next_cfg = 0;
401 if (wait_for_reload) {
402 const struct ovsrec_open_vswitch *ovs = ovsrec_open_vswitch_first(idl);
403 struct json *where = where_uuid_equals(&ovs->header_.uuid);
404 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
407 status = ovsdb_idl_txn_commit_block(txn);
408 if (wait_for_reload && status == TXN_SUCCESS) {
409 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
411 ovsdb_idl_txn_destroy(txn);
418 VLOG_ERR_RL(&rl, "OVSDB transaction unexpectedly aborted");
425 if (wait_for_reload) {
427 /* We can't use 'ovs' any longer because ovsdb_idl_run() can
429 const struct ovsrec_open_vswitch *ovs2;
432 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs2, idl) {
433 if (ovs2->cur_cfg >= next_cfg) {
445 VLOG_ERR_RL(&rl, "OVSDB transaction needs retry");
449 VLOG_ERR_RL(&rl, "OVSDB transaction failed: %s",
450 ovsdb_idl_txn_get_error(txn));
459 add_bridge(struct ovsdb_idl *idl, const struct ovsrec_open_vswitch *ovs,
462 struct ovsrec_bridge *br;
463 struct ovsrec_port *port;
464 struct ovsrec_interface *iface;
465 struct ovsdb_idl_txn *txn;
467 if (find_bridge(ovs, br_name)) {
468 VLOG_WARN("addbr %s: bridge %s exists", br_name, br_name);
470 } else if (netdev_exists(br_name)) {
473 for (i = 0; i < ovs->n_bridges; i++) {
475 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
477 for (j = 0; j < br_cfg->n_ports; j++) {
478 if (port_is_fake_bridge(br_cfg->ports[j])) {
479 VLOG_WARN("addbr %s: %s exists as a fake bridge",
486 VLOG_WARN("addbr %s: cannot create bridge %s because a network "
487 "device named %s already exists",
488 br_name, br_name, br_name);
492 txn = ovsdb_idl_txn_create(idl);
494 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: addbr %s", br_name);
496 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
497 ovsrec_interface_set_name(iface, br_name);
499 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
500 ovsrec_port_set_name(port, br_name);
501 ovsrec_port_set_interfaces(port, &iface, 1);
503 br = ovsrec_bridge_insert(txn_from_openvswitch(ovs));
504 ovsrec_bridge_set_name(br, br_name);
505 ovsrec_bridge_set_ports(br, &port, 1);
507 ovs_insert_bridge(ovs, br);
509 return commit_txn(txn, true);
513 add_port(const struct ovsrec_open_vswitch *ovs,
514 const struct ovsrec_bridge *br, const char *port_name)
516 struct ovsrec_interface *iface;
517 struct ovsrec_port *port;
518 struct ovsrec_port **ports;
521 /* xxx Check conflicts? */
522 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
523 ovsrec_interface_set_name(iface, port_name);
525 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
526 ovsrec_port_set_name(port, port_name);
527 ovsrec_port_set_interfaces(port, &iface, 1);
529 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
530 for (i = 0; i < br->n_ports; i++) {
531 ports[i] = br->ports[i];
533 ports[br->n_ports] = port;
534 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
538 /* Deletes 'port' from 'br'.
540 * After calling this function, 'port' must not be referenced again. */
542 del_port(const struct ovsrec_bridge *br, const struct ovsrec_port *port)
544 struct ovsrec_port **ports;
547 /* Remove 'port' from the bridge's list of ports. */
548 ports = xmalloc(sizeof *br->ports * br->n_ports);
549 for (i = n = 0; i < br->n_ports; i++) {
550 if (br->ports[i] != port) {
551 ports[n++] = br->ports[i];
554 ovsrec_bridge_set_ports(br, ports, n);
557 /* Delete all of the port's interfaces. */
558 for (i = 0; i < port->n_interfaces; i++) {
559 ovsrec_interface_delete(port->interfaces[i]);
562 /* Delete the port itself. */
563 ovsrec_port_delete(port);
566 /* Delete 'iface' from 'port' (which must be within 'br'). If 'iface' was
567 * 'port''s only interface, delete 'port' from 'br' also.
569 * After calling this function, 'iface' must not be referenced again. */
571 del_interface(const struct ovsrec_bridge *br,
572 const struct ovsrec_port *port,
573 const struct ovsrec_interface *iface)
575 if (port->n_interfaces == 1) {
578 struct ovsrec_interface **ifaces;
581 ifaces = xmalloc(sizeof *port->interfaces * port->n_interfaces);
582 for (i = n = 0; i < port->n_interfaces; i++) {
583 if (port->interfaces[i] != iface) {
584 ifaces[n++] = port->interfaces[i];
587 ovsrec_port_set_interfaces(port, ifaces, n);
589 ovsrec_interface_delete(iface);
593 /* Find and return a port within 'br' named 'port_name'. */
594 static const struct ovsrec_port *
595 find_port(const struct ovsrec_bridge *br, const char *port_name)
599 for (i = 0; i < br->n_ports; i++) {
600 struct ovsrec_port *port = br->ports[i];
601 if (!strcmp(port_name, port->name)) {
608 /* Find and return an interface within 'br' named 'iface_name'. */
609 static const struct ovsrec_interface *
610 find_interface(const struct ovsrec_bridge *br, const char *iface_name,
611 struct ovsrec_port **portp)
615 for (i = 0; i < br->n_ports; i++) {
616 struct ovsrec_port *port = br->ports[i];
619 for (j = 0; j < port->n_interfaces; j++) {
620 struct ovsrec_interface *iface = port->interfaces[j];
621 if (!strcmp(iface->name, iface_name)) {
633 del_bridge(struct ovsdb_idl *idl,
634 const struct ovsrec_open_vswitch *ovs, const char *br_name)
636 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
637 struct ovsrec_bridge **bridges;
638 struct ovsdb_idl_txn *txn;
642 VLOG_WARN("delbr %s: no bridge named %s", br_name, br_name);
646 txn = ovsdb_idl_txn_create(idl);
648 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: delbr %s", br_name);
650 /* Delete everything that the bridge points to, then delete the bridge
652 while (br->n_ports > 0) {
653 del_port(br, br->ports[0]);
655 for (i = 0; i < br->n_mirrors; i++) {
656 ovsrec_mirror_delete(br->mirrors[i]);
659 ovsrec_netflow_delete(br->netflow);
662 ovsrec_sflow_delete(br->sflow);
664 for (i = 0; i < br->n_controller; i++) {
665 ovsrec_controller_delete(br->controller[i]);
668 /* Remove 'br' from the vswitch's list of bridges. */
669 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
670 for (i = n = 0; i < ovs->n_bridges; i++) {
671 if (ovs->bridges[i] != br) {
672 bridges[n++] = ovs->bridges[i];
675 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
678 /* Delete the bridge itself. */
679 ovsrec_bridge_delete(br);
681 return commit_txn(txn, true);
685 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
686 const char **port_name, uint64_t *count, uint64_t *skip)
688 static const struct nl_policy policy[] = {
689 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING, .optional = true },
690 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
691 [BRC_GENL_A_FDB_COUNT] = { .type = NL_A_U64, .optional = true },
692 [BRC_GENL_A_FDB_SKIP] = { .type = NL_A_U64, .optional = true },
694 struct nlattr *attrs[ARRAY_SIZE(policy)];
696 if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
697 attrs, ARRAY_SIZE(policy))
698 || (br_name && !attrs[BRC_GENL_A_DP_NAME])
699 || (port_name && !attrs[BRC_GENL_A_PORT_NAME])
700 || (count && !attrs[BRC_GENL_A_FDB_COUNT])
701 || (skip && !attrs[BRC_GENL_A_FDB_SKIP])) {
705 *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
707 *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
710 *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
713 *count = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_COUNT]);
716 *skip = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_SKIP]);
721 /* Composes and returns a reply to a request made by the datapath with Netlink
722 * sequence number 'seq' and error code 'error'. The caller may add additional
723 * attributes to the message, then it may send it with send_reply(). */
724 static struct ofpbuf *
725 compose_reply(uint32_t seq, int error)
727 struct ofpbuf *reply = ofpbuf_new(4096);
728 nl_msg_put_genlmsghdr(reply, brc_sock, 32, brc_family, NLM_F_REQUEST,
729 BRC_GENL_C_DP_RESULT, 1);
730 ((struct nlmsghdr *) reply->data)->nlmsg_seq = seq;
731 nl_msg_put_u32(reply, BRC_GENL_A_ERR_CODE, error);
735 /* Sends 'reply' to the datapath and frees it. */
737 send_reply(struct ofpbuf *reply)
739 int retval = nl_sock_send(brc_sock, reply, false);
741 VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
744 ofpbuf_delete(reply);
747 /* Composes and sends a reply to a request made by the datapath with Netlink
748 * sequence number 'seq' and error code 'error'. */
750 send_simple_reply(uint32_t seq, int error)
752 send_reply(compose_reply(seq, error));
756 handle_bridge_cmd(struct ovsdb_idl *idl,
757 const struct ovsrec_open_vswitch *ovs,
758 struct ofpbuf *buffer, bool add)
764 error = parse_command(buffer, &seq, &br_name, NULL, NULL, NULL);
769 retval = (add ? add_bridge : del_bridge)(idl, ovs, br_name);
770 VLOG_INFO_RL(&rl, "%sbr %s: %s",
771 add ? "add" : "del", br_name, strerror(retval));
772 } while (retval == EAGAIN);
774 send_simple_reply(seq, error);
779 static const struct nl_policy brc_port_policy[] = {
780 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
781 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
785 handle_port_cmd(struct ovsdb_idl *idl,
786 const struct ovsrec_open_vswitch *ovs,
787 struct ofpbuf *buffer, bool add)
789 const char *cmd_name = add ? "add-if" : "del-if";
790 const char *br_name, *port_name;
794 error = parse_command(buffer, &seq, &br_name, &port_name, NULL, NULL);
796 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
799 VLOG_WARN("%s %s %s: no bridge named %s",
800 cmd_name, br_name, port_name, br_name);
802 } else if (!netdev_exists(port_name)) {
803 VLOG_WARN("%s %s %s: no network device named %s",
804 cmd_name, br_name, port_name, port_name);
808 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
811 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: add-if %s",
813 add_port(ovs, br, port_name);
815 const struct ovsrec_port *port = find_port(br, port_name);
817 ovsdb_idl_txn_add_comment(txn,
818 "ovs-brcompatd: del-if %s",
824 error = commit_txn(txn, true);
825 VLOG_INFO_RL(&rl, "%s %s %s: %s",
826 cmd_name, br_name, port_name, strerror(error));
827 } while (error == EAGAIN);
829 send_simple_reply(seq, error);
835 /* The caller is responsible for freeing '*ovs_name' if the call is
838 linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch *ovs,
839 const char *linux_name,
840 const struct ovsrec_bridge **ovs_bridge,
843 *ovs_bridge = find_bridge(ovs, linux_name);
845 /* Bridge name is the same. We are interested in VLAN 0. */
849 /* No such Open vSwitch bridge 'linux_name', but there might be an
850 * internal port named 'linux_name' on some other bridge
851 * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
852 * port 'linux_name' on the bridge named 'ovs_bridge'. */
855 for (i = 0; i < ovs->n_bridges; i++) {
856 const struct ovsrec_bridge *br = ovs->bridges[i];
858 for (j = 0; j < br->n_ports; j++) {
859 const struct ovsrec_port *port = br->ports[j];
861 if (!strcmp(port->name, linux_name)) {
863 *br_vlan = port->n_tag ? *port->tag : -1;
874 handle_fdb_query_cmd(const struct ovsrec_open_vswitch *ovs,
875 struct ofpbuf *buffer)
877 /* This structure is copied directly from the Linux 2.6.30 header files.
878 * It would be more straightforward to #include <linux/if_bridge.h>, but
879 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
880 * with old header files won't have it. */
885 __u32 ageing_timer_value;
894 struct mac *local_macs;
898 /* Impedance matching between the vswitchd and Linux kernel notions of what
899 * a bridge is. The kernel only handles a single VLAN per bridge, but
900 * vswitchd can deal with all the VLANs on a single bridge. We have to
901 * pretend that the former is the case even though the latter is the
903 const char *linux_name; /* Name used by brctl. */
904 const struct ovsrec_bridge *ovs_bridge; /* Bridge used by ovs-vswitchd. */
905 int br_vlan; /* VLAN tag. */
908 struct ofpbuf query_data;
909 struct ofpbuf *reply;
910 char *unixctl_command;
911 uint64_t count, skip;
917 /* Parse the command received from brcompat_mod. */
918 error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
923 /* Figure out vswitchd bridge and VLAN. */
924 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
925 &ovs_bridge, &br_vlan);
927 send_simple_reply(seq, error);
931 /* Fetch the forwarding database using ovs-appctl. */
932 unixctl_command = xasprintf("fdb/show %s", ovs_bridge->name);
933 error = execute_appctl_command(unixctl_command, &output);
934 free(unixctl_command);
936 send_simple_reply(seq, error);
940 /* Fetch the MAC address for each interface on the bridge, so that we can
941 * fill in the is_local field in the response. */
943 get_bridge_ifaces(ovs_bridge, &ifaces, br_vlan);
944 local_macs = xmalloc(ifaces.n * sizeof *local_macs);
946 for (i = 0; i < ifaces.n; i++) {
947 const char *iface_name = ifaces.names[i];
948 struct mac *mac = &local_macs[n_local_macs];
949 struct netdev *netdev;
951 error = netdev_open_default(iface_name, &netdev);
953 if (!netdev_get_etheraddr(netdev, mac->addr)) {
956 netdev_close(netdev);
959 svec_destroy(&ifaces);
961 /* Parse the response from ovs-appctl and convert it to binary format to
962 * pass back to the kernel. */
963 ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
965 strtok_r(output, "\n", &save_ptr); /* Skip header line. */
967 struct __fdb_entry *entry;
969 uint8_t mac[ETH_ADDR_LEN];
973 line = strtok_r(NULL, "\n", &save_ptr);
978 if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
979 &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
980 != 2 + ETH_ADDR_SCAN_COUNT + 1) {
981 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
982 VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
986 if (vlan != br_vlan) {
995 /* Is this the MAC address of an interface on the bridge? */
997 for (i = 0; i < n_local_macs; i++) {
998 if (eth_addr_equals(local_macs[i].addr, mac)) {
1004 entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
1005 memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
1006 entry->port_no = port & 0xff;
1007 entry->is_local = is_local;
1008 entry->ageing_timer_value = age * HZ;
1009 entry->port_hi = (port & 0xff00) >> 8;
1016 /* Compose and send reply to datapath. */
1017 reply = compose_reply(seq, 0);
1018 nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
1019 query_data.data, query_data.size);
1023 ofpbuf_uninit(&query_data);
1029 send_ifindex_reply(uint32_t seq, struct svec *ifaces)
1031 struct ofpbuf *reply;
1037 /* Make sure that any given interface only occurs once. This shouldn't
1038 * happen, but who knows what people put into their configuration files. */
1039 svec_sort_unique(ifaces);
1041 /* Convert 'ifaces' into ifindexes. */
1043 indices = xmalloc(ifaces->n * sizeof *indices);
1044 SVEC_FOR_EACH (i, iface, ifaces) {
1045 int ifindex = if_nametoindex(iface);
1047 indices[n_indices++] = ifindex;
1051 /* Compose and send reply. */
1052 reply = compose_reply(seq, 0);
1053 nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
1054 indices, n_indices * sizeof *indices);
1062 handle_get_bridges_cmd(const struct ovsrec_open_vswitch *ovs,
1063 struct ofpbuf *buffer)
1065 struct svec bridges;
1072 /* Parse Netlink command.
1074 * The command doesn't actually have any arguments, but we need the
1075 * sequence number to send the reply. */
1076 error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
1081 /* Get all the real bridges and all the fake ones. */
1082 svec_init(&bridges);
1083 for (i = 0; i < ovs->n_bridges; i++) {
1084 const struct ovsrec_bridge *br = ovs->bridges[i];
1086 svec_add(&bridges, br->name);
1087 for (j = 0; j < br->n_ports; j++) {
1088 const struct ovsrec_port *port = br->ports[j];
1090 if (port->fake_bridge) {
1091 svec_add(&bridges, port->name);
1096 send_ifindex_reply(seq, &bridges);
1097 svec_destroy(&bridges);
1103 handle_get_ports_cmd(const struct ovsrec_open_vswitch *ovs,
1104 struct ofpbuf *buffer)
1108 const char *linux_name;
1109 const struct ovsrec_bridge *ovs_bridge;
1116 /* Parse Netlink command. */
1117 error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
1122 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
1123 &ovs_bridge, &br_vlan);
1125 send_simple_reply(seq, error);
1130 get_bridge_ports(ovs_bridge, &ports, br_vlan);
1132 svec_del(&ports, linux_name);
1133 send_ifindex_reply(seq, &ports); /* XXX bonds won't show up */
1134 svec_destroy(&ports);
1140 brc_recv_update(struct ovsdb_idl *idl)
1143 struct ofpbuf *buffer;
1144 struct genlmsghdr *genlmsghdr;
1145 const struct ovsrec_open_vswitch *ovs;
1149 ofpbuf_delete(buffer);
1150 retval = nl_sock_recv(brc_sock, &buffer, false);
1151 } while (retval == ENOBUFS
1153 && (nl_msg_nlmsgerr(buffer, NULL)
1154 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE)));
1156 if (retval != EAGAIN) {
1157 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
1162 genlmsghdr = nl_msg_genlmsghdr(buffer);
1164 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
1168 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
1169 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
1170 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
1174 /* Get the Open vSwitch configuration. Just drop the request on the floor
1175 * if a valid configuration doesn't exist. (We could check this earlier,
1176 * but we want to drain pending Netlink messages even when there is no Open
1177 * vSwitch configuration.) */
1178 ovs = ovsrec_open_vswitch_first(idl);
1180 VLOG_WARN_RL(&rl, "could not find valid configuration to update");
1184 switch (genlmsghdr->cmd) {
1185 case BRC_GENL_C_DP_ADD:
1186 handle_bridge_cmd(idl, ovs, buffer, true);
1189 case BRC_GENL_C_DP_DEL:
1190 handle_bridge_cmd(idl, ovs, buffer, false);
1193 case BRC_GENL_C_PORT_ADD:
1194 handle_port_cmd(idl, ovs, buffer, true);
1197 case BRC_GENL_C_PORT_DEL:
1198 handle_port_cmd(idl, ovs, buffer, false);
1201 case BRC_GENL_C_FDB_QUERY:
1202 handle_fdb_query_cmd(ovs, buffer);
1205 case BRC_GENL_C_GET_BRIDGES:
1206 handle_get_bridges_cmd(ovs, buffer);
1209 case BRC_GENL_C_GET_PORTS:
1210 handle_get_ports_cmd(ovs, buffer);
1214 VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
1220 ofpbuf_delete(buffer);
1224 /* Check for interface configuration changes announced through RTNL. */
1226 rtnl_recv_update(struct ovsdb_idl *idl,
1227 const struct ovsrec_open_vswitch *ovs)
1231 int error = nl_sock_recv(rtnl_sock, &buf, false);
1232 if (error == EAGAIN) {
1233 /* Nothing to do. */
1234 } else if (error == ENOBUFS) {
1235 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
1237 VLOG_WARN_RL(&rl, "error on network monitor socket: %s",
1240 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1241 struct nlmsghdr *nlh;
1242 struct ifinfomsg *iim;
1244 nlh = ofpbuf_at(buf, 0, NLMSG_HDRLEN);
1245 iim = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *iim);
1247 VLOG_WARN_RL(&rl, "received bad rtnl message (no ifinfomsg)");
1252 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1253 rtnlgrp_link_policy,
1254 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1255 VLOG_WARN_RL(&rl,"received bad rtnl message (policy)");
1259 if (nlh->nlmsg_type == RTM_DELLINK && attrs[IFLA_MASTER]) {
1260 const char *port_name = nl_attr_get_string(attrs[IFLA_IFNAME]);
1261 char br_name[IFNAMSIZ];
1262 uint32_t br_idx = nl_attr_get_u32(attrs[IFLA_MASTER]);
1264 if (!if_indextoname(br_idx, br_name)) {
1269 if (!netdev_exists(port_name)) {
1270 /* Network device is really gone. */
1271 struct ovsdb_idl_txn *txn;
1272 const struct ovsrec_interface *iface;
1273 struct ovsrec_port *port;
1274 struct ovsrec_bridge *br;
1276 VLOG_INFO("network device %s destroyed, "
1277 "removing from bridge %s", port_name, br_name);
1279 br = find_bridge(ovs, br_name);
1281 VLOG_WARN("no bridge named %s from which to remove %s",
1282 br_name, port_name);
1287 txn = ovsdb_idl_txn_create(idl);
1289 iface = find_interface(br, port_name, &port);
1291 del_interface(br, port, iface);
1292 ovsdb_idl_txn_add_comment(txn,
1293 "ovs-brcompatd: destroy port %s",
1297 commit_txn(txn, false);
1299 /* A network device by that name exists even though the kernel
1300 * told us it had disappeared. Probably, what happened was
1303 * 1. Device destroyed.
1304 * 2. Notification sent to us.
1305 * 3. New device created with same name as old one.
1306 * 4. ovs-brcompatd notified, removes device from bridge.
1308 * There's no a priori reason that in this situation that the
1309 * new device with the same name should remain in the bridge;
1310 * on the contrary, that would be unexpected. *But* there is
1311 * one important situation where, if we do this, bad things
1312 * happen. This is the case of XenServer Tools version 5.0.0,
1313 * which on boot of a Windows VM cause something like this to
1314 * happen on the Xen host:
1316 * i. Create tap1.0 and vif1.0.
1317 * ii. Delete tap1.0.
1318 * iii. Delete vif1.0.
1319 * iv. Re-create vif1.0.
1321 * (XenServer Tools 5.5.0 does not exhibit this behavior, and
1322 * neither does a VM without Tools installed at all.@.)
1324 * Steps iii and iv happen within a few seconds of each other.
1325 * Step iv causes /etc/xensource/scripts/vif to run, which in
1326 * turn calls ovs-cfg-mod to add the new device to the bridge.
1327 * If step iv happens after step 4 (in our first list of
1328 * steps), then all is well, but if it happens between 3 and 4
1329 * (which can easily happen if ovs-brcompatd has to wait to
1330 * lock the configuration file), then we will remove the new
1331 * incarnation from the bridge instead of the old one!
1333 * So, to avoid this problem, we do nothing here. This is
1334 * strictly incorrect except for this one particular case, and
1335 * perhaps that will bite us someday. If that happens, then we
1336 * will have to somehow track network devices by ifindex, since
1337 * a new device will have a new ifindex even if it has the same
1338 * name as an old device.
1340 VLOG_INFO("kernel reported network device %s removed but "
1341 "a device by that name exists (XS Tools 5.0.0?)",
1350 main(int argc, char *argv[])
1352 struct unixctl_server *unixctl;
1354 struct ovsdb_idl *idl;
1357 proctitle_init(argc, argv);
1358 set_program_name(argv[0]);
1361 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
1362 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
1364 remote = parse_options(argc, argv);
1365 signal(SIGPIPE, SIG_IGN);
1369 die_if_already_running();
1372 retval = unixctl_server_create(NULL, &unixctl);
1377 if (brc_open(&brc_sock)) {
1378 ovs_fatal(0, "could not open brcompat socket. Check "
1379 "\"brcompat\" kernel module.");
1382 if (prune_timeout) {
1383 if (nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &rtnl_sock)) {
1384 ovs_fatal(0, "could not create rtnetlink socket");
1388 daemonize_complete();
1390 idl = ovsdb_idl_create(remote, &ovsrec_idl_class);
1393 const struct ovsrec_open_vswitch *ovs;
1397 unixctl_server_run(unixctl);
1398 brc_recv_update(idl);
1400 ovs = ovsrec_open_vswitch_first(idl);
1401 if (!ovs && ovsdb_idl_has_ever_connected(idl)) {
1402 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1403 VLOG_WARN_RL(&rl, "%s: database does not contain any Open vSwitch "
1404 "configuration", remote);
1408 /* If 'prune_timeout' is non-zero, we actively prune from the
1409 * configuration of port entries that are no longer valid. We
1412 * 1) The kernel explicitly notifies us of removed ports
1413 * through the RTNL messages.
1415 * 2) We periodically check all ports associated with bridges
1416 * to see if they no longer exist.
1418 if (ovs && prune_timeout) {
1419 rtnl_recv_update(idl, ovs);
1424 nl_sock_wait(rtnl_sock, POLLIN);
1425 poll_timer_wait(prune_timeout);
1429 nl_sock_wait(brc_sock, POLLIN);
1430 ovsdb_idl_wait(idl);
1431 unixctl_server_wait(unixctl);
1436 ovsdb_idl_destroy(idl);
1442 validate_appctl_command(void)
1448 for (p = strchr(appctl_command, '%'); p; p = strchr(p + 2, '%')) {
1450 /* Nothing to do. */
1451 } else if (p[1] == 's') {
1454 ovs_fatal(0, "only '%%s' and '%%%%' allowed in --appctl-command");
1458 ovs_fatal(0, "'%%s' must appear exactly once in --appctl-command");
1463 parse_options(int argc, char *argv[])
1469 LEAK_CHECKER_OPTION_ENUMS
1471 static struct option long_options[] = {
1472 {"help", no_argument, 0, 'h'},
1473 {"version", no_argument, 0, 'V'},
1474 {"prune-timeout", required_argument, 0, OPT_PRUNE_TIMEOUT},
1475 {"appctl-command", required_argument, 0, OPT_APPCTL_COMMAND},
1476 DAEMON_LONG_OPTIONS,
1478 LEAK_CHECKER_LONG_OPTIONS,
1481 char *short_options = long_options_to_short_options(long_options);
1483 appctl_command = xasprintf("%s/ovs-appctl %%s", ovs_bindir);
1487 c = getopt_long(argc, argv, short_options, long_options, NULL);
1498 OVS_PRINT_VERSION(0, 0);
1501 case OPT_PRUNE_TIMEOUT:
1502 prune_timeout = atoi(optarg) * 1000;
1505 case OPT_APPCTL_COMMAND:
1506 appctl_command = optarg;
1509 VLOG_OPTION_HANDLERS
1510 DAEMON_OPTION_HANDLERS
1511 LEAK_CHECKER_OPTION_HANDLERS
1520 free(short_options);
1522 validate_appctl_command();
1528 ovs_fatal(0, "database socket is non-option argument; "
1529 "use --help for usage");
1538 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
1539 "usage: %s [OPTIONS] CONFIG\n"
1540 "CONFIG is the configuration file used by ovs-vswitchd.\n",
1541 program_name, program_name);
1542 printf("\nConfiguration options:\n"
1543 " --appctl-command=COMMAND shell command to run ovs-appctl\n"
1544 " --prune-timeout=SECS wait at most SECS before pruning ports\n"
1548 printf("\nOther options:\n"
1549 " -h, --help display this help message\n"
1550 " -V, --version display version information\n");
1551 leak_checker_usage();
1552 printf("\nThe default appctl command is:\n%s\n", appctl_command);