1 /* Copyright (c) 2008, 2009 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"
42 #include "leak-checker.h"
46 #include "openvswitch/brcompat-netlink.h"
47 #include "ovsdb-idl.h"
49 #include "poll-loop.h"
56 #include "vswitchd/vswitch-idl.h"
59 #define THIS_MODULE VLM_brcompatd
62 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
64 /* Actions to modify bridge compatibility configuration. */
72 static const char *parse_options(int argc, char *argv[]);
73 static void usage(void) NO_RETURN;
75 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
77 /* Maximum number of milliseconds to wait before pruning port entries that
78 * no longer exist. If set to zero, ports are never pruned. */
79 static int prune_timeout = 5000;
81 /* Shell command to execute (via popen()) to send a control command to the
82 * running ovs-vswitchd process. The string must contain one instance of %s,
83 * which is replaced by the control command. */
84 static char *appctl_command;
86 /* Netlink socket to listen for interface changes. */
87 static struct nl_sock *rtnl_sock;
89 /* Netlink socket to bridge compatibility kernel module. */
90 static struct nl_sock *brc_sock;
92 /* The Generic Netlink family number used for bridge compatibility. */
93 static int brc_family;
95 static const struct nl_policy brc_multicast_policy[] = {
96 [BRC_GENL_A_MC_GROUP] = {.type = NL_A_U32 }
99 static const struct nl_policy rtnlgrp_link_policy[] = {
100 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
101 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
105 lookup_brc_multicast_group(int *multicast_group)
107 struct nl_sock *sock;
108 struct ofpbuf request, *reply;
109 struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
112 retval = nl_sock_create(NETLINK_GENERIC, 0, 0, 0, &sock);
116 ofpbuf_init(&request, 0);
117 nl_msg_put_genlmsghdr(&request, sock, 0, brc_family,
118 NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
119 retval = nl_sock_transact(sock, &request, &reply);
120 ofpbuf_uninit(&request);
122 nl_sock_destroy(sock);
125 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
126 brc_multicast_policy, attrs,
127 ARRAY_SIZE(brc_multicast_policy))) {
128 nl_sock_destroy(sock);
129 ofpbuf_delete(reply);
132 *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
133 nl_sock_destroy(sock);
134 ofpbuf_delete(reply);
139 /* Opens a socket for brcompat notifications. Returns 0 if successful,
140 * otherwise a positive errno value. */
142 brc_open(struct nl_sock **sock)
144 int multicast_group = 0;
147 retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
152 retval = lookup_brc_multicast_group(&multicast_group);
157 retval = nl_sock_create(NETLINK_GENERIC, multicast_group, 0, 0, sock);
165 static const struct nl_policy brc_dp_policy[] = {
166 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
169 static struct ovsrec_bridge *
170 find_bridge(const struct ovsrec_open_vswitch *ovs, const char *br_name)
174 for (i = 0; i < ovs->n_bridges; i++) {
175 if (!strcmp(br_name, ovs->bridges[i]->name)) {
176 return ovs->bridges[i];
184 execute_appctl_command(const char *unixctl_command, char **output)
186 char *stdout_log, *stderr_log;
192 argv[2] = xasprintf(appctl_command, unixctl_command);
195 /* Run process and log status. */
196 error = process_run_capture(argv, &stdout_log, &stderr_log, &status);
198 VLOG_ERR("failed to execute %s command via ovs-appctl: %s",
199 unixctl_command, strerror(error));
201 char *msg = process_status_msg(status);
202 VLOG_ERR("ovs-appctl exited with error (%s)", msg);
207 /* Deal with stdout_log. */
209 *output = stdout_log;
214 /* Deal with stderr_log */
215 if (stderr_log && *stderr_log) {
216 VLOG_INFO("ovs-appctl wrote to stderr:\n%s", stderr_log);
226 do_get_bridge_parts(const struct ovsrec_bridge *br, struct svec *parts,
227 int vlan, bool break_down_bonds)
233 for (i = 0; i < br->n_ports; i++) {
234 const struct ovsrec_port *port = br->ports[i];
236 svec_add(&ports, port->name);
238 int port_vlan = port->n_tag ? *port->tag : 0;
239 if (vlan != port_vlan) {
243 if (break_down_bonds) {
244 for (j = 0; j < port->n_interfaces; j++) {
245 const struct ovsrec_interface *iface = port->interfaces[j];
246 svec_add(parts, iface->name);
249 svec_add(parts, port->name);
252 svec_destroy(&ports);
255 /* Add all the interfaces for 'bridge' to 'ifaces', breaking bonded interfaces
256 * down into their constituent parts.
258 * If 'vlan' < 0, all interfaces on 'bridge' are reported. If 'vlan' == 0,
259 * then only interfaces for trunk ports or ports with implicit VLAN 0 are
260 * reported. If 'vlan' > 0, only interfaces with implicit VLAN 'vlan' are
263 get_bridge_ifaces(const struct ovsrec_bridge *br, struct svec *ifaces,
266 do_get_bridge_parts(br, ifaces, vlan, true);
269 /* Add all the ports for 'bridge' to 'ports'. Bonded ports are reported under
270 * the bond name, not broken down into their constituent interfaces.
272 * If 'vlan' < 0, all ports on 'bridge' are reported. If 'vlan' == 0, then
273 * only trunk ports or ports with implicit VLAN 0 are reported. If 'vlan' > 0,
274 * only port with implicit VLAN 'vlan' are reported. */
276 get_bridge_ports(const struct ovsrec_bridge *br, struct svec *ports,
279 do_get_bridge_parts(br, ports, vlan, false);
283 /* Go through the configuration file and remove any ports that no longer
284 * exist associated with a bridge. */
289 struct svec bridges, delete;
291 if (cfg_lock(NULL, 0)) {
292 /* Couldn't lock config file. */
298 cfg_get_subsections(&bridges, "bridge");
299 for (i=0; i<bridges.n; i++) {
300 const char *br_name = bridges.names[i];
303 /* Check that each bridge interface exists. */
305 get_bridge_ifaces(br_name, &ifaces, -1);
306 for (j = 0; j < ifaces.n; j++) {
307 const char *iface_name = ifaces.names[j];
309 /* The local port and internal ports are created and destroyed by
310 * ovs-vswitchd itself, so don't bother checking for them at all.
311 * In practice, they might not exist if ovs-vswitchd hasn't
312 * finished reloading since the configuration file was updated. */
313 if (!strcmp(iface_name, br_name)
314 || cfg_get_bool(0, "iface.%s.internal", iface_name)) {
318 if (!netdev_exists(iface_name)) {
319 VLOG_INFO_RL(&rl, "removing dead interface %s from %s",
320 iface_name, br_name);
321 svec_add(&delete, iface_name);
324 svec_destroy(&ifaces);
326 svec_destroy(&bridges);
331 for (i = 0; i < delete.n; i++) {
332 cfg_del_match("bridge.*.port=%s", delete.names[i]);
333 cfg_del_match("bonding.*.slave=%s", delete.names[i]);
340 svec_destroy(&delete);
344 static struct ovsdb_idl_txn *
345 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
347 return ovsdb_idl_txn_get(&ovs->header_);
351 port_is_fake_bridge(const struct ovsrec_port *port)
353 return (port->fake_bridge
355 && *port->tag >= 1 && *port->tag <= 4095);
359 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
360 struct ovsrec_bridge *bridge)
362 struct ovsrec_bridge **bridges;
365 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
366 for (i = 0; i < ovs->n_bridges; i++) {
367 bridges[i] = ovs->bridges[i];
369 bridges[ovs->n_bridges] = bridge;
370 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
375 add_bridge(const struct ovsrec_open_vswitch *ovs, const char *br_name)
377 struct ovsrec_bridge *br;
378 struct ovsrec_port *port;
379 struct ovsrec_interface *iface;
381 if (find_bridge(ovs, br_name)) {
382 VLOG_WARN("addbr %s: bridge %s exists", br_name, br_name);
384 } else if (netdev_exists(br_name)) {
387 for (i = 0; i < ovs->n_bridges; i++) {
389 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
391 for (j = 0; j < br_cfg->n_ports; j++) {
392 if (port_is_fake_bridge(br_cfg->ports[j])) {
393 VLOG_WARN("addbr %s: %s exists as a fake bridge",
400 VLOG_WARN("addbr %s: cannot create bridge %s because a network "
401 "device named %s already exists",
402 br_name, br_name, br_name);
406 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
407 ovsrec_interface_set_name(iface, br_name);
409 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
410 ovsrec_port_set_name(port, br_name);
411 ovsrec_port_set_interfaces(port, &iface, 1);
413 br = ovsrec_bridge_insert(txn_from_openvswitch(ovs));
414 ovsrec_bridge_set_name(br, br_name);
415 ovsrec_bridge_set_ports(br, &port, 1);
417 ovs_insert_bridge(ovs, br);
419 VLOG_INFO("addbr %s: success", br_name);
425 add_port(const struct ovsrec_open_vswitch *ovs,
426 const struct ovsrec_bridge *br, const char *port_name)
428 struct ovsrec_interface *iface;
429 struct ovsrec_port *port;
430 struct ovsrec_port **ports;
433 /* xxx Check conflicts? */
434 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
435 ovsrec_interface_set_name(iface, port_name);
437 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
438 ovsrec_port_set_name(port, port_name);
439 ovsrec_port_set_interfaces(port, &iface, 1);
441 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
442 for (i = 0; i < br->n_ports; i++) {
443 ports[i] = br->ports[i];
445 ports[br->n_ports] = port;
446 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
451 del_port(const struct ovsrec_bridge *br, const char *port_name)
454 struct ovsrec_port *port_rec = NULL;
456 for (i = 0; i < br->n_ports; i++) {
457 struct ovsrec_port *port = br->ports[i];
458 if (!strcmp(port_name, port->name)) {
461 for (j = 0; j < port->n_interfaces; j++) {
462 struct ovsrec_interface *iface = port->interfaces[j];
463 if (!strcmp(port_name, iface->name)) {
464 ovsrec_interface_delete(iface);
469 /* xxx Probably can move this into the "for" loop. */
471 struct ovsrec_port **ports;
474 ports = xmalloc(sizeof *br->ports * br->n_ports);
475 for (i = n = 0; i < br->n_ports; i++) {
476 if (br->ports[i] != port_rec) {
477 ports[n++] = br->ports[i];
480 ovsrec_bridge_set_ports(br, ports, n);
486 del_bridge(const struct ovsrec_open_vswitch *ovs, const char *br_name)
488 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
489 struct ovsrec_bridge **bridges;
493 VLOG_WARN("delbr %s: no bridge named %s", br_name, br_name);
497 del_port(br, br_name);
499 ovsrec_bridge_delete(br);
501 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
502 for (i = n = 0; i < ovs->n_bridges; i++) {
503 if (ovs->bridges[i] != br) {
504 bridges[n++] = ovs->bridges[i];
507 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
510 VLOG_INFO("delbr %s: success", br_name);
516 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
517 const char **port_name, uint64_t *count, uint64_t *skip)
519 static const struct nl_policy policy[] = {
520 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING, .optional = true },
521 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
522 [BRC_GENL_A_FDB_COUNT] = { .type = NL_A_U64, .optional = true },
523 [BRC_GENL_A_FDB_SKIP] = { .type = NL_A_U64, .optional = true },
525 struct nlattr *attrs[ARRAY_SIZE(policy)];
527 if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
528 attrs, ARRAY_SIZE(policy))
529 || (br_name && !attrs[BRC_GENL_A_DP_NAME])
530 || (port_name && !attrs[BRC_GENL_A_PORT_NAME])
531 || (count && !attrs[BRC_GENL_A_FDB_COUNT])
532 || (skip && !attrs[BRC_GENL_A_FDB_SKIP])) {
536 *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
538 *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
541 *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
544 *count = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_COUNT]);
547 *skip = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_SKIP]);
552 /* Composes and returns a reply to a request made by the datapath with Netlink
553 * sequence number 'seq' and error code 'error'. The caller may add additional
554 * attributes to the message, then it may send it with send_reply(). */
555 static struct ofpbuf *
556 compose_reply(uint32_t seq, int error)
558 struct ofpbuf *reply = ofpbuf_new(4096);
559 nl_msg_put_genlmsghdr(reply, brc_sock, 32, brc_family, NLM_F_REQUEST,
560 BRC_GENL_C_DP_RESULT, 1);
561 ((struct nlmsghdr *) reply->data)->nlmsg_seq = seq;
562 nl_msg_put_u32(reply, BRC_GENL_A_ERR_CODE, error);
566 /* Sends 'reply' to the datapath and frees it. */
568 send_reply(struct ofpbuf *reply)
570 int retval = nl_sock_send(brc_sock, reply, false);
572 VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
575 ofpbuf_delete(reply);
578 /* Composes and sends a reply to a request made by the datapath with Netlink
579 * sequence number 'seq' and error code 'error'. */
581 send_simple_reply(uint32_t seq, int error)
583 send_reply(compose_reply(seq, error));
587 handle_bridge_cmd(const struct ovsrec_open_vswitch *ovs,
588 struct ofpbuf *buffer, bool add)
594 error = parse_command(buffer, &seq, &br_name, NULL, NULL, NULL);
596 error = add ? add_bridge(ovs, br_name) : del_bridge(ovs, br_name);
597 send_simple_reply(seq, error);
602 static const struct nl_policy brc_port_policy[] = {
603 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
604 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
608 handle_port_cmd(const struct ovsrec_open_vswitch *ovs,
609 struct ofpbuf *buffer, bool add)
611 const char *cmd_name = add ? "add-if" : "del-if";
612 const char *br_name, *port_name;
616 error = parse_command(buffer, &seq, &br_name, &port_name, NULL, NULL);
618 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
621 VLOG_WARN("%s %s %s: no bridge named %s",
622 cmd_name, br_name, port_name, br_name);
624 } else if (!netdev_exists(port_name)) {
625 VLOG_WARN("%s %s %s: no network device named %s",
626 cmd_name, br_name, port_name, port_name);
630 add_port(ovs, br, port_name);
632 del_port(br, port_name);
634 VLOG_INFO("%s %s %s: success", cmd_name, br_name, port_name);
636 send_simple_reply(seq, error);
642 /* The caller is responsible for freeing '*ovs_name' if the call is
645 linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch *ovs,
646 const char *linux_name,
647 const struct ovsrec_bridge **ovs_bridge,
650 *ovs_bridge = find_bridge(ovs, linux_name);
652 /* Bridge name is the same. We are interested in VLAN 0. */
656 /* No such Open vSwitch bridge 'linux_name', but there might be an
657 * internal port named 'linux_name' on some other bridge
658 * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
659 * port 'linux_name' on the bridge named 'ovs_bridge'. */
662 for (i = 0; i < ovs->n_bridges; i++) {
663 const struct ovsrec_bridge *br = ovs->bridges[i];
665 for (j = 0; j < br->n_ports; j++) {
666 const struct ovsrec_port *port = br->ports[j];
668 if (!strcmp(port->name, linux_name)) {
670 *br_vlan = port->n_tag ? *port->tag : -1;
681 handle_fdb_query_cmd(const struct ovsrec_open_vswitch *ovs,
682 struct ofpbuf *buffer)
684 /* This structure is copied directly from the Linux 2.6.30 header files.
685 * It would be more straightforward to #include <linux/if_bridge.h>, but
686 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
687 * with old header files won't have it. */
692 __u32 ageing_timer_value;
701 struct mac *local_macs;
705 /* Impedance matching between the vswitchd and Linux kernel notions of what
706 * a bridge is. The kernel only handles a single VLAN per bridge, but
707 * vswitchd can deal with all the VLANs on a single bridge. We have to
708 * pretend that the former is the case even though the latter is the
710 const char *linux_name; /* Name used by brctl. */
711 const struct ovsrec_bridge *ovs_bridge; /* Bridge used by ovs-vswitchd. */
712 int br_vlan; /* VLAN tag. */
715 struct ofpbuf query_data;
716 struct ofpbuf *reply;
717 char *unixctl_command;
718 uint64_t count, skip;
724 /* Parse the command received from brcompat_mod. */
725 error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
730 /* Figure out vswitchd bridge and VLAN. */
731 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
732 &ovs_bridge, &br_vlan);
734 send_simple_reply(seq, error);
738 /* Fetch the forwarding database using ovs-appctl. */
739 unixctl_command = xasprintf("fdb/show %s", ovs_bridge->name);
740 error = execute_appctl_command(unixctl_command, &output);
741 free(unixctl_command);
743 send_simple_reply(seq, error);
747 /* Fetch the MAC address for each interface on the bridge, so that we can
748 * fill in the is_local field in the response. */
750 get_bridge_ifaces(ovs_bridge, &ifaces, br_vlan);
751 local_macs = xmalloc(ifaces.n * sizeof *local_macs);
753 for (i = 0; i < ifaces.n; i++) {
754 const char *iface_name = ifaces.names[i];
755 struct mac *mac = &local_macs[n_local_macs];
756 struct netdev *netdev;
758 error = netdev_open(iface_name, NETDEV_ETH_TYPE_NONE, &netdev);
760 if (!netdev_get_etheraddr(netdev, mac->addr)) {
763 netdev_close(netdev);
766 svec_destroy(&ifaces);
768 /* Parse the response from ovs-appctl and convert it to binary format to
769 * pass back to the kernel. */
770 ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
772 strtok_r(output, "\n", &save_ptr); /* Skip header line. */
774 struct __fdb_entry *entry;
776 uint8_t mac[ETH_ADDR_LEN];
780 line = strtok_r(NULL, "\n", &save_ptr);
785 if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
786 &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
787 != 2 + ETH_ADDR_SCAN_COUNT + 1) {
788 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
789 VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
793 if (vlan != br_vlan) {
802 /* Is this the MAC address of an interface on the bridge? */
804 for (i = 0; i < n_local_macs; i++) {
805 if (eth_addr_equals(local_macs[i].addr, mac)) {
811 entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
812 memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
813 entry->port_no = port & 0xff;
814 entry->is_local = is_local;
815 entry->ageing_timer_value = age * HZ;
816 entry->port_hi = (port & 0xff00) >> 8;
823 /* Compose and send reply to datapath. */
824 reply = compose_reply(seq, 0);
825 nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
826 query_data.data, query_data.size);
830 ofpbuf_uninit(&query_data);
836 send_ifindex_reply(uint32_t seq, struct svec *ifaces)
838 struct ofpbuf *reply;
844 /* Make sure that any given interface only occurs once. This shouldn't
845 * happen, but who knows what people put into their configuration files. */
846 svec_sort_unique(ifaces);
848 /* Convert 'ifaces' into ifindexes. */
850 indices = xmalloc(ifaces->n * sizeof *indices);
851 SVEC_FOR_EACH (i, iface, ifaces) {
852 int ifindex = if_nametoindex(iface);
854 indices[n_indices++] = ifindex;
858 /* Compose and send reply. */
859 reply = compose_reply(seq, 0);
860 nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
861 indices, n_indices * sizeof *indices);
869 handle_get_bridges_cmd(const struct ovsrec_open_vswitch *ovs,
870 struct ofpbuf *buffer)
879 /* Parse Netlink command.
881 * The command doesn't actually have any arguments, but we need the
882 * sequence number to send the reply. */
883 error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
888 /* Get all the real bridges and all the fake ones. */
890 for (i = 0; i < ovs->n_bridges; i++) {
891 const struct ovsrec_bridge *br = ovs->bridges[i];
893 svec_add(&bridges, br->name);
894 for (j = 0; j < br->n_ports; j++) {
895 const struct ovsrec_port *port = br->ports[j];
897 if (port->fake_bridge) {
898 svec_add(&bridges, port->name);
903 send_ifindex_reply(seq, &bridges);
904 svec_destroy(&bridges);
910 handle_get_ports_cmd(const struct ovsrec_open_vswitch *ovs,
911 struct ofpbuf *buffer)
915 const char *linux_name;
916 const struct ovsrec_bridge *ovs_bridge;
923 /* Parse Netlink command. */
924 error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
929 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
930 &ovs_bridge, &br_vlan);
932 send_simple_reply(seq, error);
937 get_bridge_ports(ovs_bridge, &ports, br_vlan);
939 svec_del(&ports, linux_name);
940 send_ifindex_reply(seq, &ports); /* XXX bonds won't show up */
941 svec_destroy(&ports);
947 brc_recv_update(const struct ovsrec_open_vswitch *ovs)
950 struct ofpbuf *buffer;
951 struct genlmsghdr *genlmsghdr;
956 ofpbuf_delete(buffer);
957 retval = nl_sock_recv(brc_sock, &buffer, false);
958 } while (retval == ENOBUFS
960 && (nl_msg_nlmsgerr(buffer, NULL)
961 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE)));
963 if (retval != EAGAIN) {
964 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
969 genlmsghdr = nl_msg_genlmsghdr(buffer);
971 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
975 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
976 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
977 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
981 switch (genlmsghdr->cmd) {
982 case BRC_GENL_C_DP_ADD:
983 handle_bridge_cmd(ovs, buffer, true);
986 case BRC_GENL_C_DP_DEL:
987 handle_bridge_cmd(ovs, buffer, false);
990 case BRC_GENL_C_PORT_ADD:
991 handle_port_cmd(ovs, buffer, true);
994 case BRC_GENL_C_PORT_DEL:
995 handle_port_cmd(ovs, buffer, false);
998 case BRC_GENL_C_FDB_QUERY:
999 handle_fdb_query_cmd(ovs, buffer);
1002 case BRC_GENL_C_GET_BRIDGES:
1003 handle_get_bridges_cmd(ovs, buffer);
1006 case BRC_GENL_C_GET_PORTS:
1007 handle_get_ports_cmd(ovs, buffer);
1011 VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
1017 ofpbuf_delete(buffer);
1022 /* Check for interface configuration changes announced through RTNL. */
1024 rtnl_recv_update(void)
1028 int error = nl_sock_recv(rtnl_sock, &buf, false);
1029 if (error == EAGAIN) {
1030 /* Nothing to do. */
1031 } else if (error == ENOBUFS) {
1032 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
1034 VLOG_WARN_RL(&rl, "error on network monitor socket: %s",
1037 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1038 struct nlmsghdr *nlh;
1039 struct ifinfomsg *iim;
1041 nlh = ofpbuf_at(buf, 0, NLMSG_HDRLEN);
1042 iim = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *iim);
1044 VLOG_WARN_RL(&rl, "received bad rtnl message (no ifinfomsg)");
1049 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1050 rtnlgrp_link_policy,
1051 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1052 VLOG_WARN_RL(&rl,"received bad rtnl message (policy)");
1056 if (nlh->nlmsg_type == RTM_DELLINK && attrs[IFLA_MASTER]) {
1057 const char *port_name = nl_attr_get_string(attrs[IFLA_IFNAME]);
1058 char br_name[IFNAMSIZ];
1059 uint32_t br_idx = nl_attr_get_u32(attrs[IFLA_MASTER]);
1061 if (!if_indextoname(br_idx, br_name)) {
1066 if (cfg_lock(NULL, lock_timeout)) {
1067 /* Couldn't lock config file. */
1068 /* xxx this should try again and print error msg. */
1073 if (!netdev_exists(port_name)) {
1074 /* Network device is really gone. */
1077 VLOG_INFO("network device %s destroyed, "
1078 "removing from bridge %s", port_name, br_name);
1081 cfg_get_all_keys(&ports, "bridge.%s.port", br_name);
1083 if (svec_contains(&ports, port_name)) {
1084 del_port(br_name, port_name);
1086 svec_destroy(&ports);
1088 /* A network device by that name exists even though the kernel
1089 * told us it had disappeared. Probably, what happened was
1092 * 1. Device destroyed.
1093 * 2. Notification sent to us.
1094 * 3. New device created with same name as old one.
1095 * 4. ovs-brcompatd notified, removes device from bridge.
1097 * There's no a priori reason that in this situation that the
1098 * new device with the same name should remain in the bridge;
1099 * on the contrary, that would be unexpected. *But* there is
1100 * one important situation where, if we do this, bad things
1101 * happen. This is the case of XenServer Tools version 5.0.0,
1102 * which on boot of a Windows VM cause something like this to
1103 * happen on the Xen host:
1105 * i. Create tap1.0 and vif1.0.
1106 * ii. Delete tap1.0.
1107 * iii. Delete vif1.0.
1108 * iv. Re-create vif1.0.
1110 * (XenServer Tools 5.5.0 does not exhibit this behavior, and
1111 * neither does a VM without Tools installed at all.@.)
1113 * Steps iii and iv happen within a few seconds of each other.
1114 * Step iv causes /etc/xensource/scripts/vif to run, which in
1115 * turn calls ovs-cfg-mod to add the new device to the bridge.
1116 * If step iv happens after step 4 (in our first list of
1117 * steps), then all is well, but if it happens between 3 and 4
1118 * (which can easily happen if ovs-brcompatd has to wait to
1119 * lock the configuration file), then we will remove the new
1120 * incarnation from the bridge instead of the old one!
1122 * So, to avoid this problem, we do nothing here. This is
1123 * strictly incorrect except for this one particular case, and
1124 * perhaps that will bite us someday. If that happens, then we
1125 * will have to somehow track network devices by ifindex, since
1126 * a new device will have a new ifindex even if it has the same
1127 * name as an old device.
1129 VLOG_INFO("kernel reported network device %s removed but "
1130 "a device by that name exists (XS Tools 5.0.0?)",
1141 main(int argc, char *argv[])
1143 struct unixctl_server *unixctl;
1145 struct ovsdb_idl *idl;
1148 set_program_name(argv[0]);
1151 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
1152 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
1154 remote = parse_options(argc, argv);
1155 signal(SIGPIPE, SIG_IGN);
1158 die_if_already_running();
1161 retval = unixctl_server_create(NULL, &unixctl);
1163 ovs_fatal(retval, "could not listen for vlog connections");
1166 if (brc_open(&brc_sock)) {
1167 ovs_fatal(0, "could not open brcompat socket. Check "
1168 "\"brcompat\" kernel module.");
1171 if (prune_timeout) {
1172 if (nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &rtnl_sock)) {
1173 ovs_fatal(0, "could not create rtnetlink socket");
1177 daemonize_complete();
1179 idl = ovsdb_idl_create(remote, &ovsrec_idl_class);
1182 const struct ovsrec_open_vswitch *ovs;
1183 struct ovsdb_idl_txn *txn;
1184 enum ovsdb_idl_txn_status status;
1188 txn = ovsdb_idl_txn_create(idl);
1190 unixctl_server_run(unixctl);
1191 ovs = ovsrec_open_vswitch_first(idl);
1193 brc_recv_update(ovs);
1194 } else if (ovsdb_idl_get_seqno(idl)) {
1195 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1196 VLOG_WARN_RL(&rl, "%s: database does not contain any Open vSwitch "
1197 "configuration", remote);
1199 /* Haven't yet received initial database contents. */
1204 /* If 'prune_timeout' is non-zero, we actively prune from the
1205 * config file any 'bridge.<br_name>.port' entries that are no
1206 * longer valid. We use two methods:
1208 * 1) The kernel explicitly notifies us of removed ports
1209 * through the RTNL messages.
1211 * 2) We periodically check all ports associated with bridges
1212 * to see if they no longer exist.
1214 if (prune_timeout) {
1218 nl_sock_wait(rtnl_sock, POLLIN);
1219 poll_timer_wait(prune_timeout);
1223 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1225 ovsdb_idl_wait(idl);
1226 ovsdb_idl_txn_wait(txn);
1229 ovsdb_idl_txn_destroy(txn);
1232 case TXN_INCOMPLETE:
1236 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
1237 ovs_fatal(0, "transaction aborted");
1244 /* xxx Handle this better! */
1245 printf("xxx We need to try again!\n");
1249 /* xxx Is this what we want to do? */
1250 ovs_fatal(0, "transaction error");
1256 nl_sock_wait(brc_sock, POLLIN);
1257 ovsdb_idl_wait(idl);
1258 unixctl_server_wait(unixctl);
1263 ovsdb_idl_destroy(idl);
1269 validate_appctl_command(void)
1275 for (p = strchr(appctl_command, '%'); p; p = strchr(p + 2, '%')) {
1277 /* Nothing to do. */
1278 } else if (p[1] == 's') {
1281 ovs_fatal(0, "only '%%s' and '%%%%' allowed in --appctl-command");
1285 ovs_fatal(0, "'%%s' must appear exactly once in --appctl-command");
1290 parse_options(int argc, char *argv[])
1296 LEAK_CHECKER_OPTION_ENUMS
1298 static struct option long_options[] = {
1299 {"help", no_argument, 0, 'h'},
1300 {"version", no_argument, 0, 'V'},
1301 {"prune-timeout", required_argument, 0, OPT_PRUNE_TIMEOUT},
1302 {"appctl-command", required_argument, 0, OPT_APPCTL_COMMAND},
1303 DAEMON_LONG_OPTIONS,
1305 LEAK_CHECKER_LONG_OPTIONS,
1308 char *short_options = long_options_to_short_options(long_options);
1310 appctl_command = xasprintf("%s/ovs-appctl %%s", ovs_bindir);
1314 c = getopt_long(argc, argv, short_options, long_options, NULL);
1325 OVS_PRINT_VERSION(0, 0);
1328 case OPT_PRUNE_TIMEOUT:
1329 prune_timeout = atoi(optarg) * 1000;
1332 case OPT_APPCTL_COMMAND:
1333 appctl_command = optarg;
1336 VLOG_OPTION_HANDLERS
1337 DAEMON_OPTION_HANDLERS
1338 LEAK_CHECKER_OPTION_HANDLERS
1347 free(short_options);
1349 validate_appctl_command();
1355 ovs_fatal(0, "database socket is non-option argument; "
1356 "use --help for usage");
1365 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
1366 "usage: %s [OPTIONS] CONFIG\n"
1367 "CONFIG is the configuration file used by ovs-vswitchd.\n",
1368 program_name, program_name);
1369 printf("\nConfiguration options:\n"
1370 " --appctl-command=COMMAND shell command to run ovs-appctl\n"
1371 " --prune-timeout=SECS wait at most SECS before pruning ports\n"
1375 printf("\nOther options:\n"
1376 " -h, --help display this help message\n"
1377 " -V, --version display version information\n");
1378 leak_checker_usage();
1379 printf("\nThe default appctl command is:\n%s\n", appctl_command);