1 /* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <asm/param.h>
25 #include <linux/genetlink.h>
26 #include <linux/rtnetlink.h>
30 #include <sys/types.h>
36 #include "command-line.h"
40 #include "dynamic-string.h"
41 #include "fatal-signal.h"
43 #include "leak-checker.h"
46 #include "netlink-socket.h"
48 #include "openvswitch/brcompat-netlink.h"
49 #include "ovsdb-idl.h"
51 #include "poll-loop.h"
53 #include "rtnetlink.h"
54 #include "rtnetlink-link.h"
61 #include "vswitchd/vswitch-idl.h"
63 VLOG_DEFINE_THIS_MODULE(brcompatd);
66 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
68 /* Actions to modify bridge compatibility configuration. */
76 static const char *parse_options(int argc, char *argv[]);
77 static void usage(void) NO_RETURN;
79 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
81 /* 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 bridge compatibility kernel module. */
87 static struct nl_sock *brc_sock;
89 /* The Generic Netlink family number used for bridge compatibility. */
90 static int brc_family;
92 static const struct nl_policy brc_multicast_policy[] = {
93 [BRC_GENL_A_MC_GROUP] = {.type = NL_A_U32 }
97 lookup_brc_multicast_group(int *multicast_group)
100 struct ofpbuf request, *reply;
101 struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
104 retval = nl_sock_create(NETLINK_GENERIC, &sock);
108 ofpbuf_init(&request, 0);
109 nl_msg_put_genlmsghdr(&request, 0, brc_family,
110 NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
111 retval = nl_sock_transact(sock, &request, &reply);
112 ofpbuf_uninit(&request);
114 nl_sock_destroy(sock);
117 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
118 brc_multicast_policy, attrs,
119 ARRAY_SIZE(brc_multicast_policy))) {
120 nl_sock_destroy(sock);
121 ofpbuf_delete(reply);
124 *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
125 nl_sock_destroy(sock);
126 ofpbuf_delete(reply);
131 /* Opens a socket for brcompat notifications. Returns 0 if successful,
132 * otherwise a positive errno value. */
134 brc_open(struct nl_sock **sock)
136 int multicast_group = 0;
139 retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
144 retval = lookup_brc_multicast_group(&multicast_group);
149 retval = nl_sock_create(NETLINK_GENERIC, sock);
154 retval = nl_sock_join_mcgroup(*sock, multicast_group);
156 nl_sock_destroy(*sock);
162 static const struct nl_policy brc_dp_policy[] = {
163 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
166 static struct ovsrec_bridge *
167 find_bridge(const struct ovsrec_open_vswitch *ovs, const char *br_name)
171 for (i = 0; i < ovs->n_bridges; i++) {
172 if (!strcmp(br_name, ovs->bridges[i]->name)) {
173 return ovs->bridges[i];
181 execute_appctl_command(const char *unixctl_command, char **output)
183 char *stdout_log, *stderr_log;
189 argv[2] = xasprintf(appctl_command, unixctl_command);
192 /* Run process and log status. */
193 error = process_run_capture(argv, &stdout_log, &stderr_log, 65536,
196 VLOG_ERR("failed to execute %s command via ovs-appctl: %s",
197 unixctl_command, strerror(error));
199 char *msg = process_status_msg(status);
200 VLOG_ERR("ovs-appctl exited with error (%s)", msg);
205 /* Deal with stdout_log. */
207 *output = stdout_log;
212 /* Deal with stderr_log */
213 if (stderr_log && *stderr_log) {
214 VLOG_INFO("ovs-appctl wrote to stderr:\n%s", stderr_log);
224 do_get_bridge_parts(const struct ovsrec_bridge *br, struct sset *parts,
225 int vlan, bool break_down_bonds)
229 for (i = 0; i < br->n_ports; i++) {
230 const struct ovsrec_port *port = br->ports[i];
233 int port_vlan = port->n_tag ? *port->tag : 0;
234 if (vlan != port_vlan) {
238 if (break_down_bonds) {
239 for (j = 0; j < port->n_interfaces; j++) {
240 const struct ovsrec_interface *iface = port->interfaces[j];
241 sset_add(parts, iface->name);
244 sset_add(parts, port->name);
249 /* Add all the interfaces for 'bridge' to 'ifaces', breaking bonded interfaces
250 * down into their constituent parts.
252 * If 'vlan' < 0, all interfaces on 'bridge' are reported. If 'vlan' == 0,
253 * then only interfaces for trunk ports or ports with implicit VLAN 0 are
254 * reported. If 'vlan' > 0, only interfaces with implicit VLAN 'vlan' are
257 get_bridge_ifaces(const struct ovsrec_bridge *br, struct sset *ifaces,
260 do_get_bridge_parts(br, ifaces, vlan, true);
263 /* Add all the ports for 'bridge' to 'ports'. Bonded ports are reported under
264 * the bond name, not broken down into their constituent interfaces.
266 * If 'vlan' < 0, all ports on 'bridge' are reported. If 'vlan' == 0, then
267 * only trunk ports or ports with implicit VLAN 0 are reported. If 'vlan' > 0,
268 * only port with implicit VLAN 'vlan' are reported. */
270 get_bridge_ports(const struct ovsrec_bridge *br, struct sset *ports,
273 do_get_bridge_parts(br, ports, vlan, false);
276 static struct ovsdb_idl_txn *
277 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
279 return ovsdb_idl_txn_get(&ovs->header_);
283 port_is_fake_bridge(const struct ovsrec_port *port)
285 return (port->fake_bridge
287 && *port->tag >= 1 && *port->tag <= 4095);
291 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
292 struct ovsrec_bridge *bridge)
294 struct ovsrec_bridge **bridges;
297 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
298 for (i = 0; i < ovs->n_bridges; i++) {
299 bridges[i] = ovs->bridges[i];
301 bridges[ovs->n_bridges] = bridge;
302 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
307 where_uuid_equals(const struct uuid *uuid)
312 json_string_create("_uuid"),
313 json_string_create("=="),
315 json_string_create("uuid"),
316 json_string_create_nocopy(
317 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
320 /* Commits 'txn'. If 'wait_for_reload' is true, also waits for Open vSwitch to
321 reload the configuration before returning.
323 Returns EAGAIN if the caller should try the operation again, 0 on success,
324 otherwise a positive errno value. */
326 commit_txn(struct ovsdb_idl_txn *txn, bool wait_for_reload)
328 struct ovsdb_idl *idl = ovsdb_idl_txn_get_idl (txn);
329 enum ovsdb_idl_txn_status status;
330 int64_t next_cfg = 0;
332 if (wait_for_reload) {
333 const struct ovsrec_open_vswitch *ovs = ovsrec_open_vswitch_first(idl);
334 struct json *where = where_uuid_equals(&ovs->header_.uuid);
335 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
338 status = ovsdb_idl_txn_commit_block(txn);
339 if (wait_for_reload && status == TXN_SUCCESS) {
340 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
342 ovsdb_idl_txn_destroy(txn);
349 VLOG_ERR_RL(&rl, "OVSDB transaction unexpectedly aborted");
356 if (wait_for_reload) {
358 /* We can't use 'ovs' any longer because ovsdb_idl_run() can
360 const struct ovsrec_open_vswitch *ovs2;
363 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs2, idl) {
364 if (ovs2->cur_cfg >= next_cfg) {
376 VLOG_ERR_RL(&rl, "OVSDB transaction needs retry");
380 VLOG_ERR_RL(&rl, "OVSDB transaction failed: %s",
381 ovsdb_idl_txn_get_error(txn));
390 add_bridge(struct ovsdb_idl *idl, const struct ovsrec_open_vswitch *ovs,
393 struct ovsrec_bridge *br;
394 struct ovsrec_port *port;
395 struct ovsrec_interface *iface;
396 struct ovsdb_idl_txn *txn;
398 if (find_bridge(ovs, br_name)) {
399 VLOG_WARN("addbr %s: bridge %s exists", br_name, br_name);
401 } else if (netdev_exists(br_name)) {
404 for (i = 0; i < ovs->n_bridges; i++) {
406 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
408 for (j = 0; j < br_cfg->n_ports; j++) {
409 if (port_is_fake_bridge(br_cfg->ports[j])) {
410 VLOG_WARN("addbr %s: %s exists as a fake bridge",
417 VLOG_WARN("addbr %s: cannot create bridge %s because a network "
418 "device named %s already exists",
419 br_name, br_name, br_name);
423 txn = ovsdb_idl_txn_create(idl);
425 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: addbr %s", br_name);
427 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
428 ovsrec_interface_set_name(iface, br_name);
430 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
431 ovsrec_port_set_name(port, br_name);
432 ovsrec_port_set_interfaces(port, &iface, 1);
434 br = ovsrec_bridge_insert(txn_from_openvswitch(ovs));
435 ovsrec_bridge_set_name(br, br_name);
436 ovsrec_bridge_set_ports(br, &port, 1);
438 ovs_insert_bridge(ovs, br);
440 return commit_txn(txn, true);
444 add_port(const struct ovsrec_open_vswitch *ovs,
445 const struct ovsrec_bridge *br, const char *port_name)
447 struct ovsrec_interface *iface;
448 struct ovsrec_port *port;
449 struct ovsrec_port **ports;
452 /* xxx Check conflicts? */
453 iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
454 ovsrec_interface_set_name(iface, port_name);
456 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
457 ovsrec_port_set_name(port, port_name);
458 ovsrec_port_set_interfaces(port, &iface, 1);
460 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
461 for (i = 0; i < br->n_ports; i++) {
462 ports[i] = br->ports[i];
464 ports[br->n_ports] = port;
465 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
469 /* Deletes 'port' from 'br'.
471 * After calling this function, 'port' must not be referenced again. */
473 del_port(const struct ovsrec_bridge *br, const struct ovsrec_port *port)
475 struct ovsrec_port **ports;
478 /* Remove 'port' from the bridge's list of ports. */
479 ports = xmalloc(sizeof *br->ports * br->n_ports);
480 for (i = n = 0; i < br->n_ports; i++) {
481 if (br->ports[i] != port) {
482 ports[n++] = br->ports[i];
485 ovsrec_bridge_set_ports(br, ports, n);
489 /* Delete 'iface' from 'port' (which must be within 'br'). If 'iface' was
490 * 'port''s only interface, delete 'port' from 'br' also.
492 * After calling this function, 'iface' must not be referenced again. */
494 del_interface(const struct ovsrec_bridge *br,
495 const struct ovsrec_port *port,
496 const struct ovsrec_interface *iface)
498 if (port->n_interfaces == 1) {
501 struct ovsrec_interface **ifaces;
504 ifaces = xmalloc(sizeof *port->interfaces * port->n_interfaces);
505 for (i = n = 0; i < port->n_interfaces; i++) {
506 if (port->interfaces[i] != iface) {
507 ifaces[n++] = port->interfaces[i];
510 ovsrec_port_set_interfaces(port, ifaces, n);
515 /* Find and return a port within 'br' named 'port_name'. */
516 static const struct ovsrec_port *
517 find_port(const struct ovsrec_bridge *br, const char *port_name)
521 for (i = 0; i < br->n_ports; i++) {
522 struct ovsrec_port *port = br->ports[i];
523 if (!strcmp(port_name, port->name)) {
530 /* Find and return an interface within 'br' named 'iface_name'. */
531 static const struct ovsrec_interface *
532 find_interface(const struct ovsrec_bridge *br, const char *iface_name,
533 struct ovsrec_port **portp)
537 for (i = 0; i < br->n_ports; i++) {
538 struct ovsrec_port *port = br->ports[i];
541 for (j = 0; j < port->n_interfaces; j++) {
542 struct ovsrec_interface *iface = port->interfaces[j];
543 if (!strcmp(iface->name, iface_name)) {
555 del_bridge(struct ovsdb_idl *idl,
556 const struct ovsrec_open_vswitch *ovs, const char *br_name)
558 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
559 struct ovsrec_bridge **bridges;
560 struct ovsdb_idl_txn *txn;
564 VLOG_WARN("delbr %s: no bridge named %s", br_name, br_name);
568 txn = ovsdb_idl_txn_create(idl);
570 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: delbr %s", br_name);
572 /* Remove 'br' from the vswitch's list of bridges. */
573 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
574 for (i = n = 0; i < ovs->n_bridges; i++) {
575 if (ovs->bridges[i] != br) {
576 bridges[n++] = ovs->bridges[i];
579 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
582 return commit_txn(txn, true);
586 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
587 const char **port_name, uint64_t *count, uint64_t *skip)
589 static const struct nl_policy policy[] = {
590 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING, .optional = true },
591 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
592 [BRC_GENL_A_FDB_COUNT] = { .type = NL_A_U64, .optional = true },
593 [BRC_GENL_A_FDB_SKIP] = { .type = NL_A_U64, .optional = true },
595 struct nlattr *attrs[ARRAY_SIZE(policy)];
597 if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
598 attrs, ARRAY_SIZE(policy))
599 || (br_name && !attrs[BRC_GENL_A_DP_NAME])
600 || (port_name && !attrs[BRC_GENL_A_PORT_NAME])
601 || (count && !attrs[BRC_GENL_A_FDB_COUNT])
602 || (skip && !attrs[BRC_GENL_A_FDB_SKIP])) {
606 *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
608 *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
611 *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
614 *count = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_COUNT]);
617 *skip = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_SKIP]);
622 /* Composes and returns a reply to a request made by the datapath with Netlink
623 * sequence number 'seq' and error code 'error'. The caller may add additional
624 * attributes to the message, then it may send it with send_reply(). */
625 static struct ofpbuf *
626 compose_reply(uint32_t seq, int error)
628 struct ofpbuf *reply = ofpbuf_new(4096);
629 nl_msg_put_genlmsghdr(reply, 32, brc_family, NLM_F_REQUEST,
630 BRC_GENL_C_DP_RESULT, 1);
631 ((struct nlmsghdr *) reply->data)->nlmsg_seq = seq;
632 nl_msg_put_u32(reply, BRC_GENL_A_ERR_CODE, error);
636 /* Sends 'reply' to the datapath and frees it. */
638 send_reply(struct ofpbuf *reply)
640 int retval = nl_sock_send(brc_sock, reply, false);
642 VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
645 ofpbuf_delete(reply);
648 /* Composes and sends a reply to a request made by the datapath with Netlink
649 * sequence number 'seq' and error code 'error'. */
651 send_simple_reply(uint32_t seq, int error)
653 send_reply(compose_reply(seq, error));
657 handle_bridge_cmd(struct ovsdb_idl *idl,
658 const struct ovsrec_open_vswitch *ovs,
659 struct ofpbuf *buffer, bool add)
665 error = parse_command(buffer, &seq, &br_name, NULL, NULL, NULL);
670 retval = (add ? add_bridge : del_bridge)(idl, ovs, br_name);
671 VLOG_INFO_RL(&rl, "%sbr %s: %s",
672 add ? "add" : "del", br_name, strerror(retval));
673 } while (retval == EAGAIN);
675 send_simple_reply(seq, error);
680 static const struct nl_policy brc_port_policy[] = {
681 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
682 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
686 handle_port_cmd(struct ovsdb_idl *idl,
687 const struct ovsrec_open_vswitch *ovs,
688 struct ofpbuf *buffer, bool add)
690 const char *cmd_name = add ? "add-if" : "del-if";
691 const char *br_name, *port_name;
695 error = parse_command(buffer, &seq, &br_name, &port_name, NULL, NULL);
697 struct ovsrec_bridge *br = find_bridge(ovs, br_name);
700 VLOG_WARN("%s %s %s: no bridge named %s",
701 cmd_name, br_name, port_name, br_name);
703 } else if (!netdev_exists(port_name)) {
704 VLOG_WARN("%s %s %s: no network device named %s",
705 cmd_name, br_name, port_name, port_name);
709 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
712 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: add-if %s",
714 add_port(ovs, br, port_name);
716 const struct ovsrec_port *port = find_port(br, port_name);
718 ovsdb_idl_txn_add_comment(txn,
719 "ovs-brcompatd: del-if %s",
725 error = commit_txn(txn, true);
726 VLOG_INFO_RL(&rl, "%s %s %s: %s",
727 cmd_name, br_name, port_name, strerror(error));
728 } while (error == EAGAIN);
730 send_simple_reply(seq, error);
736 /* The caller is responsible for freeing '*ovs_name' if the call is
739 linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch *ovs,
740 const char *linux_name,
741 const struct ovsrec_bridge **ovs_bridge,
744 *ovs_bridge = find_bridge(ovs, linux_name);
746 /* Bridge name is the same. We are interested in VLAN 0. */
750 /* No such Open vSwitch bridge 'linux_name', but there might be an
751 * internal port named 'linux_name' on some other bridge
752 * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
753 * port 'linux_name' on the bridge named 'ovs_bridge'. */
756 for (i = 0; i < ovs->n_bridges; i++) {
757 const struct ovsrec_bridge *br = ovs->bridges[i];
759 for (j = 0; j < br->n_ports; j++) {
760 const struct ovsrec_port *port = br->ports[j];
762 if (!strcmp(port->name, linux_name)) {
764 *br_vlan = port->n_tag ? *port->tag : -1;
775 handle_fdb_query_cmd(const struct ovsrec_open_vswitch *ovs,
776 struct ofpbuf *buffer)
778 /* This structure is copied directly from the Linux 2.6.30 header files.
779 * It would be more straightforward to #include <linux/if_bridge.h>, but
780 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
781 * with old header files won't have it. */
786 __u32 ageing_timer_value;
795 struct mac *local_macs;
799 /* Impedance matching between the vswitchd and Linux kernel notions of what
800 * a bridge is. The kernel only handles a single VLAN per bridge, but
801 * vswitchd can deal with all the VLANs on a single bridge. We have to
802 * pretend that the former is the case even though the latter is the
804 const char *linux_name; /* Name used by brctl. */
805 const struct ovsrec_bridge *ovs_bridge; /* Bridge used by ovs-vswitchd. */
806 int br_vlan; /* VLAN tag. */
809 struct ofpbuf query_data;
810 const char *iface_name;
811 struct ofpbuf *reply;
812 char *unixctl_command;
813 uint64_t count, skip;
819 /* Parse the command received from brcompat_mod. */
820 error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
825 /* Figure out vswitchd bridge and VLAN. */
826 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
827 &ovs_bridge, &br_vlan);
829 send_simple_reply(seq, error);
833 /* Fetch the forwarding database using ovs-appctl. */
834 unixctl_command = xasprintf("fdb/show %s", ovs_bridge->name);
835 error = execute_appctl_command(unixctl_command, &output);
836 free(unixctl_command);
838 send_simple_reply(seq, error);
842 /* Fetch the MAC address for each interface on the bridge, so that we can
843 * fill in the is_local field in the response. */
845 get_bridge_ifaces(ovs_bridge, &ifaces, br_vlan);
846 local_macs = xmalloc(sset_count(&ifaces) * sizeof *local_macs);
848 SSET_FOR_EACH (iface_name, &ifaces) {
849 struct mac *mac = &local_macs[n_local_macs];
850 struct netdev *netdev;
852 error = netdev_open_default(iface_name, &netdev);
854 if (!netdev_get_etheraddr(netdev, mac->addr)) {
857 netdev_close(netdev);
860 sset_destroy(&ifaces);
862 /* Parse the response from ovs-appctl and convert it to binary format to
863 * pass back to the kernel. */
864 ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
866 strtok_r(output, "\n", &save_ptr); /* Skip header line. */
868 struct __fdb_entry *entry;
870 uint8_t mac[ETH_ADDR_LEN];
874 line = strtok_r(NULL, "\n", &save_ptr);
879 if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
880 &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
881 != 2 + ETH_ADDR_SCAN_COUNT + 1) {
882 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
883 VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
887 if (vlan != br_vlan) {
896 /* Is this the MAC address of an interface on the bridge? */
898 for (i = 0; i < n_local_macs; i++) {
899 if (eth_addr_equals(local_macs[i].addr, mac)) {
905 entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
906 memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
907 entry->port_no = port & 0xff;
908 entry->is_local = is_local;
909 entry->ageing_timer_value = age * HZ;
910 entry->port_hi = (port & 0xff00) >> 8;
917 /* Compose and send reply to datapath. */
918 reply = compose_reply(seq, 0);
919 nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
920 query_data.data, query_data.size);
924 ofpbuf_uninit(&query_data);
931 send_ifindex_reply(uint32_t seq, struct sset *ifaces)
933 struct ofpbuf *reply;
938 /* Convert 'ifaces' into ifindexes. */
940 indices = xmalloc(sset_count(ifaces) * sizeof *indices);
941 SSET_FOR_EACH (iface, ifaces) {
942 int ifindex = if_nametoindex(iface);
944 indices[n_indices++] = ifindex;
948 /* Compose and send reply. */
949 reply = compose_reply(seq, 0);
950 nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
951 indices, n_indices * sizeof *indices);
959 handle_get_bridges_cmd(const struct ovsrec_open_vswitch *ovs,
960 struct ofpbuf *buffer)
969 /* Parse Netlink command.
971 * The command doesn't actually have any arguments, but we need the
972 * sequence number to send the reply. */
973 error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
978 /* Get all the real bridges and all the fake ones. */
980 for (i = 0; i < ovs->n_bridges; i++) {
981 const struct ovsrec_bridge *br = ovs->bridges[i];
983 sset_add(&bridges, br->name);
984 for (j = 0; j < br->n_ports; j++) {
985 const struct ovsrec_port *port = br->ports[j];
987 if (port->fake_bridge) {
988 sset_add(&bridges, port->name);
993 send_ifindex_reply(seq, &bridges);
994 sset_destroy(&bridges);
1000 handle_get_ports_cmd(const struct ovsrec_open_vswitch *ovs,
1001 struct ofpbuf *buffer)
1005 const char *linux_name;
1006 const struct ovsrec_bridge *ovs_bridge;
1013 /* Parse Netlink command. */
1014 error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
1019 error = linux_bridge_to_ovs_bridge(ovs, linux_name,
1020 &ovs_bridge, &br_vlan);
1022 send_simple_reply(seq, error);
1027 get_bridge_ports(ovs_bridge, &ports, br_vlan);
1028 sset_find_and_delete(&ports, linux_name);
1029 send_ifindex_reply(seq, &ports); /* XXX bonds won't show up */
1030 sset_destroy(&ports);
1035 static struct ofpbuf *
1036 brc_recv_update__(void)
1039 struct ofpbuf *buffer;
1042 retval = nl_sock_recv(brc_sock, &buffer, false);
1045 if (nl_msg_nlmsgerr(buffer, NULL)
1046 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE) {
1058 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
1061 ofpbuf_delete(buffer);
1066 brc_recv_update(struct ovsdb_idl *idl)
1068 struct ofpbuf *buffer;
1069 struct genlmsghdr *genlmsghdr;
1070 const struct ovsrec_open_vswitch *ovs;
1072 buffer = brc_recv_update__();
1077 genlmsghdr = nl_msg_genlmsghdr(buffer);
1079 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
1083 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
1084 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
1085 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
1089 /* Get the Open vSwitch configuration. Just drop the request on the floor
1090 * if a valid configuration doesn't exist. (We could check this earlier,
1091 * but we want to drain pending Netlink messages even when there is no Open
1092 * vSwitch configuration.) */
1093 ovs = ovsrec_open_vswitch_first(idl);
1095 VLOG_WARN_RL(&rl, "could not find valid configuration to update");
1099 /* Service all pending network device notifications before executing the
1100 * command. This is very important to avoid a race in a scenario like the
1101 * following, which is what happens with XenServer Tools version 5.0.0
1102 * during boot of a Windows VM:
1104 * 1. Create tap1.0 and vif1.0.
1107 * 4. Re-create vif1.0.
1109 * We must process the network device notification from step 3 before we
1110 * process the brctl command from step 4. If we process them in the
1111 * reverse order, then step 4 completes as a no-op but step 3 then deletes
1112 * the port that was just added.
1114 * (XenServer Tools 5.5.0 does not exhibit this behavior, and neither does
1115 * a VM without Tools installed at all.)
1117 rtnetlink_link_notifier_run();
1119 switch (genlmsghdr->cmd) {
1120 case BRC_GENL_C_DP_ADD:
1121 handle_bridge_cmd(idl, ovs, buffer, true);
1124 case BRC_GENL_C_DP_DEL:
1125 handle_bridge_cmd(idl, ovs, buffer, false);
1128 case BRC_GENL_C_PORT_ADD:
1129 handle_port_cmd(idl, ovs, buffer, true);
1132 case BRC_GENL_C_PORT_DEL:
1133 handle_port_cmd(idl, ovs, buffer, false);
1136 case BRC_GENL_C_FDB_QUERY:
1137 handle_fdb_query_cmd(ovs, buffer);
1140 case BRC_GENL_C_GET_BRIDGES:
1141 handle_get_bridges_cmd(ovs, buffer);
1144 case BRC_GENL_C_GET_PORTS:
1145 handle_get_ports_cmd(ovs, buffer);
1149 VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
1155 ofpbuf_delete(buffer);
1160 netdev_changed_cb(const struct rtnetlink_link_change *change, void *idl_)
1162 struct ovsdb_idl *idl = idl_;
1163 const struct ovsrec_open_vswitch *ovs;
1164 const struct ovsrec_interface *iface;
1165 struct ovsdb_idl_txn *txn;
1166 struct ovsrec_port *port;
1167 struct ovsrec_bridge *br;
1168 char br_name[IFNAMSIZ];
1169 const char *port_name;
1172 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
1176 if (change->nlmsg_type != RTM_DELLINK || !change->master_ifindex) {
1180 ovs = ovsrec_open_vswitch_first(idl);
1185 port_name = change->ifname;
1186 if (!if_indextoname(change->master_ifindex, br_name)) {
1190 VLOG_INFO("network device %s destroyed, removing from bridge %s",
1191 port_name, br_name);
1193 br = find_bridge(ovs, br_name);
1195 VLOG_WARN("no bridge named %s from which to remove %s",
1196 br_name, port_name);
1200 iface = find_interface(br, port_name, &port);
1205 txn = ovsdb_idl_txn_create(idl);
1206 del_interface(br, port, iface);
1207 ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: destroy port %s",
1209 commit_txn(txn, false);
1213 main(int argc, char *argv[])
1215 extern struct vlog_module VLM_reconnect;
1216 struct rtnetlink_notifier link_notifier;
1217 struct unixctl_server *unixctl;
1219 struct ovsdb_idl *idl;
1222 proctitle_init(argc, argv);
1223 set_program_name(argv[0]);
1224 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
1225 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
1227 remote = parse_options(argc, argv);
1228 signal(SIGPIPE, SIG_IGN);
1234 retval = unixctl_server_create(NULL, &unixctl);
1239 if (brc_open(&brc_sock)) {
1240 VLOG_FATAL("could not open brcompat socket. Check "
1241 "\"brcompat\" kernel module.");
1245 rtnetlink_link_notifier_register(&link_notifier, netdev_changed_cb, NULL);
1247 daemonize_complete();
1249 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
1252 const struct ovsrec_open_vswitch *ovs;
1256 unixctl_server_run(unixctl);
1257 rtnetlink_link_notifier_run();
1258 brc_recv_update(idl);
1260 ovs = ovsrec_open_vswitch_first(idl);
1261 if (!ovs && ovsdb_idl_has_ever_connected(idl)) {
1262 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1263 VLOG_WARN_RL(&rl, "%s: database does not contain any Open vSwitch "
1264 "configuration", remote);
1268 nl_sock_wait(brc_sock, POLLIN);
1269 ovsdb_idl_wait(idl);
1270 unixctl_server_wait(unixctl);
1271 rtnetlink_link_notifier_wait();
1276 rtnetlink_link_notifier_unregister(&link_notifier);
1277 ovsdb_idl_destroy(idl);
1283 validate_appctl_command(void)
1289 for (p = strchr(appctl_command, '%'); p; p = strchr(p + 2, '%')) {
1291 /* Nothing to do. */
1292 } else if (p[1] == 's') {
1295 VLOG_FATAL("only '%%s' and '%%%%' allowed in --appctl-command");
1299 VLOG_FATAL("'%%s' must appear exactly once in --appctl-command");
1304 parse_options(int argc, char *argv[])
1309 LEAK_CHECKER_OPTION_ENUMS,
1312 static struct option long_options[] = {
1313 {"help", no_argument, NULL, 'h'},
1314 {"version", no_argument, NULL, 'V'},
1315 {"appctl-command", required_argument, NULL, OPT_APPCTL_COMMAND},
1316 DAEMON_LONG_OPTIONS,
1318 LEAK_CHECKER_LONG_OPTIONS,
1321 char *short_options = long_options_to_short_options(long_options);
1323 appctl_command = xasprintf("%s/ovs-appctl %%s", ovs_bindir());
1327 c = getopt_long(argc, argv, short_options, long_options, NULL);
1338 OVS_PRINT_VERSION(0, 0);
1341 case OPT_APPCTL_COMMAND:
1342 appctl_command = optarg;
1345 VLOG_OPTION_HANDLERS
1346 DAEMON_OPTION_HANDLERS
1347 LEAK_CHECKER_OPTION_HANDLERS
1356 free(short_options);
1358 validate_appctl_command();
1364 VLOG_FATAL("database socket is non-option argument; "
1365 "use --help for usage");
1374 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
1375 "usage: %s [OPTIONS] CONFIG\n"
1376 "CONFIG is the configuration file used by ovs-vswitchd.\n",
1377 program_name, program_name);
1378 printf("\nConfiguration options:\n"
1379 " --appctl-command=COMMAND shell command to run ovs-appctl\n"
1383 printf("\nOther options:\n"
1384 " -h, --help display this help message\n"
1385 " -V, --version display version information\n");
1386 leak_checker_usage();
1387 printf("\nThe default appctl command is:\n%s\n", appctl_command);