1 /* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <asm/param.h>
25 #include <linux/genetlink.h>
26 #include <linux/rtnetlink.h>
30 #include <sys/types.h>
36 #include "command-line.h"
40 #include "dynamic-string.h"
41 #include "fatal-signal.h"
43 #include "leak-checker.h"
46 #include "netlink-socket.h"
48 #include "openvswitch/brcompat-netlink.h"
49 #include "ovsdb-idl.h"
51 #include "poll-loop.h"
59 #include "vswitchd/vswitch-idl.h"
61 VLOG_DEFINE_THIS_MODULE(brcompatd
);
64 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
66 /* Actions to modify bridge compatibility configuration. */
74 static const char *parse_options(int argc
, char *argv
[]);
75 static void usage(void) NO_RETURN
;
77 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 60);
79 /* Maximum number of milliseconds to wait before pruning port entries that
80 * no longer exist. If set to zero, ports are never pruned. */
81 static int prune_timeout
= 5000;
83 /* Shell command to execute (via popen()) to send a control command to the
84 * running ovs-vswitchd process. The string must contain one instance of %s,
85 * which is replaced by the control command. */
86 static char *appctl_command
;
88 /* Netlink socket to listen for interface changes. */
89 static struct nl_sock
*rtnl_sock
;
91 /* Netlink socket to bridge compatibility kernel module. */
92 static struct nl_sock
*brc_sock
;
94 /* The Generic Netlink family number used for bridge compatibility. */
95 static int brc_family
;
97 static const struct nl_policy brc_multicast_policy
[] = {
98 [BRC_GENL_A_MC_GROUP
] = {.type
= NL_A_U32
}
101 static const struct nl_policy rtnlgrp_link_policy
[] = {
102 [IFLA_IFNAME
] = { .type
= NL_A_STRING
, .optional
= false },
103 [IFLA_MASTER
] = { .type
= NL_A_U32
, .optional
= true },
107 lookup_brc_multicast_group(int *multicast_group
)
109 struct nl_sock
*sock
;
110 struct ofpbuf request
, *reply
;
111 struct nlattr
*attrs
[ARRAY_SIZE(brc_multicast_policy
)];
114 retval
= nl_sock_create(NETLINK_GENERIC
, &sock
);
118 ofpbuf_init(&request
, 0);
119 nl_msg_put_genlmsghdr(&request
, 0, brc_family
,
120 NLM_F_REQUEST
, BRC_GENL_C_QUERY_MC
, 1);
121 retval
= nl_sock_transact(sock
, &request
, &reply
);
122 ofpbuf_uninit(&request
);
124 nl_sock_destroy(sock
);
127 if (!nl_policy_parse(reply
, NLMSG_HDRLEN
+ GENL_HDRLEN
,
128 brc_multicast_policy
, attrs
,
129 ARRAY_SIZE(brc_multicast_policy
))) {
130 nl_sock_destroy(sock
);
131 ofpbuf_delete(reply
);
134 *multicast_group
= nl_attr_get_u32(attrs
[BRC_GENL_A_MC_GROUP
]);
135 nl_sock_destroy(sock
);
136 ofpbuf_delete(reply
);
141 /* Opens a socket for brcompat notifications. Returns 0 if successful,
142 * otherwise a positive errno value. */
144 brc_open(struct nl_sock
**sock
)
146 int multicast_group
= 0;
149 retval
= nl_lookup_genl_family(BRC_GENL_FAMILY_NAME
, &brc_family
);
154 retval
= lookup_brc_multicast_group(&multicast_group
);
159 retval
= nl_sock_create(NETLINK_GENERIC
, sock
);
164 retval
= nl_sock_join_mcgroup(*sock
, multicast_group
);
166 nl_sock_destroy(*sock
);
172 static const struct nl_policy brc_dp_policy
[] = {
173 [BRC_GENL_A_DP_NAME
] = { .type
= NL_A_STRING
},
176 static struct ovsrec_bridge
*
177 find_bridge(const struct ovsrec_open_vswitch
*ovs
, const char *br_name
)
181 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
182 if (!strcmp(br_name
, ovs
->bridges
[i
]->name
)) {
183 return ovs
->bridges
[i
];
191 execute_appctl_command(const char *unixctl_command
, char **output
)
193 char *stdout_log
, *stderr_log
;
199 argv
[2] = xasprintf(appctl_command
, unixctl_command
);
202 /* Run process and log status. */
203 error
= process_run_capture(argv
, &stdout_log
, &stderr_log
, &status
);
205 VLOG_ERR("failed to execute %s command via ovs-appctl: %s",
206 unixctl_command
, strerror(error
));
208 char *msg
= process_status_msg(status
);
209 VLOG_ERR("ovs-appctl exited with error (%s)", msg
);
214 /* Deal with stdout_log. */
216 *output
= stdout_log
;
221 /* Deal with stderr_log */
222 if (stderr_log
&& *stderr_log
) {
223 VLOG_INFO("ovs-appctl wrote to stderr:\n%s", stderr_log
);
233 do_get_bridge_parts(const struct ovsrec_bridge
*br
, struct svec
*parts
,
234 int vlan
, bool break_down_bonds
)
238 for (i
= 0; i
< br
->n_ports
; i
++) {
239 const struct ovsrec_port
*port
= br
->ports
[i
];
242 int port_vlan
= port
->n_tag
? *port
->tag
: 0;
243 if (vlan
!= port_vlan
) {
247 if (break_down_bonds
) {
248 for (j
= 0; j
< port
->n_interfaces
; j
++) {
249 const struct ovsrec_interface
*iface
= port
->interfaces
[j
];
250 svec_add(parts
, iface
->name
);
253 svec_add(parts
, port
->name
);
258 /* Add all the interfaces for 'bridge' to 'ifaces', breaking bonded interfaces
259 * down into their constituent parts.
261 * If 'vlan' < 0, all interfaces on 'bridge' are reported. If 'vlan' == 0,
262 * then only interfaces for trunk ports or ports with implicit VLAN 0 are
263 * reported. If 'vlan' > 0, only interfaces with implicit VLAN 'vlan' are
266 get_bridge_ifaces(const struct ovsrec_bridge
*br
, struct svec
*ifaces
,
269 do_get_bridge_parts(br
, ifaces
, vlan
, true);
272 /* Add all the ports for 'bridge' to 'ports'. Bonded ports are reported under
273 * the bond name, not broken down into their constituent interfaces.
275 * If 'vlan' < 0, all ports on 'bridge' are reported. If 'vlan' == 0, then
276 * only trunk ports or ports with implicit VLAN 0 are reported. If 'vlan' > 0,
277 * only port with implicit VLAN 'vlan' are reported. */
279 get_bridge_ports(const struct ovsrec_bridge
*br
, struct svec
*ports
,
282 do_get_bridge_parts(br
, ports
, vlan
, false);
285 static struct ovsdb_idl_txn
*
286 txn_from_openvswitch(const struct ovsrec_open_vswitch
*ovs
)
288 return ovsdb_idl_txn_get(&ovs
->header_
);
292 port_is_fake_bridge(const struct ovsrec_port
*port
)
294 return (port
->fake_bridge
296 && *port
->tag
>= 1 && *port
->tag
<= 4095);
300 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
301 struct ovsrec_bridge
*bridge
)
303 struct ovsrec_bridge
**bridges
;
306 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
307 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
308 bridges
[i
] = ovs
->bridges
[i
];
310 bridges
[ovs
->n_bridges
] = bridge
;
311 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
316 where_uuid_equals(const struct uuid
*uuid
)
321 json_string_create("_uuid"),
322 json_string_create("=="),
324 json_string_create("uuid"),
325 json_string_create_nocopy(
326 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
329 /* Commits 'txn'. If 'wait_for_reload' is true, also waits for Open vSwitch to
330 reload the configuration before returning.
332 Returns EAGAIN if the caller should try the operation again, 0 on success,
333 otherwise a positive errno value. */
335 commit_txn(struct ovsdb_idl_txn
*txn
, bool wait_for_reload
)
337 struct ovsdb_idl
*idl
= ovsdb_idl_txn_get_idl (txn
);
338 enum ovsdb_idl_txn_status status
;
339 int64_t next_cfg
= 0;
341 if (wait_for_reload
) {
342 const struct ovsrec_open_vswitch
*ovs
= ovsrec_open_vswitch_first(idl
);
343 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
344 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
347 status
= ovsdb_idl_txn_commit_block(txn
);
348 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
349 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
351 ovsdb_idl_txn_destroy(txn
);
358 VLOG_ERR_RL(&rl
, "OVSDB transaction unexpectedly aborted");
365 if (wait_for_reload
) {
367 /* We can't use 'ovs' any longer because ovsdb_idl_run() can
369 const struct ovsrec_open_vswitch
*ovs2
;
372 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs2
, idl
) {
373 if (ovs2
->cur_cfg
>= next_cfg
) {
385 VLOG_ERR_RL(&rl
, "OVSDB transaction needs retry");
389 VLOG_ERR_RL(&rl
, "OVSDB transaction failed: %s",
390 ovsdb_idl_txn_get_error(txn
));
399 add_bridge(struct ovsdb_idl
*idl
, const struct ovsrec_open_vswitch
*ovs
,
402 struct ovsrec_bridge
*br
;
403 struct ovsrec_port
*port
;
404 struct ovsrec_interface
*iface
;
405 struct ovsdb_idl_txn
*txn
;
407 if (find_bridge(ovs
, br_name
)) {
408 VLOG_WARN("addbr %s: bridge %s exists", br_name
, br_name
);
410 } else if (netdev_exists(br_name
)) {
413 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
415 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
417 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
418 if (port_is_fake_bridge(br_cfg
->ports
[j
])) {
419 VLOG_WARN("addbr %s: %s exists as a fake bridge",
426 VLOG_WARN("addbr %s: cannot create bridge %s because a network "
427 "device named %s already exists",
428 br_name
, br_name
, br_name
);
432 txn
= ovsdb_idl_txn_create(idl
);
434 ovsdb_idl_txn_add_comment(txn
, "ovs-brcompatd: addbr %s", br_name
);
436 iface
= ovsrec_interface_insert(txn_from_openvswitch(ovs
));
437 ovsrec_interface_set_name(iface
, br_name
);
439 port
= ovsrec_port_insert(txn_from_openvswitch(ovs
));
440 ovsrec_port_set_name(port
, br_name
);
441 ovsrec_port_set_interfaces(port
, &iface
, 1);
443 br
= ovsrec_bridge_insert(txn_from_openvswitch(ovs
));
444 ovsrec_bridge_set_name(br
, br_name
);
445 ovsrec_bridge_set_ports(br
, &port
, 1);
447 ovs_insert_bridge(ovs
, br
);
449 return commit_txn(txn
, true);
453 add_port(const struct ovsrec_open_vswitch
*ovs
,
454 const struct ovsrec_bridge
*br
, const char *port_name
)
456 struct ovsrec_interface
*iface
;
457 struct ovsrec_port
*port
;
458 struct ovsrec_port
**ports
;
461 /* xxx Check conflicts? */
462 iface
= ovsrec_interface_insert(txn_from_openvswitch(ovs
));
463 ovsrec_interface_set_name(iface
, port_name
);
465 port
= ovsrec_port_insert(txn_from_openvswitch(ovs
));
466 ovsrec_port_set_name(port
, port_name
);
467 ovsrec_port_set_interfaces(port
, &iface
, 1);
469 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
470 for (i
= 0; i
< br
->n_ports
; i
++) {
471 ports
[i
] = br
->ports
[i
];
473 ports
[br
->n_ports
] = port
;
474 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
478 /* Deletes 'port' from 'br'.
480 * After calling this function, 'port' must not be referenced again. */
482 del_port(const struct ovsrec_bridge
*br
, const struct ovsrec_port
*port
)
484 struct ovsrec_port
**ports
;
487 /* Remove 'port' from the bridge's list of ports. */
488 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
489 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
490 if (br
->ports
[i
] != port
) {
491 ports
[n
++] = br
->ports
[i
];
494 ovsrec_bridge_set_ports(br
, ports
, n
);
498 /* Delete 'iface' from 'port' (which must be within 'br'). If 'iface' was
499 * 'port''s only interface, delete 'port' from 'br' also.
501 * After calling this function, 'iface' must not be referenced again. */
503 del_interface(const struct ovsrec_bridge
*br
,
504 const struct ovsrec_port
*port
,
505 const struct ovsrec_interface
*iface
)
507 if (port
->n_interfaces
== 1) {
510 struct ovsrec_interface
**ifaces
;
513 ifaces
= xmalloc(sizeof *port
->interfaces
* port
->n_interfaces
);
514 for (i
= n
= 0; i
< port
->n_interfaces
; i
++) {
515 if (port
->interfaces
[i
] != iface
) {
516 ifaces
[n
++] = port
->interfaces
[i
];
519 ovsrec_port_set_interfaces(port
, ifaces
, n
);
524 /* Find and return a port within 'br' named 'port_name'. */
525 static const struct ovsrec_port
*
526 find_port(const struct ovsrec_bridge
*br
, const char *port_name
)
530 for (i
= 0; i
< br
->n_ports
; i
++) {
531 struct ovsrec_port
*port
= br
->ports
[i
];
532 if (!strcmp(port_name
, port
->name
)) {
539 /* Find and return an interface within 'br' named 'iface_name'. */
540 static const struct ovsrec_interface
*
541 find_interface(const struct ovsrec_bridge
*br
, const char *iface_name
,
542 struct ovsrec_port
**portp
)
546 for (i
= 0; i
< br
->n_ports
; i
++) {
547 struct ovsrec_port
*port
= br
->ports
[i
];
550 for (j
= 0; j
< port
->n_interfaces
; j
++) {
551 struct ovsrec_interface
*iface
= port
->interfaces
[j
];
552 if (!strcmp(iface
->name
, iface_name
)) {
564 del_bridge(struct ovsdb_idl
*idl
,
565 const struct ovsrec_open_vswitch
*ovs
, const char *br_name
)
567 struct ovsrec_bridge
*br
= find_bridge(ovs
, br_name
);
568 struct ovsrec_bridge
**bridges
;
569 struct ovsdb_idl_txn
*txn
;
573 VLOG_WARN("delbr %s: no bridge named %s", br_name
, br_name
);
577 txn
= ovsdb_idl_txn_create(idl
);
579 ovsdb_idl_txn_add_comment(txn
, "ovs-brcompatd: delbr %s", br_name
);
581 /* Remove 'br' from the vswitch's list of bridges. */
582 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
583 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
584 if (ovs
->bridges
[i
] != br
) {
585 bridges
[n
++] = ovs
->bridges
[i
];
588 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
591 return commit_txn(txn
, true);
595 parse_command(struct ofpbuf
*buffer
, uint32_t *seq
, const char **br_name
,
596 const char **port_name
, uint64_t *count
, uint64_t *skip
)
598 static const struct nl_policy policy
[] = {
599 [BRC_GENL_A_DP_NAME
] = { .type
= NL_A_STRING
, .optional
= true },
600 [BRC_GENL_A_PORT_NAME
] = { .type
= NL_A_STRING
, .optional
= true },
601 [BRC_GENL_A_FDB_COUNT
] = { .type
= NL_A_U64
, .optional
= true },
602 [BRC_GENL_A_FDB_SKIP
] = { .type
= NL_A_U64
, .optional
= true },
604 struct nlattr
*attrs
[ARRAY_SIZE(policy
)];
606 if (!nl_policy_parse(buffer
, NLMSG_HDRLEN
+ GENL_HDRLEN
, policy
,
607 attrs
, ARRAY_SIZE(policy
))
608 || (br_name
&& !attrs
[BRC_GENL_A_DP_NAME
])
609 || (port_name
&& !attrs
[BRC_GENL_A_PORT_NAME
])
610 || (count
&& !attrs
[BRC_GENL_A_FDB_COUNT
])
611 || (skip
&& !attrs
[BRC_GENL_A_FDB_SKIP
])) {
615 *seq
= ((struct nlmsghdr
*) buffer
->data
)->nlmsg_seq
;
617 *br_name
= nl_attr_get_string(attrs
[BRC_GENL_A_DP_NAME
]);
620 *port_name
= nl_attr_get_string(attrs
[BRC_GENL_A_PORT_NAME
]);
623 *count
= nl_attr_get_u64(attrs
[BRC_GENL_A_FDB_COUNT
]);
626 *skip
= nl_attr_get_u64(attrs
[BRC_GENL_A_FDB_SKIP
]);
631 /* Composes and returns a reply to a request made by the datapath with Netlink
632 * sequence number 'seq' and error code 'error'. The caller may add additional
633 * attributes to the message, then it may send it with send_reply(). */
634 static struct ofpbuf
*
635 compose_reply(uint32_t seq
, int error
)
637 struct ofpbuf
*reply
= ofpbuf_new(4096);
638 nl_msg_put_genlmsghdr(reply
, 32, brc_family
, NLM_F_REQUEST
,
639 BRC_GENL_C_DP_RESULT
, 1);
640 ((struct nlmsghdr
*) reply
->data
)->nlmsg_seq
= seq
;
641 nl_msg_put_u32(reply
, BRC_GENL_A_ERR_CODE
, error
);
645 /* Sends 'reply' to the datapath and frees it. */
647 send_reply(struct ofpbuf
*reply
)
649 int retval
= nl_sock_send(brc_sock
, reply
, false);
651 VLOG_WARN_RL(&rl
, "replying to brcompat request: %s",
654 ofpbuf_delete(reply
);
657 /* Composes and sends a reply to a request made by the datapath with Netlink
658 * sequence number 'seq' and error code 'error'. */
660 send_simple_reply(uint32_t seq
, int error
)
662 send_reply(compose_reply(seq
, error
));
666 handle_bridge_cmd(struct ovsdb_idl
*idl
,
667 const struct ovsrec_open_vswitch
*ovs
,
668 struct ofpbuf
*buffer
, bool add
)
674 error
= parse_command(buffer
, &seq
, &br_name
, NULL
, NULL
, NULL
);
679 retval
= (add
? add_bridge
: del_bridge
)(idl
, ovs
, br_name
);
680 VLOG_INFO_RL(&rl
, "%sbr %s: %s",
681 add
? "add" : "del", br_name
, strerror(retval
));
682 } while (retval
== EAGAIN
);
684 send_simple_reply(seq
, error
);
689 static const struct nl_policy brc_port_policy
[] = {
690 [BRC_GENL_A_DP_NAME
] = { .type
= NL_A_STRING
},
691 [BRC_GENL_A_PORT_NAME
] = { .type
= NL_A_STRING
},
695 handle_port_cmd(struct ovsdb_idl
*idl
,
696 const struct ovsrec_open_vswitch
*ovs
,
697 struct ofpbuf
*buffer
, bool add
)
699 const char *cmd_name
= add
? "add-if" : "del-if";
700 const char *br_name
, *port_name
;
704 error
= parse_command(buffer
, &seq
, &br_name
, &port_name
, NULL
, NULL
);
706 struct ovsrec_bridge
*br
= find_bridge(ovs
, br_name
);
709 VLOG_WARN("%s %s %s: no bridge named %s",
710 cmd_name
, br_name
, port_name
, br_name
);
712 } else if (!netdev_exists(port_name
)) {
713 VLOG_WARN("%s %s %s: no network device named %s",
714 cmd_name
, br_name
, port_name
, port_name
);
718 struct ovsdb_idl_txn
*txn
= ovsdb_idl_txn_create(idl
);
721 ovsdb_idl_txn_add_comment(txn
, "ovs-brcompatd: add-if %s",
723 add_port(ovs
, br
, port_name
);
725 const struct ovsrec_port
*port
= find_port(br
, port_name
);
727 ovsdb_idl_txn_add_comment(txn
,
728 "ovs-brcompatd: del-if %s",
734 error
= commit_txn(txn
, true);
735 VLOG_INFO_RL(&rl
, "%s %s %s: %s",
736 cmd_name
, br_name
, port_name
, strerror(error
));
737 } while (error
== EAGAIN
);
739 send_simple_reply(seq
, error
);
745 /* The caller is responsible for freeing '*ovs_name' if the call is
748 linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch
*ovs
,
749 const char *linux_name
,
750 const struct ovsrec_bridge
**ovs_bridge
,
753 *ovs_bridge
= find_bridge(ovs
, linux_name
);
755 /* Bridge name is the same. We are interested in VLAN 0. */
759 /* No such Open vSwitch bridge 'linux_name', but there might be an
760 * internal port named 'linux_name' on some other bridge
761 * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
762 * port 'linux_name' on the bridge named 'ovs_bridge'. */
765 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
766 const struct ovsrec_bridge
*br
= ovs
->bridges
[i
];
768 for (j
= 0; j
< br
->n_ports
; j
++) {
769 const struct ovsrec_port
*port
= br
->ports
[j
];
771 if (!strcmp(port
->name
, linux_name
)) {
773 *br_vlan
= port
->n_tag
? *port
->tag
: -1;
784 handle_fdb_query_cmd(const struct ovsrec_open_vswitch
*ovs
,
785 struct ofpbuf
*buffer
)
787 /* This structure is copied directly from the Linux 2.6.30 header files.
788 * It would be more straightforward to #include <linux/if_bridge.h>, but
789 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
790 * with old header files won't have it. */
795 __u32 ageing_timer_value
;
804 struct mac
*local_macs
;
808 /* Impedance matching between the vswitchd and Linux kernel notions of what
809 * a bridge is. The kernel only handles a single VLAN per bridge, but
810 * vswitchd can deal with all the VLANs on a single bridge. We have to
811 * pretend that the former is the case even though the latter is the
813 const char *linux_name
; /* Name used by brctl. */
814 const struct ovsrec_bridge
*ovs_bridge
; /* Bridge used by ovs-vswitchd. */
815 int br_vlan
; /* VLAN tag. */
818 struct ofpbuf query_data
;
819 struct ofpbuf
*reply
;
820 char *unixctl_command
;
821 uint64_t count
, skip
;
827 /* Parse the command received from brcompat_mod. */
828 error
= parse_command(buffer
, &seq
, &linux_name
, NULL
, &count
, &skip
);
833 /* Figure out vswitchd bridge and VLAN. */
834 error
= linux_bridge_to_ovs_bridge(ovs
, linux_name
,
835 &ovs_bridge
, &br_vlan
);
837 send_simple_reply(seq
, error
);
841 /* Fetch the forwarding database using ovs-appctl. */
842 unixctl_command
= xasprintf("fdb/show %s", ovs_bridge
->name
);
843 error
= execute_appctl_command(unixctl_command
, &output
);
844 free(unixctl_command
);
846 send_simple_reply(seq
, error
);
850 /* Fetch the MAC address for each interface on the bridge, so that we can
851 * fill in the is_local field in the response. */
853 get_bridge_ifaces(ovs_bridge
, &ifaces
, br_vlan
);
854 local_macs
= xmalloc(ifaces
.n
* sizeof *local_macs
);
856 for (i
= 0; i
< ifaces
.n
; i
++) {
857 const char *iface_name
= ifaces
.names
[i
];
858 struct mac
*mac
= &local_macs
[n_local_macs
];
859 struct netdev
*netdev
;
861 error
= netdev_open_default(iface_name
, &netdev
);
863 if (!netdev_get_etheraddr(netdev
, mac
->addr
)) {
866 netdev_close(netdev
);
869 svec_destroy(&ifaces
);
871 /* Parse the response from ovs-appctl and convert it to binary format to
872 * pass back to the kernel. */
873 ofpbuf_init(&query_data
, sizeof(struct __fdb_entry
) * 8);
875 strtok_r(output
, "\n", &save_ptr
); /* Skip header line. */
877 struct __fdb_entry
*entry
;
879 uint8_t mac
[ETH_ADDR_LEN
];
883 line
= strtok_r(NULL
, "\n", &save_ptr
);
888 if (sscanf(line
, "%d %d "ETH_ADDR_SCAN_FMT
" %d",
889 &port
, &vlan
, ETH_ADDR_SCAN_ARGS(mac
), &age
)
890 != 2 + ETH_ADDR_SCAN_COUNT
+ 1) {
891 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
892 VLOG_INFO_RL(&rl
, "fdb/show output has invalid format: %s", line
);
896 if (vlan
!= br_vlan
) {
905 /* Is this the MAC address of an interface on the bridge? */
907 for (i
= 0; i
< n_local_macs
; i
++) {
908 if (eth_addr_equals(local_macs
[i
].addr
, mac
)) {
914 entry
= ofpbuf_put_uninit(&query_data
, sizeof *entry
);
915 memcpy(entry
->mac_addr
, mac
, ETH_ADDR_LEN
);
916 entry
->port_no
= port
& 0xff;
917 entry
->is_local
= is_local
;
918 entry
->ageing_timer_value
= age
* HZ
;
919 entry
->port_hi
= (port
& 0xff00) >> 8;
926 /* Compose and send reply to datapath. */
927 reply
= compose_reply(seq
, 0);
928 nl_msg_put_unspec(reply
, BRC_GENL_A_FDB_DATA
,
929 query_data
.data
, query_data
.size
);
933 ofpbuf_uninit(&query_data
);
940 send_ifindex_reply(uint32_t seq
, struct svec
*ifaces
)
942 struct ofpbuf
*reply
;
948 /* Make sure that any given interface only occurs once. This shouldn't
949 * happen, but who knows what people put into their configuration files. */
950 svec_sort_unique(ifaces
);
952 /* Convert 'ifaces' into ifindexes. */
954 indices
= xmalloc(ifaces
->n
* sizeof *indices
);
955 SVEC_FOR_EACH (i
, iface
, ifaces
) {
956 int ifindex
= if_nametoindex(iface
);
958 indices
[n_indices
++] = ifindex
;
962 /* Compose and send reply. */
963 reply
= compose_reply(seq
, 0);
964 nl_msg_put_unspec(reply
, BRC_GENL_A_IFINDEXES
,
965 indices
, n_indices
* sizeof *indices
);
973 handle_get_bridges_cmd(const struct ovsrec_open_vswitch
*ovs
,
974 struct ofpbuf
*buffer
)
983 /* Parse Netlink command.
985 * The command doesn't actually have any arguments, but we need the
986 * sequence number to send the reply. */
987 error
= parse_command(buffer
, &seq
, NULL
, NULL
, NULL
, NULL
);
992 /* Get all the real bridges and all the fake ones. */
994 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
995 const struct ovsrec_bridge
*br
= ovs
->bridges
[i
];
997 svec_add(&bridges
, br
->name
);
998 for (j
= 0; j
< br
->n_ports
; j
++) {
999 const struct ovsrec_port
*port
= br
->ports
[j
];
1001 if (port
->fake_bridge
) {
1002 svec_add(&bridges
, port
->name
);
1007 send_ifindex_reply(seq
, &bridges
);
1008 svec_destroy(&bridges
);
1014 handle_get_ports_cmd(const struct ovsrec_open_vswitch
*ovs
,
1015 struct ofpbuf
*buffer
)
1019 const char *linux_name
;
1020 const struct ovsrec_bridge
*ovs_bridge
;
1027 /* Parse Netlink command. */
1028 error
= parse_command(buffer
, &seq
, &linux_name
, NULL
, NULL
, NULL
);
1033 error
= linux_bridge_to_ovs_bridge(ovs
, linux_name
,
1034 &ovs_bridge
, &br_vlan
);
1036 send_simple_reply(seq
, error
);
1041 get_bridge_ports(ovs_bridge
, &ports
, br_vlan
);
1043 svec_del(&ports
, linux_name
);
1044 send_ifindex_reply(seq
, &ports
); /* XXX bonds won't show up */
1045 svec_destroy(&ports
);
1050 static struct ofpbuf
*
1051 brc_recv_update__(void)
1054 struct ofpbuf
*buffer
;
1057 retval
= nl_sock_recv(brc_sock
, &buffer
, false);
1060 if (nl_msg_nlmsgerr(buffer
, NULL
)
1061 || nl_msg_nlmsghdr(buffer
)->nlmsg_type
== NLMSG_DONE
) {
1073 VLOG_WARN_RL(&rl
, "brc_recv_update: %s", strerror(retval
));
1076 ofpbuf_delete(buffer
);
1081 brc_recv_update(struct ovsdb_idl
*idl
)
1083 struct ofpbuf
*buffer
;
1084 struct genlmsghdr
*genlmsghdr
;
1085 const struct ovsrec_open_vswitch
*ovs
;
1087 buffer
= brc_recv_update__();
1092 genlmsghdr
= nl_msg_genlmsghdr(buffer
);
1094 VLOG_WARN_RL(&rl
, "received packet too short for generic NetLink");
1098 if (nl_msg_nlmsghdr(buffer
)->nlmsg_type
!= brc_family
) {
1099 VLOG_DBG_RL(&rl
, "received type (%"PRIu16
") != brcompat family (%d)",
1100 nl_msg_nlmsghdr(buffer
)->nlmsg_type
, brc_family
);
1104 /* Get the Open vSwitch configuration. Just drop the request on the floor
1105 * if a valid configuration doesn't exist. (We could check this earlier,
1106 * but we want to drain pending Netlink messages even when there is no Open
1107 * vSwitch configuration.) */
1108 ovs
= ovsrec_open_vswitch_first(idl
);
1110 VLOG_WARN_RL(&rl
, "could not find valid configuration to update");
1114 switch (genlmsghdr
->cmd
) {
1115 case BRC_GENL_C_DP_ADD
:
1116 handle_bridge_cmd(idl
, ovs
, buffer
, true);
1119 case BRC_GENL_C_DP_DEL
:
1120 handle_bridge_cmd(idl
, ovs
, buffer
, false);
1123 case BRC_GENL_C_PORT_ADD
:
1124 handle_port_cmd(idl
, ovs
, buffer
, true);
1127 case BRC_GENL_C_PORT_DEL
:
1128 handle_port_cmd(idl
, ovs
, buffer
, false);
1131 case BRC_GENL_C_FDB_QUERY
:
1132 handle_fdb_query_cmd(ovs
, buffer
);
1135 case BRC_GENL_C_GET_BRIDGES
:
1136 handle_get_bridges_cmd(ovs
, buffer
);
1139 case BRC_GENL_C_GET_PORTS
:
1140 handle_get_ports_cmd(ovs
, buffer
);
1144 VLOG_WARN_RL(&rl
, "received unknown brc netlink command: %d\n",
1150 ofpbuf_delete(buffer
);
1154 /* Check for interface configuration changes announced through RTNL. */
1156 rtnl_recv_update(struct ovsdb_idl
*idl
,
1157 const struct ovsrec_open_vswitch
*ovs
)
1161 int error
= nl_sock_recv(rtnl_sock
, &buf
, false);
1162 if (error
== EAGAIN
) {
1163 /* Nothing to do. */
1164 } else if (error
== ENOBUFS
) {
1165 VLOG_WARN_RL(&rl
, "network monitor socket overflowed");
1167 VLOG_WARN_RL(&rl
, "error on network monitor socket: %s",
1170 struct nlattr
*attrs
[ARRAY_SIZE(rtnlgrp_link_policy
)];
1171 struct nlmsghdr
*nlh
;
1172 struct ifinfomsg
*iim
;
1174 nlh
= ofpbuf_at(buf
, 0, NLMSG_HDRLEN
);
1175 iim
= ofpbuf_at(buf
, NLMSG_HDRLEN
, sizeof *iim
);
1177 VLOG_WARN_RL(&rl
, "received bad rtnl message (no ifinfomsg)");
1182 if (!nl_policy_parse(buf
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
),
1183 rtnlgrp_link_policy
,
1184 attrs
, ARRAY_SIZE(rtnlgrp_link_policy
))) {
1185 VLOG_WARN_RL(&rl
,"received bad rtnl message (policy)");
1189 if (nlh
->nlmsg_type
== RTM_DELLINK
&& attrs
[IFLA_MASTER
]) {
1190 const char *port_name
= nl_attr_get_string(attrs
[IFLA_IFNAME
]);
1191 char br_name
[IFNAMSIZ
];
1192 uint32_t br_idx
= nl_attr_get_u32(attrs
[IFLA_MASTER
]);
1194 if (!if_indextoname(br_idx
, br_name
)) {
1199 if (!netdev_exists(port_name
)) {
1200 /* Network device is really gone. */
1201 struct ovsdb_idl_txn
*txn
;
1202 const struct ovsrec_interface
*iface
;
1203 struct ovsrec_port
*port
;
1204 struct ovsrec_bridge
*br
;
1206 VLOG_INFO("network device %s destroyed, "
1207 "removing from bridge %s", port_name
, br_name
);
1209 br
= find_bridge(ovs
, br_name
);
1211 VLOG_WARN("no bridge named %s from which to remove %s",
1212 br_name
, port_name
);
1217 txn
= ovsdb_idl_txn_create(idl
);
1219 iface
= find_interface(br
, port_name
, &port
);
1221 del_interface(br
, port
, iface
);
1222 ovsdb_idl_txn_add_comment(txn
,
1223 "ovs-brcompatd: destroy port %s",
1227 commit_txn(txn
, false);
1229 /* A network device by that name exists even though the kernel
1230 * told us it had disappeared. Probably, what happened was
1233 * 1. Device destroyed.
1234 * 2. Notification sent to us.
1235 * 3. New device created with same name as old one.
1236 * 4. ovs-brcompatd notified, removes device from bridge.
1238 * There's no a priori reason that in this situation that the
1239 * new device with the same name should remain in the bridge;
1240 * on the contrary, that would be unexpected. *But* there is
1241 * one important situation where, if we do this, bad things
1242 * happen. This is the case of XenServer Tools version 5.0.0,
1243 * which on boot of a Windows VM cause something like this to
1244 * happen on the Xen host:
1246 * i. Create tap1.0 and vif1.0.
1247 * ii. Delete tap1.0.
1248 * iii. Delete vif1.0.
1249 * iv. Re-create vif1.0.
1251 * (XenServer Tools 5.5.0 does not exhibit this behavior, and
1252 * neither does a VM without Tools installed at all.@.)
1254 * Steps iii and iv happen within a few seconds of each other.
1255 * Step iv causes /etc/xensource/scripts/vif to run, which in
1256 * turn calls ovs-cfg-mod to add the new device to the bridge.
1257 * If step iv happens after step 4 (in our first list of
1258 * steps), then all is well, but if it happens between 3 and 4
1259 * (which can easily happen if ovs-brcompatd has to wait to
1260 * lock the configuration file), then we will remove the new
1261 * incarnation from the bridge instead of the old one!
1263 * So, to avoid this problem, we do nothing here. This is
1264 * strictly incorrect except for this one particular case, and
1265 * perhaps that will bite us someday. If that happens, then we
1266 * will have to somehow track network devices by ifindex, since
1267 * a new device will have a new ifindex even if it has the same
1268 * name as an old device.
1270 VLOG_INFO("kernel reported network device %s removed but "
1271 "a device by that name exists (XS Tools 5.0.0?)",
1280 main(int argc
, char *argv
[])
1282 extern struct vlog_module VLM_reconnect
;
1283 struct unixctl_server
*unixctl
;
1285 struct ovsdb_idl
*idl
;
1288 proctitle_init(argc
, argv
);
1289 set_program_name(argv
[0]);
1290 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
1291 vlog_set_levels(&VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
1293 remote
= parse_options(argc
, argv
);
1294 signal(SIGPIPE
, SIG_IGN
);
1298 die_if_already_running();
1301 retval
= unixctl_server_create(NULL
, &unixctl
);
1306 if (brc_open(&brc_sock
)) {
1307 ovs_fatal(0, "could not open brcompat socket. Check "
1308 "\"brcompat\" kernel module.");
1311 if (prune_timeout
) {
1314 error
= nl_sock_create(NETLINK_ROUTE
, &rtnl_sock
);
1316 ovs_fatal(error
, "could not create rtnetlink socket");
1319 error
= nl_sock_join_mcgroup(rtnl_sock
, RTNLGRP_LINK
);
1321 ovs_fatal(error
, "could not join RTNLGRP_LINK multicast group");
1325 daemonize_complete();
1327 idl
= ovsdb_idl_create(remote
, &ovsrec_idl_class
, true);
1330 const struct ovsrec_open_vswitch
*ovs
;
1334 unixctl_server_run(unixctl
);
1335 brc_recv_update(idl
);
1337 ovs
= ovsrec_open_vswitch_first(idl
);
1338 if (!ovs
&& ovsdb_idl_has_ever_connected(idl
)) {
1339 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
1340 VLOG_WARN_RL(&rl
, "%s: database does not contain any Open vSwitch "
1341 "configuration", remote
);
1345 /* If 'prune_timeout' is non-zero, we actively prune from the
1346 * configuration of port entries that are no longer valid. We
1349 * 1) The kernel explicitly notifies us of removed ports
1350 * through the RTNL messages.
1352 * 2) We periodically check all ports associated with bridges
1353 * to see if they no longer exist.
1355 if (ovs
&& prune_timeout
) {
1356 rtnl_recv_update(idl
, ovs
);
1357 nl_sock_wait(rtnl_sock
, POLLIN
);
1358 poll_timer_wait(prune_timeout
);
1362 nl_sock_wait(brc_sock
, POLLIN
);
1363 ovsdb_idl_wait(idl
);
1364 unixctl_server_wait(unixctl
);
1369 ovsdb_idl_destroy(idl
);
1375 validate_appctl_command(void)
1381 for (p
= strchr(appctl_command
, '%'); p
; p
= strchr(p
+ 2, '%')) {
1383 /* Nothing to do. */
1384 } else if (p
[1] == 's') {
1387 ovs_fatal(0, "only '%%s' and '%%%%' allowed in --appctl-command");
1391 ovs_fatal(0, "'%%s' must appear exactly once in --appctl-command");
1396 parse_options(int argc
, char *argv
[])
1402 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
);