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
, 0, 0, 0, &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
, multicast_group
, 0, 0, sock
);
167 static const struct nl_policy brc_dp_policy
[] = {
168 [BRC_GENL_A_DP_NAME
] = { .type
= NL_A_STRING
},
171 static struct ovsrec_bridge
*
172 find_bridge(const struct ovsrec_open_vswitch
*ovs
, const char *br_name
)
176 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
177 if (!strcmp(br_name
, ovs
->bridges
[i
]->name
)) {
178 return ovs
->bridges
[i
];
186 execute_appctl_command(const char *unixctl_command
, char **output
)
188 char *stdout_log
, *stderr_log
;
194 argv
[2] = xasprintf(appctl_command
, unixctl_command
);
197 /* Run process and log status. */
198 error
= process_run_capture(argv
, &stdout_log
, &stderr_log
, &status
);
200 VLOG_ERR("failed to execute %s command via ovs-appctl: %s",
201 unixctl_command
, strerror(error
));
203 char *msg
= process_status_msg(status
);
204 VLOG_ERR("ovs-appctl exited with error (%s)", msg
);
209 /* Deal with stdout_log. */
211 *output
= stdout_log
;
216 /* Deal with stderr_log */
217 if (stderr_log
&& *stderr_log
) {
218 VLOG_INFO("ovs-appctl wrote to stderr:\n%s", stderr_log
);
228 do_get_bridge_parts(const struct ovsrec_bridge
*br
, struct svec
*parts
,
229 int vlan
, bool break_down_bonds
)
235 for (i
= 0; i
< br
->n_ports
; i
++) {
236 const struct ovsrec_port
*port
= br
->ports
[i
];
238 svec_add(&ports
, port
->name
);
240 int port_vlan
= port
->n_tag
? *port
->tag
: 0;
241 if (vlan
!= port_vlan
) {
245 if (break_down_bonds
) {
246 for (j
= 0; j
< port
->n_interfaces
; j
++) {
247 const struct ovsrec_interface
*iface
= port
->interfaces
[j
];
248 svec_add(parts
, iface
->name
);
251 svec_add(parts
, port
->name
);
254 svec_destroy(&ports
);
257 /* Add all the interfaces for 'bridge' to 'ifaces', breaking bonded interfaces
258 * down into their constituent parts.
260 * If 'vlan' < 0, all interfaces on 'bridge' are reported. If 'vlan' == 0,
261 * then only interfaces for trunk ports or ports with implicit VLAN 0 are
262 * reported. If 'vlan' > 0, only interfaces with implicit VLAN 'vlan' are
265 get_bridge_ifaces(const struct ovsrec_bridge
*br
, struct svec
*ifaces
,
268 do_get_bridge_parts(br
, ifaces
, vlan
, true);
271 /* Add all the ports for 'bridge' to 'ports'. Bonded ports are reported under
272 * the bond name, not broken down into their constituent interfaces.
274 * If 'vlan' < 0, all ports on 'bridge' are reported. If 'vlan' == 0, then
275 * only trunk ports or ports with implicit VLAN 0 are reported. If 'vlan' > 0,
276 * only port with implicit VLAN 'vlan' are reported. */
278 get_bridge_ports(const struct ovsrec_bridge
*br
, struct svec
*ports
,
281 do_get_bridge_parts(br
, ports
, vlan
, false);
284 static struct ovsdb_idl_txn
*
285 txn_from_openvswitch(const struct ovsrec_open_vswitch
*ovs
)
287 return ovsdb_idl_txn_get(&ovs
->header_
);
291 port_is_fake_bridge(const struct ovsrec_port
*port
)
293 return (port
->fake_bridge
295 && *port
->tag
>= 1 && *port
->tag
<= 4095);
299 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
300 struct ovsrec_bridge
*bridge
)
302 struct ovsrec_bridge
**bridges
;
305 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
306 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
307 bridges
[i
] = ovs
->bridges
[i
];
309 bridges
[ovs
->n_bridges
] = bridge
;
310 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
315 where_uuid_equals(const struct uuid
*uuid
)
320 json_string_create("_uuid"),
321 json_string_create("=="),
323 json_string_create("uuid"),
324 json_string_create_nocopy(
325 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
328 /* Commits 'txn'. If 'wait_for_reload' is true, also waits for Open vSwitch to
329 reload the configuration before returning.
331 Returns EAGAIN if the caller should try the operation again, 0 on success,
332 otherwise a positive errno value. */
334 commit_txn(struct ovsdb_idl_txn
*txn
, bool wait_for_reload
)
336 struct ovsdb_idl
*idl
= ovsdb_idl_txn_get_idl (txn
);
337 enum ovsdb_idl_txn_status status
;
338 int64_t next_cfg
= 0;
340 if (wait_for_reload
) {
341 const struct ovsrec_open_vswitch
*ovs
= ovsrec_open_vswitch_first(idl
);
342 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
343 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
346 status
= ovsdb_idl_txn_commit_block(txn
);
347 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
348 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
350 ovsdb_idl_txn_destroy(txn
);
357 VLOG_ERR_RL(&rl
, "OVSDB transaction unexpectedly aborted");
364 if (wait_for_reload
) {
366 /* We can't use 'ovs' any longer because ovsdb_idl_run() can
368 const struct ovsrec_open_vswitch
*ovs2
;
371 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs2
, idl
) {
372 if (ovs2
->cur_cfg
>= next_cfg
) {
384 VLOG_ERR_RL(&rl
, "OVSDB transaction needs retry");
388 VLOG_ERR_RL(&rl
, "OVSDB transaction failed: %s",
389 ovsdb_idl_txn_get_error(txn
));
398 add_bridge(struct ovsdb_idl
*idl
, const struct ovsrec_open_vswitch
*ovs
,
401 struct ovsrec_bridge
*br
;
402 struct ovsrec_port
*port
;
403 struct ovsrec_interface
*iface
;
404 struct ovsdb_idl_txn
*txn
;
406 if (find_bridge(ovs
, br_name
)) {
407 VLOG_WARN("addbr %s: bridge %s exists", br_name
, br_name
);
409 } else if (netdev_exists(br_name
)) {
412 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
414 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
416 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
417 if (port_is_fake_bridge(br_cfg
->ports
[j
])) {
418 VLOG_WARN("addbr %s: %s exists as a fake bridge",
425 VLOG_WARN("addbr %s: cannot create bridge %s because a network "
426 "device named %s already exists",
427 br_name
, br_name
, br_name
);
431 txn
= ovsdb_idl_txn_create(idl
);
433 ovsdb_idl_txn_add_comment(txn
, "ovs-brcompatd: addbr %s", br_name
);
435 iface
= ovsrec_interface_insert(txn_from_openvswitch(ovs
));
436 ovsrec_interface_set_name(iface
, br_name
);
438 port
= ovsrec_port_insert(txn_from_openvswitch(ovs
));
439 ovsrec_port_set_name(port
, br_name
);
440 ovsrec_port_set_interfaces(port
, &iface
, 1);
442 br
= ovsrec_bridge_insert(txn_from_openvswitch(ovs
));
443 ovsrec_bridge_set_name(br
, br_name
);
444 ovsrec_bridge_set_ports(br
, &port
, 1);
446 ovs_insert_bridge(ovs
, br
);
448 return commit_txn(txn
, true);
452 add_port(const struct ovsrec_open_vswitch
*ovs
,
453 const struct ovsrec_bridge
*br
, const char *port_name
)
455 struct ovsrec_interface
*iface
;
456 struct ovsrec_port
*port
;
457 struct ovsrec_port
**ports
;
460 /* xxx Check conflicts? */
461 iface
= ovsrec_interface_insert(txn_from_openvswitch(ovs
));
462 ovsrec_interface_set_name(iface
, port_name
);
464 port
= ovsrec_port_insert(txn_from_openvswitch(ovs
));
465 ovsrec_port_set_name(port
, port_name
);
466 ovsrec_port_set_interfaces(port
, &iface
, 1);
468 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
469 for (i
= 0; i
< br
->n_ports
; i
++) {
470 ports
[i
] = br
->ports
[i
];
472 ports
[br
->n_ports
] = port
;
473 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
477 /* Deletes 'port' from 'br'.
479 * After calling this function, 'port' must not be referenced again. */
481 del_port(const struct ovsrec_bridge
*br
, const struct ovsrec_port
*port
)
483 struct ovsrec_port
**ports
;
486 /* Remove 'port' from the bridge's list of ports. */
487 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
488 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
489 if (br
->ports
[i
] != port
) {
490 ports
[n
++] = br
->ports
[i
];
493 ovsrec_bridge_set_ports(br
, ports
, n
);
496 /* Delete all of the port's interfaces. */
497 for (i
= 0; i
< port
->n_interfaces
; i
++) {
498 ovsrec_interface_delete(port
->interfaces
[i
]);
501 /* Delete the port itself. */
502 ovsrec_port_delete(port
);
505 /* Delete 'iface' from 'port' (which must be within 'br'). If 'iface' was
506 * 'port''s only interface, delete 'port' from 'br' also.
508 * After calling this function, 'iface' must not be referenced again. */
510 del_interface(const struct ovsrec_bridge
*br
,
511 const struct ovsrec_port
*port
,
512 const struct ovsrec_interface
*iface
)
514 if (port
->n_interfaces
== 1) {
517 struct ovsrec_interface
**ifaces
;
520 ifaces
= xmalloc(sizeof *port
->interfaces
* port
->n_interfaces
);
521 for (i
= n
= 0; i
< port
->n_interfaces
; i
++) {
522 if (port
->interfaces
[i
] != iface
) {
523 ifaces
[n
++] = port
->interfaces
[i
];
526 ovsrec_port_set_interfaces(port
, ifaces
, n
);
528 ovsrec_interface_delete(iface
);
532 /* Find and return a port within 'br' named 'port_name'. */
533 static const struct ovsrec_port
*
534 find_port(const struct ovsrec_bridge
*br
, const char *port_name
)
538 for (i
= 0; i
< br
->n_ports
; i
++) {
539 struct ovsrec_port
*port
= br
->ports
[i
];
540 if (!strcmp(port_name
, port
->name
)) {
547 /* Find and return an interface within 'br' named 'iface_name'. */
548 static const struct ovsrec_interface
*
549 find_interface(const struct ovsrec_bridge
*br
, const char *iface_name
,
550 struct ovsrec_port
**portp
)
554 for (i
= 0; i
< br
->n_ports
; i
++) {
555 struct ovsrec_port
*port
= br
->ports
[i
];
558 for (j
= 0; j
< port
->n_interfaces
; j
++) {
559 struct ovsrec_interface
*iface
= port
->interfaces
[j
];
560 if (!strcmp(iface
->name
, iface_name
)) {
572 del_bridge(struct ovsdb_idl
*idl
,
573 const struct ovsrec_open_vswitch
*ovs
, const char *br_name
)
575 struct ovsrec_bridge
*br
= find_bridge(ovs
, br_name
);
576 struct ovsrec_bridge
**bridges
;
577 struct ovsdb_idl_txn
*txn
;
581 VLOG_WARN("delbr %s: no bridge named %s", br_name
, br_name
);
585 txn
= ovsdb_idl_txn_create(idl
);
587 ovsdb_idl_txn_add_comment(txn
, "ovs-brcompatd: delbr %s", br_name
);
589 /* Delete everything that the bridge points to, then delete the bridge
591 while (br
->n_ports
> 0) {
592 del_port(br
, br
->ports
[0]);
594 for (i
= 0; i
< br
->n_mirrors
; i
++) {
595 ovsrec_mirror_delete(br
->mirrors
[i
]);
598 ovsrec_netflow_delete(br
->netflow
);
601 ovsrec_sflow_delete(br
->sflow
);
603 for (i
= 0; i
< br
->n_controller
; i
++) {
604 ovsrec_controller_delete(br
->controller
[i
]);
607 /* Remove 'br' from the vswitch's list of bridges. */
608 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
609 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
610 if (ovs
->bridges
[i
] != br
) {
611 bridges
[n
++] = ovs
->bridges
[i
];
614 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
617 /* Delete the bridge itself. */
618 ovsrec_bridge_delete(br
);
620 return commit_txn(txn
, true);
624 parse_command(struct ofpbuf
*buffer
, uint32_t *seq
, const char **br_name
,
625 const char **port_name
, uint64_t *count
, uint64_t *skip
)
627 static const struct nl_policy policy
[] = {
628 [BRC_GENL_A_DP_NAME
] = { .type
= NL_A_STRING
, .optional
= true },
629 [BRC_GENL_A_PORT_NAME
] = { .type
= NL_A_STRING
, .optional
= true },
630 [BRC_GENL_A_FDB_COUNT
] = { .type
= NL_A_U64
, .optional
= true },
631 [BRC_GENL_A_FDB_SKIP
] = { .type
= NL_A_U64
, .optional
= true },
633 struct nlattr
*attrs
[ARRAY_SIZE(policy
)];
635 if (!nl_policy_parse(buffer
, NLMSG_HDRLEN
+ GENL_HDRLEN
, policy
,
636 attrs
, ARRAY_SIZE(policy
))
637 || (br_name
&& !attrs
[BRC_GENL_A_DP_NAME
])
638 || (port_name
&& !attrs
[BRC_GENL_A_PORT_NAME
])
639 || (count
&& !attrs
[BRC_GENL_A_FDB_COUNT
])
640 || (skip
&& !attrs
[BRC_GENL_A_FDB_SKIP
])) {
644 *seq
= ((struct nlmsghdr
*) buffer
->data
)->nlmsg_seq
;
646 *br_name
= nl_attr_get_string(attrs
[BRC_GENL_A_DP_NAME
]);
649 *port_name
= nl_attr_get_string(attrs
[BRC_GENL_A_PORT_NAME
]);
652 *count
= nl_attr_get_u64(attrs
[BRC_GENL_A_FDB_COUNT
]);
655 *skip
= nl_attr_get_u64(attrs
[BRC_GENL_A_FDB_SKIP
]);
660 /* Composes and returns a reply to a request made by the datapath with Netlink
661 * sequence number 'seq' and error code 'error'. The caller may add additional
662 * attributes to the message, then it may send it with send_reply(). */
663 static struct ofpbuf
*
664 compose_reply(uint32_t seq
, int error
)
666 struct ofpbuf
*reply
= ofpbuf_new(4096);
667 nl_msg_put_genlmsghdr(reply
, 32, brc_family
, NLM_F_REQUEST
,
668 BRC_GENL_C_DP_RESULT
, 1);
669 ((struct nlmsghdr
*) reply
->data
)->nlmsg_seq
= seq
;
670 nl_msg_put_u32(reply
, BRC_GENL_A_ERR_CODE
, error
);
674 /* Sends 'reply' to the datapath and frees it. */
676 send_reply(struct ofpbuf
*reply
)
678 int retval
= nl_sock_send(brc_sock
, reply
, false);
680 VLOG_WARN_RL(&rl
, "replying to brcompat request: %s",
683 ofpbuf_delete(reply
);
686 /* Composes and sends a reply to a request made by the datapath with Netlink
687 * sequence number 'seq' and error code 'error'. */
689 send_simple_reply(uint32_t seq
, int error
)
691 send_reply(compose_reply(seq
, error
));
695 handle_bridge_cmd(struct ovsdb_idl
*idl
,
696 const struct ovsrec_open_vswitch
*ovs
,
697 struct ofpbuf
*buffer
, bool add
)
703 error
= parse_command(buffer
, &seq
, &br_name
, NULL
, NULL
, NULL
);
708 retval
= (add
? add_bridge
: del_bridge
)(idl
, ovs
, br_name
);
709 VLOG_INFO_RL(&rl
, "%sbr %s: %s",
710 add
? "add" : "del", br_name
, strerror(retval
));
711 } while (retval
== EAGAIN
);
713 send_simple_reply(seq
, error
);
718 static const struct nl_policy brc_port_policy
[] = {
719 [BRC_GENL_A_DP_NAME
] = { .type
= NL_A_STRING
},
720 [BRC_GENL_A_PORT_NAME
] = { .type
= NL_A_STRING
},
724 handle_port_cmd(struct ovsdb_idl
*idl
,
725 const struct ovsrec_open_vswitch
*ovs
,
726 struct ofpbuf
*buffer
, bool add
)
728 const char *cmd_name
= add
? "add-if" : "del-if";
729 const char *br_name
, *port_name
;
733 error
= parse_command(buffer
, &seq
, &br_name
, &port_name
, NULL
, NULL
);
735 struct ovsrec_bridge
*br
= find_bridge(ovs
, br_name
);
738 VLOG_WARN("%s %s %s: no bridge named %s",
739 cmd_name
, br_name
, port_name
, br_name
);
741 } else if (!netdev_exists(port_name
)) {
742 VLOG_WARN("%s %s %s: no network device named %s",
743 cmd_name
, br_name
, port_name
, port_name
);
747 struct ovsdb_idl_txn
*txn
= ovsdb_idl_txn_create(idl
);
750 ovsdb_idl_txn_add_comment(txn
, "ovs-brcompatd: add-if %s",
752 add_port(ovs
, br
, port_name
);
754 const struct ovsrec_port
*port
= find_port(br
, port_name
);
756 ovsdb_idl_txn_add_comment(txn
,
757 "ovs-brcompatd: del-if %s",
763 error
= commit_txn(txn
, true);
764 VLOG_INFO_RL(&rl
, "%s %s %s: %s",
765 cmd_name
, br_name
, port_name
, strerror(error
));
766 } while (error
== EAGAIN
);
768 send_simple_reply(seq
, error
);
774 /* The caller is responsible for freeing '*ovs_name' if the call is
777 linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch
*ovs
,
778 const char *linux_name
,
779 const struct ovsrec_bridge
**ovs_bridge
,
782 *ovs_bridge
= find_bridge(ovs
, linux_name
);
784 /* Bridge name is the same. We are interested in VLAN 0. */
788 /* No such Open vSwitch bridge 'linux_name', but there might be an
789 * internal port named 'linux_name' on some other bridge
790 * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
791 * port 'linux_name' on the bridge named 'ovs_bridge'. */
794 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
795 const struct ovsrec_bridge
*br
= ovs
->bridges
[i
];
797 for (j
= 0; j
< br
->n_ports
; j
++) {
798 const struct ovsrec_port
*port
= br
->ports
[j
];
800 if (!strcmp(port
->name
, linux_name
)) {
802 *br_vlan
= port
->n_tag
? *port
->tag
: -1;
813 handle_fdb_query_cmd(const struct ovsrec_open_vswitch
*ovs
,
814 struct ofpbuf
*buffer
)
816 /* This structure is copied directly from the Linux 2.6.30 header files.
817 * It would be more straightforward to #include <linux/if_bridge.h>, but
818 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
819 * with old header files won't have it. */
824 __u32 ageing_timer_value
;
833 struct mac
*local_macs
;
837 /* Impedance matching between the vswitchd and Linux kernel notions of what
838 * a bridge is. The kernel only handles a single VLAN per bridge, but
839 * vswitchd can deal with all the VLANs on a single bridge. We have to
840 * pretend that the former is the case even though the latter is the
842 const char *linux_name
; /* Name used by brctl. */
843 const struct ovsrec_bridge
*ovs_bridge
; /* Bridge used by ovs-vswitchd. */
844 int br_vlan
; /* VLAN tag. */
847 struct ofpbuf query_data
;
848 struct ofpbuf
*reply
;
849 char *unixctl_command
;
850 uint64_t count
, skip
;
856 /* Parse the command received from brcompat_mod. */
857 error
= parse_command(buffer
, &seq
, &linux_name
, NULL
, &count
, &skip
);
862 /* Figure out vswitchd bridge and VLAN. */
863 error
= linux_bridge_to_ovs_bridge(ovs
, linux_name
,
864 &ovs_bridge
, &br_vlan
);
866 send_simple_reply(seq
, error
);
870 /* Fetch the forwarding database using ovs-appctl. */
871 unixctl_command
= xasprintf("fdb/show %s", ovs_bridge
->name
);
872 error
= execute_appctl_command(unixctl_command
, &output
);
873 free(unixctl_command
);
875 send_simple_reply(seq
, error
);
879 /* Fetch the MAC address for each interface on the bridge, so that we can
880 * fill in the is_local field in the response. */
882 get_bridge_ifaces(ovs_bridge
, &ifaces
, br_vlan
);
883 local_macs
= xmalloc(ifaces
.n
* sizeof *local_macs
);
885 for (i
= 0; i
< ifaces
.n
; i
++) {
886 const char *iface_name
= ifaces
.names
[i
];
887 struct mac
*mac
= &local_macs
[n_local_macs
];
888 struct netdev
*netdev
;
890 error
= netdev_open_default(iface_name
, &netdev
);
892 if (!netdev_get_etheraddr(netdev
, mac
->addr
)) {
895 netdev_close(netdev
);
898 svec_destroy(&ifaces
);
900 /* Parse the response from ovs-appctl and convert it to binary format to
901 * pass back to the kernel. */
902 ofpbuf_init(&query_data
, sizeof(struct __fdb_entry
) * 8);
904 strtok_r(output
, "\n", &save_ptr
); /* Skip header line. */
906 struct __fdb_entry
*entry
;
908 uint8_t mac
[ETH_ADDR_LEN
];
912 line
= strtok_r(NULL
, "\n", &save_ptr
);
917 if (sscanf(line
, "%d %d "ETH_ADDR_SCAN_FMT
" %d",
918 &port
, &vlan
, ETH_ADDR_SCAN_ARGS(mac
), &age
)
919 != 2 + ETH_ADDR_SCAN_COUNT
+ 1) {
920 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
921 VLOG_INFO_RL(&rl
, "fdb/show output has invalid format: %s", line
);
925 if (vlan
!= br_vlan
) {
934 /* Is this the MAC address of an interface on the bridge? */
936 for (i
= 0; i
< n_local_macs
; i
++) {
937 if (eth_addr_equals(local_macs
[i
].addr
, mac
)) {
943 entry
= ofpbuf_put_uninit(&query_data
, sizeof *entry
);
944 memcpy(entry
->mac_addr
, mac
, ETH_ADDR_LEN
);
945 entry
->port_no
= port
& 0xff;
946 entry
->is_local
= is_local
;
947 entry
->ageing_timer_value
= age
* HZ
;
948 entry
->port_hi
= (port
& 0xff00) >> 8;
955 /* Compose and send reply to datapath. */
956 reply
= compose_reply(seq
, 0);
957 nl_msg_put_unspec(reply
, BRC_GENL_A_FDB_DATA
,
958 query_data
.data
, query_data
.size
);
962 ofpbuf_uninit(&query_data
);
968 send_ifindex_reply(uint32_t seq
, struct svec
*ifaces
)
970 struct ofpbuf
*reply
;
976 /* Make sure that any given interface only occurs once. This shouldn't
977 * happen, but who knows what people put into their configuration files. */
978 svec_sort_unique(ifaces
);
980 /* Convert 'ifaces' into ifindexes. */
982 indices
= xmalloc(ifaces
->n
* sizeof *indices
);
983 SVEC_FOR_EACH (i
, iface
, ifaces
) {
984 int ifindex
= if_nametoindex(iface
);
986 indices
[n_indices
++] = ifindex
;
990 /* Compose and send reply. */
991 reply
= compose_reply(seq
, 0);
992 nl_msg_put_unspec(reply
, BRC_GENL_A_IFINDEXES
,
993 indices
, n_indices
* sizeof *indices
);
1001 handle_get_bridges_cmd(const struct ovsrec_open_vswitch
*ovs
,
1002 struct ofpbuf
*buffer
)
1004 struct svec bridges
;
1011 /* Parse Netlink command.
1013 * The command doesn't actually have any arguments, but we need the
1014 * sequence number to send the reply. */
1015 error
= parse_command(buffer
, &seq
, NULL
, NULL
, NULL
, NULL
);
1020 /* Get all the real bridges and all the fake ones. */
1021 svec_init(&bridges
);
1022 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
1023 const struct ovsrec_bridge
*br
= ovs
->bridges
[i
];
1025 svec_add(&bridges
, br
->name
);
1026 for (j
= 0; j
< br
->n_ports
; j
++) {
1027 const struct ovsrec_port
*port
= br
->ports
[j
];
1029 if (port
->fake_bridge
) {
1030 svec_add(&bridges
, port
->name
);
1035 send_ifindex_reply(seq
, &bridges
);
1036 svec_destroy(&bridges
);
1042 handle_get_ports_cmd(const struct ovsrec_open_vswitch
*ovs
,
1043 struct ofpbuf
*buffer
)
1047 const char *linux_name
;
1048 const struct ovsrec_bridge
*ovs_bridge
;
1055 /* Parse Netlink command. */
1056 error
= parse_command(buffer
, &seq
, &linux_name
, NULL
, NULL
, NULL
);
1061 error
= linux_bridge_to_ovs_bridge(ovs
, linux_name
,
1062 &ovs_bridge
, &br_vlan
);
1064 send_simple_reply(seq
, error
);
1069 get_bridge_ports(ovs_bridge
, &ports
, br_vlan
);
1071 svec_del(&ports
, linux_name
);
1072 send_ifindex_reply(seq
, &ports
); /* XXX bonds won't show up */
1073 svec_destroy(&ports
);
1079 brc_recv_update(struct ovsdb_idl
*idl
)
1082 struct ofpbuf
*buffer
;
1083 struct genlmsghdr
*genlmsghdr
;
1084 const struct ovsrec_open_vswitch
*ovs
;
1088 ofpbuf_delete(buffer
);
1089 retval
= nl_sock_recv(brc_sock
, &buffer
, false);
1090 } while (retval
== ENOBUFS
1092 && (nl_msg_nlmsgerr(buffer
, NULL
)
1093 || nl_msg_nlmsghdr(buffer
)->nlmsg_type
== NLMSG_DONE
)));
1095 if (retval
!= EAGAIN
) {
1096 VLOG_WARN_RL(&rl
, "brc_recv_update: %s", strerror(retval
));
1101 genlmsghdr
= nl_msg_genlmsghdr(buffer
);
1103 VLOG_WARN_RL(&rl
, "received packet too short for generic NetLink");
1107 if (nl_msg_nlmsghdr(buffer
)->nlmsg_type
!= brc_family
) {
1108 VLOG_DBG_RL(&rl
, "received type (%"PRIu16
") != brcompat family (%d)",
1109 nl_msg_nlmsghdr(buffer
)->nlmsg_type
, brc_family
);
1113 /* Get the Open vSwitch configuration. Just drop the request on the floor
1114 * if a valid configuration doesn't exist. (We could check this earlier,
1115 * but we want to drain pending Netlink messages even when there is no Open
1116 * vSwitch configuration.) */
1117 ovs
= ovsrec_open_vswitch_first(idl
);
1119 VLOG_WARN_RL(&rl
, "could not find valid configuration to update");
1123 switch (genlmsghdr
->cmd
) {
1124 case BRC_GENL_C_DP_ADD
:
1125 handle_bridge_cmd(idl
, ovs
, buffer
, true);
1128 case BRC_GENL_C_DP_DEL
:
1129 handle_bridge_cmd(idl
, ovs
, buffer
, false);
1132 case BRC_GENL_C_PORT_ADD
:
1133 handle_port_cmd(idl
, ovs
, buffer
, true);
1136 case BRC_GENL_C_PORT_DEL
:
1137 handle_port_cmd(idl
, ovs
, buffer
, false);
1140 case BRC_GENL_C_FDB_QUERY
:
1141 handle_fdb_query_cmd(ovs
, buffer
);
1144 case BRC_GENL_C_GET_BRIDGES
:
1145 handle_get_bridges_cmd(ovs
, buffer
);
1148 case BRC_GENL_C_GET_PORTS
:
1149 handle_get_ports_cmd(ovs
, buffer
);
1153 VLOG_WARN_RL(&rl
, "received unknown brc netlink command: %d\n",
1159 ofpbuf_delete(buffer
);
1163 /* Check for interface configuration changes announced through RTNL. */
1165 rtnl_recv_update(struct ovsdb_idl
*idl
,
1166 const struct ovsrec_open_vswitch
*ovs
)
1170 int error
= nl_sock_recv(rtnl_sock
, &buf
, false);
1171 if (error
== EAGAIN
) {
1172 /* Nothing to do. */
1173 } else if (error
== ENOBUFS
) {
1174 VLOG_WARN_RL(&rl
, "network monitor socket overflowed");
1176 VLOG_WARN_RL(&rl
, "error on network monitor socket: %s",
1179 struct nlattr
*attrs
[ARRAY_SIZE(rtnlgrp_link_policy
)];
1180 struct nlmsghdr
*nlh
;
1181 struct ifinfomsg
*iim
;
1183 nlh
= ofpbuf_at(buf
, 0, NLMSG_HDRLEN
);
1184 iim
= ofpbuf_at(buf
, NLMSG_HDRLEN
, sizeof *iim
);
1186 VLOG_WARN_RL(&rl
, "received bad rtnl message (no ifinfomsg)");
1191 if (!nl_policy_parse(buf
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
),
1192 rtnlgrp_link_policy
,
1193 attrs
, ARRAY_SIZE(rtnlgrp_link_policy
))) {
1194 VLOG_WARN_RL(&rl
,"received bad rtnl message (policy)");
1198 if (nlh
->nlmsg_type
== RTM_DELLINK
&& attrs
[IFLA_MASTER
]) {
1199 const char *port_name
= nl_attr_get_string(attrs
[IFLA_IFNAME
]);
1200 char br_name
[IFNAMSIZ
];
1201 uint32_t br_idx
= nl_attr_get_u32(attrs
[IFLA_MASTER
]);
1203 if (!if_indextoname(br_idx
, br_name
)) {
1208 if (!netdev_exists(port_name
)) {
1209 /* Network device is really gone. */
1210 struct ovsdb_idl_txn
*txn
;
1211 const struct ovsrec_interface
*iface
;
1212 struct ovsrec_port
*port
;
1213 struct ovsrec_bridge
*br
;
1215 VLOG_INFO("network device %s destroyed, "
1216 "removing from bridge %s", port_name
, br_name
);
1218 br
= find_bridge(ovs
, br_name
);
1220 VLOG_WARN("no bridge named %s from which to remove %s",
1221 br_name
, port_name
);
1226 txn
= ovsdb_idl_txn_create(idl
);
1228 iface
= find_interface(br
, port_name
, &port
);
1230 del_interface(br
, port
, iface
);
1231 ovsdb_idl_txn_add_comment(txn
,
1232 "ovs-brcompatd: destroy port %s",
1236 commit_txn(txn
, false);
1238 /* A network device by that name exists even though the kernel
1239 * told us it had disappeared. Probably, what happened was
1242 * 1. Device destroyed.
1243 * 2. Notification sent to us.
1244 * 3. New device created with same name as old one.
1245 * 4. ovs-brcompatd notified, removes device from bridge.
1247 * There's no a priori reason that in this situation that the
1248 * new device with the same name should remain in the bridge;
1249 * on the contrary, that would be unexpected. *But* there is
1250 * one important situation where, if we do this, bad things
1251 * happen. This is the case of XenServer Tools version 5.0.0,
1252 * which on boot of a Windows VM cause something like this to
1253 * happen on the Xen host:
1255 * i. Create tap1.0 and vif1.0.
1256 * ii. Delete tap1.0.
1257 * iii. Delete vif1.0.
1258 * iv. Re-create vif1.0.
1260 * (XenServer Tools 5.5.0 does not exhibit this behavior, and
1261 * neither does a VM without Tools installed at all.@.)
1263 * Steps iii and iv happen within a few seconds of each other.
1264 * Step iv causes /etc/xensource/scripts/vif to run, which in
1265 * turn calls ovs-cfg-mod to add the new device to the bridge.
1266 * If step iv happens after step 4 (in our first list of
1267 * steps), then all is well, but if it happens between 3 and 4
1268 * (which can easily happen if ovs-brcompatd has to wait to
1269 * lock the configuration file), then we will remove the new
1270 * incarnation from the bridge instead of the old one!
1272 * So, to avoid this problem, we do nothing here. This is
1273 * strictly incorrect except for this one particular case, and
1274 * perhaps that will bite us someday. If that happens, then we
1275 * will have to somehow track network devices by ifindex, since
1276 * a new device will have a new ifindex even if it has the same
1277 * name as an old device.
1279 VLOG_INFO("kernel reported network device %s removed but "
1280 "a device by that name exists (XS Tools 5.0.0?)",
1289 main(int argc
, char *argv
[])
1291 extern struct vlog_module VLM_reconnect
;
1292 struct unixctl_server
*unixctl
;
1294 struct ovsdb_idl
*idl
;
1297 proctitle_init(argc
, argv
);
1298 set_program_name(argv
[0]);
1299 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
1300 vlog_set_levels(&VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
1302 remote
= parse_options(argc
, argv
);
1303 signal(SIGPIPE
, SIG_IGN
);
1307 die_if_already_running();
1310 retval
= unixctl_server_create(NULL
, &unixctl
);
1315 if (brc_open(&brc_sock
)) {
1316 ovs_fatal(0, "could not open brcompat socket. Check "
1317 "\"brcompat\" kernel module.");
1320 if (prune_timeout
) {
1321 if (nl_sock_create(NETLINK_ROUTE
, RTNLGRP_LINK
, 0, 0, &rtnl_sock
)) {
1322 ovs_fatal(0, "could not create rtnetlink socket");
1326 daemonize_complete();
1328 idl
= ovsdb_idl_create(remote
, &ovsrec_idl_class
, true);
1331 const struct ovsrec_open_vswitch
*ovs
;
1335 unixctl_server_run(unixctl
);
1336 brc_recv_update(idl
);
1338 ovs
= ovsrec_open_vswitch_first(idl
);
1339 if (!ovs
&& ovsdb_idl_has_ever_connected(idl
)) {
1340 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1341 VLOG_WARN_RL(&rl
, "%s: database does not contain any Open vSwitch "
1342 "configuration", remote
);
1346 /* If 'prune_timeout' is non-zero, we actively prune from the
1347 * configuration of port entries that are no longer valid. We
1350 * 1) The kernel explicitly notifies us of removed ports
1351 * through the RTNL messages.
1353 * 2) We periodically check all ports associated with bridges
1354 * to see if they no longer exist.
1356 if (ovs
&& prune_timeout
) {
1357 rtnl_recv_update(idl
, ovs
);
1358 nl_sock_wait(rtnl_sock
, POLLIN
);
1359 poll_timer_wait(prune_timeout
);
1363 nl_sock_wait(brc_sock
, POLLIN
);
1364 ovsdb_idl_wait(idl
);
1365 unixctl_server_wait(unixctl
);
1370 ovsdb_idl_destroy(idl
);
1376 validate_appctl_command(void)
1382 for (p
= strchr(appctl_command
, '%'); p
; p
= strchr(p
+ 2, '%')) {
1384 /* Nothing to do. */
1385 } else if (p
[1] == 's') {
1388 ovs_fatal(0, "only '%%s' and '%%%%' allowed in --appctl-command");
1392 ovs_fatal(0, "'%%s' must appear exactly once in --appctl-command");
1397 parse_options(int argc
, char *argv
[])
1403 LEAK_CHECKER_OPTION_ENUMS
1405 static struct option long_options
[] = {
1406 {"help", no_argument
, 0, 'h'},
1407 {"version", no_argument
, 0, 'V'},
1408 {"prune-timeout", required_argument
, 0, OPT_PRUNE_TIMEOUT
},
1409 {"appctl-command", required_argument
, 0, OPT_APPCTL_COMMAND
},
1410 DAEMON_LONG_OPTIONS
,
1412 LEAK_CHECKER_LONG_OPTIONS
,
1415 char *short_options
= long_options_to_short_options(long_options
);
1417 appctl_command
= xasprintf("%s/ovs-appctl %%s", ovs_bindir());
1421 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
1432 OVS_PRINT_VERSION(0, 0);
1435 case OPT_PRUNE_TIMEOUT
:
1436 prune_timeout
= atoi(optarg
) * 1000;
1439 case OPT_APPCTL_COMMAND
:
1440 appctl_command
= optarg
;
1443 VLOG_OPTION_HANDLERS
1444 DAEMON_OPTION_HANDLERS
1445 LEAK_CHECKER_OPTION_HANDLERS
1454 free(short_options
);
1456 validate_appctl_command();
1462 ovs_fatal(0, "database socket is non-option argument; "
1463 "use --help for usage");
1472 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
1473 "usage: %s [OPTIONS] CONFIG\n"
1474 "CONFIG is the configuration file used by ovs-vswitchd.\n",
1475 program_name
, program_name
);
1476 printf("\nConfiguration options:\n"
1477 " --appctl-command=COMMAND shell command to run ovs-appctl\n"
1478 " --prune-timeout=SECS wait at most SECS before pruning ports\n"
1482 printf("\nOther options:\n"
1483 " -h, --help display this help message\n"
1484 " -V, --version display version information\n");
1485 leak_checker_usage();
1486 printf("\nThe default appctl command is:\n%s\n", appctl_command
);