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"
59 #include "vswitchd/vswitch-idl.h"
61 VLOG_DEFINE_THIS_MODULE(brcompatd);
64 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
66 /* Actions to modify bridge compatibility configuration. */
74 static const char *parse_options(int argc, char *argv[]);
75 static void usage(void) NO_RETURN;
77 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
79 /* Maximum number of milliseconds to wait before pruning port entries that
80 * no longer exist. If set to zero, ports are never pruned. */
81 static int prune_timeout = 5000;
83 /* Shell command to execute (via popen()) to send a control command to the
84 * running ovs-vswitchd process. The string must contain one instance of %s,
85 * which is replaced by the control command. */
86 static char *appctl_command;
88 /* Netlink socket to listen for interface changes. */
89 static struct nl_sock *rtnl_sock;
91 /* Netlink socket to bridge compatibility kernel module. */
92 static struct nl_sock *brc_sock;
94 /* The Generic Netlink family number used for bridge compatibility. */
95 static int brc_family;
97 static const struct nl_policy brc_multicast_policy[] = {
98 [BRC_GENL_A_MC_GROUP] = {.type = NL_A_U32 }
101 static const struct nl_policy rtnlgrp_link_policy[] = {
102 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
103 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
107 lookup_brc_multicast_group(int *multicast_group)
109 struct nl_sock *sock;
110 struct ofpbuf request, *reply;
111 struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
114 retval = nl_sock_create(NETLINK_GENERIC, &sock);
118 ofpbuf_init(&request, 0);
119 nl_msg_put_genlmsghdr(&request, 0, brc_family,
120 NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
121 retval = nl_sock_transact(sock, &request, &reply);
122 ofpbuf_uninit(&request);
124 nl_sock_destroy(sock);
127 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
128 brc_multicast_policy, attrs,
129 ARRAY_SIZE(brc_multicast_policy))) {
130 nl_sock_destroy(sock);
131 ofpbuf_delete(reply);
134 *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
135 nl_sock_destroy(sock);
136 ofpbuf_delete(reply);
141 /* Opens a socket for brcompat notifications. Returns 0 if successful,
142 * otherwise a positive errno value. */
144 brc_open(struct nl_sock **sock)
146 int multicast_group = 0;
149 retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
154 retval = lookup_brc_multicast_group(&multicast_group);
159 retval = nl_sock_create(NETLINK_GENERIC, sock);
164 retval = nl_sock_join_mcgroup(*sock, multicast_group);
166 nl_sock_destroy(*sock);
172 static const struct nl_policy brc_dp_policy[] = {
173 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
176 static struct ovsrec_bridge *
177 find_bridge(const struct ovsrec_open_vswitch *ovs, const char *br_name)
181 for (i = 0; i < ovs->n_bridges; i++) {
182 if (!strcmp(br_name, ovs->bridges[i]->name)) {
183 return ovs->bridges[i];
191 execute_appctl_command(const char *unixctl_command, char **output)
193 char *stdout_log, *stderr_log;
199 argv[2] = xasprintf(appctl_command, unixctl_command);
202 /* Run process and log status. */
203 error = process_run_capture(argv, &stdout_log, &stderr_log, &status);
205 VLOG_ERR("failed to execute %s command via ovs-appctl: %s",
206 unixctl_command, strerror(error));
208 char *msg = process_status_msg(status);
209 VLOG_ERR("ovs-appctl exited with error (%s)", msg);
214 /* Deal with stdout_log. */
216 *output = stdout_log;
221 /* Deal with stderr_log */
222 if (stderr_log && *stderr_log) {
223 VLOG_INFO("ovs-appctl wrote to stderr:\n%s", stderr_log);
233 do_get_bridge_parts(const struct ovsrec_bridge *br, struct svec *parts,
234 int vlan, bool break_down_bonds)
240 for (i = 0; i < br->n_ports; i++) {
241 const struct ovsrec_port *port = br->ports[i];
243 svec_add(&ports, port->name);
245 int port_vlan = port->n_tag ? *port->tag : 0;
246 if (vlan != port_vlan) {
250 if (break_down_bonds) {
251 for (j = 0; j < port->n_interfaces; j++) {
252 const struct ovsrec_interface *iface = port->interfaces[j];
253 svec_add(parts, iface->name);
256 svec_add(parts, port->name);
259 svec_destroy(&ports);
262 /* Add all the interfaces for 'bridge' to 'ifaces', breaking bonded interfaces
263 * down into their constituent parts.
265 * If 'vlan' < 0, all interfaces on 'bridge' are reported. If 'vlan' == 0,
266 * then only interfaces for trunk ports or ports with implicit VLAN 0 are
267 * reported. If 'vlan' > 0, only interfaces with implicit VLAN 'vlan' are
270 get_bridge_ifaces(const struct ovsrec_bridge *br, struct svec *ifaces,
273 do_get_bridge_parts(br, ifaces, vlan, true);
276 /* Add all the ports for 'bridge' to 'ports'. Bonded ports are reported under
277 * the bond name, not broken down into their constituent interfaces.
279 * If 'vlan' < 0, all ports on 'bridge' are reported. If 'vlan' == 0, then
280 * only trunk ports or ports with implicit VLAN 0 are reported. If 'vlan' > 0,
281 * only port with implicit VLAN 'vlan' are reported. */
283 get_bridge_ports(const struct ovsrec_bridge *br, struct svec *ports,
286 do_get_bridge_parts(br, ports, vlan, false);
289 static struct ovsdb_idl_txn *
290 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
292 return ovsdb_idl_txn_get(&ovs->header_);
296 port_is_fake_bridge(const struct ovsrec_port *port)
298 return (port->fake_bridge
300 && *port->tag >= 1 && *port->tag <= 4095);
304 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
305 struct ovsrec_bridge *bridge)
307 struct ovsrec_bridge **bridges;
310 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
311 for (i = 0; i < ovs->n_bridges; i++) {
312 bridges[i] = ovs->bridges[i];
314 bridges[ovs->n_bridges] = bridge;
315 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
320 where_uuid_equals(const struct uuid *uuid)
325 json_string_create("_uuid"),
326 json_string_create("=="),
328 json_string_create("uuid"),
329 json_string_create_nocopy(
330 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
333 /* Commits 'txn'. If 'wait_for_reload' is true, also waits for Open vSwitch to
334 reload the configuration before returning.
336 Returns EAGAIN if the caller should try the operation again, 0 on success,
337 otherwise a positive errno value. */
339 commit_txn(struct ovsdb_idl_txn *txn, bool wait_for_reload)
341 struct ovsdb_idl *idl = ovsdb_idl_txn_get_idl (txn);
342 enum ovsdb_idl_txn_status status;
343 int64_t next_cfg = 0;
345 if (wait_for_reload) {
346 const struct ovsrec_open_vswitch *ovs = ovsrec_open_vswitch_first(idl);
347 struct json *where = where_uuid_equals(&ovs->header_.uuid);
348 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
351 status = ovsdb_idl_txn_commit_block(txn);
352 if (wait_for_reload && status == TXN_SUCCESS) {
353 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
355 ovsdb_idl_txn_destroy(txn);
362 VLOG_ERR_RL(&rl, "OVSDB transaction unexpectedly aborted");
369 if (wait_for_reload) {
371 /* We can't use 'ovs' any longer because ovsdb_idl_run() can
373 const struct ovsrec_open_vswitch *ovs2;
376 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs2, idl) {
377 if (ovs2->cur_cfg >= next_cfg) {
389 VLOG_ERR_RL(&rl, "OVSDB transaction needs retry");
393 VLOG_ERR_RL(&rl, "OVSDB transaction failed: %s",
394 ovsdb_idl_txn_get_error(txn));
403 add_bridge(struct ovsdb_idl *idl, const struct ovsrec_open_vswitch *ovs,
406 struct ovsrec_bridge *br;
407 struct ovsrec_port *port;
408 struct ovsrec_interface *iface;
409 struct ovsdb_idl_txn *txn;
411 if (find_bridge(ovs, br_name)) {
412 VLOG_WARN("addbr %s: bridge %s exists", br_name, br_name);
414 } else if (netdev_exists(br_name)) {
417 for (i = 0; i < ovs->n_bridges; i++) {
419 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
421 for (j = 0; j < br_cfg->n_ports; j++) {
422 if (port_is_fake_bridge(br_cfg->ports[j])) {
423 VLOG_WARN("addbr %s: %s exists as a fake bridge",
430 VLOG_WARN("addbr %s: cannot create bridge %s because a network "
431 "device named %s already exists",
432 br_name, br_name, br_name);
436 txn = ovsdb_idl_txn_create(idl);
438 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: addbr %s", br_name);
440 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
441 ovsrec_interface_set_name(iface, br_name);
443 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
444 ovsrec_port_set_name(port, br_name);
445 ovsrec_port_set_interfaces(port, &iface, 1);
447 br = ovsrec_bridge_insert(txn_from_openvswitch(ovs));
448 ovsrec_bridge_set_name(br, br_name);
449 ovsrec_bridge_set_ports(br, &port, 1);
451 ovs_insert_bridge(ovs, br);
453 return commit_txn(txn, true);
457 add_port(const struct ovsrec_open_vswitch *ovs,
458 const struct ovsrec_bridge *br, const char *port_name)
460 struct ovsrec_interface *iface;
461 struct ovsrec_port *port;
462 struct ovsrec_port **ports;
465 /* xxx Check conflicts? */
466 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
467 ovsrec_interface_set_name(iface, port_name);
469 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
470 ovsrec_port_set_name(port, port_name);
471 ovsrec_port_set_interfaces(port, &iface, 1);
473 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
474 for (i = 0; i < br->n_ports; i++) {
475 ports[i] = br->ports[i];
477 ports[br->n_ports] = port;
478 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
482 /* Deletes 'port' from 'br'.
484 * After calling this function, 'port' must not be referenced again. */
486 del_port(const struct ovsrec_bridge *br, const struct ovsrec_port *port)
488 struct ovsrec_port **ports;
491 /* Remove 'port' from the bridge's list of ports. */
492 ports = xmalloc(sizeof *br->ports * br->n_ports);
493 for (i = n = 0; i < br->n_ports; i++) {
494 if (br->ports[i] != port) {
495 ports[n++] = br->ports[i];
498 ovsrec_bridge_set_ports(br, ports, n);
502 /* Delete 'iface' from 'port' (which must be within 'br'). If 'iface' was
503 * 'port''s only interface, delete 'port' from 'br' also.
505 * After calling this function, 'iface' must not be referenced again. */
507 del_interface(const struct ovsrec_bridge *br,
508 const struct ovsrec_port *port,
509 const struct ovsrec_interface *iface)
511 if (port->n_interfaces == 1) {
514 struct ovsrec_interface **ifaces;
517 ifaces = xmalloc(sizeof *port->interfaces * port->n_interfaces);
518 for (i = n = 0; i < port->n_interfaces; i++) {
519 if (port->interfaces[i] != iface) {
520 ifaces[n++] = port->interfaces[i];
523 ovsrec_port_set_interfaces(port, ifaces, n);
528 /* Find and return a port within 'br' named 'port_name'. */
529 static const struct ovsrec_port *
530 find_port(const struct ovsrec_bridge *br, const char *port_name)
534 for (i = 0; i < br->n_ports; i++) {
535 struct ovsrec_port *port = br->ports[i];
536 if (!strcmp(port_name, port->name)) {
543 /* Find and return an interface within 'br' named 'iface_name'. */
544 static const struct ovsrec_interface *
545 find_interface(const struct ovsrec_bridge *br, const char *iface_name,
546 struct ovsrec_port **portp)
550 for (i = 0; i < br->n_ports; i++) {
551 struct ovsrec_port *port = br->ports[i];
554 for (j = 0; j < port->n_interfaces; j++) {
555 struct ovsrec_interface *iface = port->interfaces[j];
556 if (!strcmp(iface->name, iface_name)) {
568 del_bridge(struct ovsdb_idl *idl,
569 const struct ovsrec_open_vswitch *ovs, const char *br_name)
571 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
572 struct ovsrec_bridge **bridges;
573 struct ovsdb_idl_txn *txn;
577 VLOG_WARN("delbr %s: no bridge named %s", br_name, br_name);
581 txn = ovsdb_idl_txn_create(idl);
583 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: delbr %s", br_name);
585 /* Remove 'br' from the vswitch's list of bridges. */
586 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
587 for (i = n = 0; i < ovs->n_bridges; i++) {
588 if (ovs->bridges[i] != br) {
589 bridges[n++] = ovs->bridges[i];
592 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
595 return commit_txn(txn, true);
599 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
600 const char **port_name, uint64_t *count, uint64_t *skip)
602 static const struct nl_policy policy[] = {
603 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING, .optional = true },
604 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
605 [BRC_GENL_A_FDB_COUNT] = { .type = NL_A_U64, .optional = true },
606 [BRC_GENL_A_FDB_SKIP] = { .type = NL_A_U64, .optional = true },
608 struct nlattr *attrs[ARRAY_SIZE(policy)];
610 if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
611 attrs, ARRAY_SIZE(policy))
612 || (br_name && !attrs[BRC_GENL_A_DP_NAME])
613 || (port_name && !attrs[BRC_GENL_A_PORT_NAME])
614 || (count && !attrs[BRC_GENL_A_FDB_COUNT])
615 || (skip && !attrs[BRC_GENL_A_FDB_SKIP])) {
619 *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
621 *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
624 *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
627 *count = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_COUNT]);
630 *skip = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_SKIP]);
635 /* Composes and returns a reply to a request made by the datapath with Netlink
636 * sequence number 'seq' and error code 'error'. The caller may add additional
637 * attributes to the message, then it may send it with send_reply(). */
638 static struct ofpbuf *
639 compose_reply(uint32_t seq, int error)
641 struct ofpbuf *reply = ofpbuf_new(4096);
642 nl_msg_put_genlmsghdr(reply, 32, brc_family, NLM_F_REQUEST,
643 BRC_GENL_C_DP_RESULT, 1);
644 ((struct nlmsghdr *) reply->data)->nlmsg_seq = seq;
645 nl_msg_put_u32(reply, BRC_GENL_A_ERR_CODE, error);
649 /* Sends 'reply' to the datapath and frees it. */
651 send_reply(struct ofpbuf *reply)
653 int retval = nl_sock_send(brc_sock, reply, false);
655 VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
658 ofpbuf_delete(reply);
661 /* Composes and sends a reply to a request made by the datapath with Netlink
662 * sequence number 'seq' and error code 'error'. */
664 send_simple_reply(uint32_t seq, int error)
666 send_reply(compose_reply(seq, error));
670 handle_bridge_cmd(struct ovsdb_idl *idl,
671 const struct ovsrec_open_vswitch *ovs,
672 struct ofpbuf *buffer, bool add)
678 error = parse_command(buffer, &seq, &br_name, NULL, NULL, NULL);
683 retval = (add ? add_bridge : del_bridge)(idl, ovs, br_name);
684 VLOG_INFO_RL(&rl, "%sbr %s: %s",
685 add ? "add" : "del", br_name, strerror(retval));
686 } while (retval == EAGAIN);
688 send_simple_reply(seq, error);
693 static const struct nl_policy brc_port_policy[] = {
694 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
695 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
699 handle_port_cmd(struct ovsdb_idl *idl,
700 const struct ovsrec_open_vswitch *ovs,
701 struct ofpbuf *buffer, bool add)
703 const char *cmd_name = add ? "add-if" : "del-if";
704 const char *br_name, *port_name;
708 error = parse_command(buffer, &seq, &br_name, &port_name, NULL, NULL);
710 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
713 VLOG_WARN("%s %s %s: no bridge named %s",
714 cmd_name, br_name, port_name, br_name);
716 } else if (!netdev_exists(port_name)) {
717 VLOG_WARN("%s %s %s: no network device named %s",
718 cmd_name, br_name, port_name, port_name);
722 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
725 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: add-if %s",
727 add_port(ovs, br, port_name);
729 const struct ovsrec_port *port = find_port(br, port_name);
731 ovsdb_idl_txn_add_comment(txn,
732 "ovs-brcompatd: del-if %s",
738 error = commit_txn(txn, true);
739 VLOG_INFO_RL(&rl, "%s %s %s: %s",
740 cmd_name, br_name, port_name, strerror(error));
741 } while (error == EAGAIN);
743 send_simple_reply(seq, error);
749 /* The caller is responsible for freeing '*ovs_name' if the call is
752 linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch *ovs,
753 const char *linux_name,
754 const struct ovsrec_bridge **ovs_bridge,
757 *ovs_bridge = find_bridge(ovs, linux_name);
759 /* Bridge name is the same. We are interested in VLAN 0. */
763 /* No such Open vSwitch bridge 'linux_name', but there might be an
764 * internal port named 'linux_name' on some other bridge
765 * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
766 * port 'linux_name' on the bridge named 'ovs_bridge'. */
769 for (i = 0; i < ovs->n_bridges; i++) {
770 const struct ovsrec_bridge *br = ovs->bridges[i];
772 for (j = 0; j < br->n_ports; j++) {
773 const struct ovsrec_port *port = br->ports[j];
775 if (!strcmp(port->name, linux_name)) {
777 *br_vlan = port->n_tag ? *port->tag : -1;
788 handle_fdb_query_cmd(const struct ovsrec_open_vswitch *ovs,
789 struct ofpbuf *buffer)
791 /* This structure is copied directly from the Linux 2.6.30 header files.
792 * It would be more straightforward to #include <linux/if_bridge.h>, but
793 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
794 * with old header files won't have it. */
799 __u32 ageing_timer_value;
808 struct mac *local_macs;
812 /* Impedance matching between the vswitchd and Linux kernel notions of what
813 * a bridge is. The kernel only handles a single VLAN per bridge, but
814 * vswitchd can deal with all the VLANs on a single bridge. We have to
815 * pretend that the former is the case even though the latter is the
817 const char *linux_name; /* Name used by brctl. */
818 const struct ovsrec_bridge *ovs_bridge; /* Bridge used by ovs-vswitchd. */
819 int br_vlan; /* VLAN tag. */
822 struct ofpbuf query_data;
823 struct ofpbuf *reply;
824 char *unixctl_command;
825 uint64_t count, skip;
831 /* Parse the command received from brcompat_mod. */
832 error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
837 /* Figure out vswitchd bridge and VLAN. */
838 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
839 &ovs_bridge, &br_vlan);
841 send_simple_reply(seq, error);
845 /* Fetch the forwarding database using ovs-appctl. */
846 unixctl_command = xasprintf("fdb/show %s", ovs_bridge->name);
847 error = execute_appctl_command(unixctl_command, &output);
848 free(unixctl_command);
850 send_simple_reply(seq, error);
854 /* Fetch the MAC address for each interface on the bridge, so that we can
855 * fill in the is_local field in the response. */
857 get_bridge_ifaces(ovs_bridge, &ifaces, br_vlan);
858 local_macs = xmalloc(ifaces.n * sizeof *local_macs);
860 for (i = 0; i < ifaces.n; i++) {
861 const char *iface_name = ifaces.names[i];
862 struct mac *mac = &local_macs[n_local_macs];
863 struct netdev *netdev;
865 error = netdev_open_default(iface_name, &netdev);
867 if (!netdev_get_etheraddr(netdev, mac->addr)) {
870 netdev_close(netdev);
873 svec_destroy(&ifaces);
875 /* Parse the response from ovs-appctl and convert it to binary format to
876 * pass back to the kernel. */
877 ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
879 strtok_r(output, "\n", &save_ptr); /* Skip header line. */
881 struct __fdb_entry *entry;
883 uint8_t mac[ETH_ADDR_LEN];
887 line = strtok_r(NULL, "\n", &save_ptr);
892 if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
893 &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
894 != 2 + ETH_ADDR_SCAN_COUNT + 1) {
895 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
896 VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
900 if (vlan != br_vlan) {
909 /* Is this the MAC address of an interface on the bridge? */
911 for (i = 0; i < n_local_macs; i++) {
912 if (eth_addr_equals(local_macs[i].addr, mac)) {
918 entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
919 memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
920 entry->port_no = port & 0xff;
921 entry->is_local = is_local;
922 entry->ageing_timer_value = age * HZ;
923 entry->port_hi = (port & 0xff00) >> 8;
930 /* Compose and send reply to datapath. */
931 reply = compose_reply(seq, 0);
932 nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
933 query_data.data, query_data.size);
937 ofpbuf_uninit(&query_data);
944 send_ifindex_reply(uint32_t seq, struct svec *ifaces)
946 struct ofpbuf *reply;
952 /* Make sure that any given interface only occurs once. This shouldn't
953 * happen, but who knows what people put into their configuration files. */
954 svec_sort_unique(ifaces);
956 /* Convert 'ifaces' into ifindexes. */
958 indices = xmalloc(ifaces->n * sizeof *indices);
959 SVEC_FOR_EACH (i, iface, ifaces) {
960 int ifindex = if_nametoindex(iface);
962 indices[n_indices++] = ifindex;
966 /* Compose and send reply. */
967 reply = compose_reply(seq, 0);
968 nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
969 indices, n_indices * sizeof *indices);
977 handle_get_bridges_cmd(const struct ovsrec_open_vswitch *ovs,
978 struct ofpbuf *buffer)
987 /* Parse Netlink command.
989 * The command doesn't actually have any arguments, but we need the
990 * sequence number to send the reply. */
991 error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
996 /* Get all the real bridges and all the fake ones. */
998 for (i = 0; i < ovs->n_bridges; i++) {
999 const struct ovsrec_bridge *br = ovs->bridges[i];
1001 svec_add(&bridges, br->name);
1002 for (j = 0; j < br->n_ports; j++) {
1003 const struct ovsrec_port *port = br->ports[j];
1005 if (port->fake_bridge) {
1006 svec_add(&bridges, port->name);
1011 send_ifindex_reply(seq, &bridges);
1012 svec_destroy(&bridges);
1018 handle_get_ports_cmd(const struct ovsrec_open_vswitch *ovs,
1019 struct ofpbuf *buffer)
1023 const char *linux_name;
1024 const struct ovsrec_bridge *ovs_bridge;
1031 /* Parse Netlink command. */
1032 error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
1037 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
1038 &ovs_bridge, &br_vlan);
1040 send_simple_reply(seq, error);
1045 get_bridge_ports(ovs_bridge, &ports, br_vlan);
1047 svec_del(&ports, linux_name);
1048 send_ifindex_reply(seq, &ports); /* XXX bonds won't show up */
1049 svec_destroy(&ports);
1054 static struct ofpbuf *
1055 brc_recv_update__(void)
1058 struct ofpbuf *buffer;
1061 retval = nl_sock_recv(brc_sock, &buffer, false);
1064 if (nl_msg_nlmsgerr(buffer, NULL)
1065 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE) {
1077 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
1080 ofpbuf_delete(buffer);
1085 brc_recv_update(struct ovsdb_idl *idl)
1087 struct ofpbuf *buffer;
1088 struct genlmsghdr *genlmsghdr;
1089 const struct ovsrec_open_vswitch *ovs;
1091 buffer = brc_recv_update__();
1096 genlmsghdr = nl_msg_genlmsghdr(buffer);
1098 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
1102 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
1103 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
1104 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
1108 /* Get the Open vSwitch configuration. Just drop the request on the floor
1109 * if a valid configuration doesn't exist. (We could check this earlier,
1110 * but we want to drain pending Netlink messages even when there is no Open
1111 * vSwitch configuration.) */
1112 ovs = ovsrec_open_vswitch_first(idl);
1114 VLOG_WARN_RL(&rl, "could not find valid configuration to update");
1118 switch (genlmsghdr->cmd) {
1119 case BRC_GENL_C_DP_ADD:
1120 handle_bridge_cmd(idl, ovs, buffer, true);
1123 case BRC_GENL_C_DP_DEL:
1124 handle_bridge_cmd(idl, ovs, buffer, false);
1127 case BRC_GENL_C_PORT_ADD:
1128 handle_port_cmd(idl, ovs, buffer, true);
1131 case BRC_GENL_C_PORT_DEL:
1132 handle_port_cmd(idl, ovs, buffer, false);
1135 case BRC_GENL_C_FDB_QUERY:
1136 handle_fdb_query_cmd(ovs, buffer);
1139 case BRC_GENL_C_GET_BRIDGES:
1140 handle_get_bridges_cmd(ovs, buffer);
1143 case BRC_GENL_C_GET_PORTS:
1144 handle_get_ports_cmd(ovs, buffer);
1148 VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
1154 ofpbuf_delete(buffer);
1158 /* Check for interface configuration changes announced through RTNL. */
1160 rtnl_recv_update(struct ovsdb_idl *idl,
1161 const struct ovsrec_open_vswitch *ovs)
1165 int error = nl_sock_recv(rtnl_sock, &buf, false);
1166 if (error == EAGAIN) {
1167 /* Nothing to do. */
1168 } else if (error == ENOBUFS) {
1169 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
1171 VLOG_WARN_RL(&rl, "error on network monitor socket: %s",
1174 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1175 struct nlmsghdr *nlh;
1176 struct ifinfomsg *iim;
1178 nlh = ofpbuf_at(buf, 0, NLMSG_HDRLEN);
1179 iim = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *iim);
1181 VLOG_WARN_RL(&rl, "received bad rtnl message (no ifinfomsg)");
1186 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1187 rtnlgrp_link_policy,
1188 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1189 VLOG_WARN_RL(&rl,"received bad rtnl message (policy)");
1193 if (nlh->nlmsg_type == RTM_DELLINK && attrs[IFLA_MASTER]) {
1194 const char *port_name = nl_attr_get_string(attrs[IFLA_IFNAME]);
1195 char br_name[IFNAMSIZ];
1196 uint32_t br_idx = nl_attr_get_u32(attrs[IFLA_MASTER]);
1198 if (!if_indextoname(br_idx, br_name)) {
1203 if (!netdev_exists(port_name)) {
1204 /* Network device is really gone. */
1205 struct ovsdb_idl_txn *txn;
1206 const struct ovsrec_interface *iface;
1207 struct ovsrec_port *port;
1208 struct ovsrec_bridge *br;
1210 VLOG_INFO("network device %s destroyed, "
1211 "removing from bridge %s", port_name, br_name);
1213 br = find_bridge(ovs, br_name);
1215 VLOG_WARN("no bridge named %s from which to remove %s",
1216 br_name, port_name);
1221 txn = ovsdb_idl_txn_create(idl);
1223 iface = find_interface(br, port_name, &port);
1225 del_interface(br, port, iface);
1226 ovsdb_idl_txn_add_comment(txn,
1227 "ovs-brcompatd: destroy port %s",
1231 commit_txn(txn, false);
1233 /* A network device by that name exists even though the kernel
1234 * told us it had disappeared. Probably, what happened was
1237 * 1. Device destroyed.
1238 * 2. Notification sent to us.
1239 * 3. New device created with same name as old one.
1240 * 4. ovs-brcompatd notified, removes device from bridge.
1242 * There's no a priori reason that in this situation that the
1243 * new device with the same name should remain in the bridge;
1244 * on the contrary, that would be unexpected. *But* there is
1245 * one important situation where, if we do this, bad things
1246 * happen. This is the case of XenServer Tools version 5.0.0,
1247 * which on boot of a Windows VM cause something like this to
1248 * happen on the Xen host:
1250 * i. Create tap1.0 and vif1.0.
1251 * ii. Delete tap1.0.
1252 * iii. Delete vif1.0.
1253 * iv. Re-create vif1.0.
1255 * (XenServer Tools 5.5.0 does not exhibit this behavior, and
1256 * neither does a VM without Tools installed at all.@.)
1258 * Steps iii and iv happen within a few seconds of each other.
1259 * Step iv causes /etc/xensource/scripts/vif to run, which in
1260 * turn calls ovs-cfg-mod to add the new device to the bridge.
1261 * If step iv happens after step 4 (in our first list of
1262 * steps), then all is well, but if it happens between 3 and 4
1263 * (which can easily happen if ovs-brcompatd has to wait to
1264 * lock the configuration file), then we will remove the new
1265 * incarnation from the bridge instead of the old one!
1267 * So, to avoid this problem, we do nothing here. This is
1268 * strictly incorrect except for this one particular case, and
1269 * perhaps that will bite us someday. If that happens, then we
1270 * will have to somehow track network devices by ifindex, since
1271 * a new device will have a new ifindex even if it has the same
1272 * name as an old device.
1274 VLOG_INFO("kernel reported network device %s removed but "
1275 "a device by that name exists (XS Tools 5.0.0?)",
1284 main(int argc, char *argv[])
1286 extern struct vlog_module VLM_reconnect;
1287 struct unixctl_server *unixctl;
1289 struct ovsdb_idl *idl;
1292 proctitle_init(argc, argv);
1293 set_program_name(argv[0]);
1294 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
1295 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
1297 remote = parse_options(argc, argv);
1298 signal(SIGPIPE, SIG_IGN);
1302 die_if_already_running();
1305 retval = unixctl_server_create(NULL, &unixctl);
1310 if (brc_open(&brc_sock)) {
1311 ovs_fatal(0, "could not open brcompat socket. Check "
1312 "\"brcompat\" kernel module.");
1315 if (prune_timeout) {
1318 error = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
1320 ovs_fatal(error, "could not create rtnetlink socket");
1323 error = nl_sock_join_mcgroup(rtnl_sock, RTNLGRP_LINK);
1325 ovs_fatal(error, "could not join RTNLGRP_LINK multicast group");
1329 daemonize_complete();
1331 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
1334 const struct ovsrec_open_vswitch *ovs;
1338 unixctl_server_run(unixctl);
1339 brc_recv_update(idl);
1341 ovs = ovsrec_open_vswitch_first(idl);
1342 if (!ovs && ovsdb_idl_has_ever_connected(idl)) {
1343 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1344 VLOG_WARN_RL(&rl, "%s: database does not contain any Open vSwitch "
1345 "configuration", remote);
1349 /* If 'prune_timeout' is non-zero, we actively prune from the
1350 * configuration of port entries that are no longer valid. We
1353 * 1) The kernel explicitly notifies us of removed ports
1354 * through the RTNL messages.
1356 * 2) We periodically check all ports associated with bridges
1357 * to see if they no longer exist.
1359 if (ovs && prune_timeout) {
1360 rtnl_recv_update(idl, ovs);
1361 nl_sock_wait(rtnl_sock, POLLIN);
1362 poll_timer_wait(prune_timeout);
1366 nl_sock_wait(brc_sock, POLLIN);
1367 ovsdb_idl_wait(idl);
1368 unixctl_server_wait(unixctl);
1373 ovsdb_idl_destroy(idl);
1379 validate_appctl_command(void)
1385 for (p = strchr(appctl_command, '%'); p; p = strchr(p + 2, '%')) {
1387 /* Nothing to do. */
1388 } else if (p[1] == 's') {
1391 ovs_fatal(0, "only '%%s' and '%%%%' allowed in --appctl-command");
1395 ovs_fatal(0, "'%%s' must appear exactly once in --appctl-command");
1400 parse_options(int argc, char *argv[])
1406 LEAK_CHECKER_OPTION_ENUMS,
1409 static struct option long_options[] = {
1410 {"help", no_argument, 0, 'h'},
1411 {"version", no_argument, 0, 'V'},
1412 {"prune-timeout", required_argument, 0, OPT_PRUNE_TIMEOUT},
1413 {"appctl-command", required_argument, 0, OPT_APPCTL_COMMAND},
1414 DAEMON_LONG_OPTIONS,
1416 LEAK_CHECKER_LONG_OPTIONS,
1419 char *short_options = long_options_to_short_options(long_options);
1421 appctl_command = xasprintf("%s/ovs-appctl %%s", ovs_bindir());
1425 c = getopt_long(argc, argv, short_options, long_options, NULL);
1436 OVS_PRINT_VERSION(0, 0);
1439 case OPT_PRUNE_TIMEOUT:
1440 prune_timeout = atoi(optarg) * 1000;
1443 case OPT_APPCTL_COMMAND:
1444 appctl_command = optarg;
1447 VLOG_OPTION_HANDLERS
1448 DAEMON_OPTION_HANDLERS
1449 LEAK_CHECKER_OPTION_HANDLERS
1458 free(short_options);
1460 validate_appctl_command();
1466 ovs_fatal(0, "database socket is non-option argument; "
1467 "use --help for usage");
1476 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
1477 "usage: %s [OPTIONS] CONFIG\n"
1478 "CONFIG is the configuration file used by ovs-vswitchd.\n",
1479 program_name, program_name);
1480 printf("\nConfiguration options:\n"
1481 " --appctl-command=COMMAND shell command to run ovs-appctl\n"
1482 " --prune-timeout=SECS wait at most SECS before pruning ports\n"
1486 printf("\nOther options:\n"
1487 " -h, --help display this help message\n"
1488 " -V, --version display version information\n");
1489 leak_checker_usage();
1490 printf("\nThe default appctl command is:\n%s\n", appctl_command);