cea7fda5645e6396961b16bfb428fdca8b0115be
[openvswitch] / vswitchd / ovs-brcompatd.c
1 /* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include <asm/param.h>
19 #include <assert.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <inttypes.h>
23 #include <limits.h>
24 #include <net/if.h>
25 #include <linux/genetlink.h>
26 #include <linux/rtnetlink.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <time.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #include "command-line.h"
37 #include "coverage.h"
38 #include "daemon.h"
39 #include "dirs.h"
40 #include "dynamic-string.h"
41 #include "fatal-signal.h"
42 #include "json.h"
43 #include "leak-checker.h"
44 #include "netdev.h"
45 #include "netlink.h"
46 #include "netlink-socket.h"
47 #include "ofpbuf.h"
48 #include "openvswitch/brcompat-netlink.h"
49 #include "ovsdb-idl.h"
50 #include "packets.h"
51 #include "poll-loop.h"
52 #include "process.h"
53 #include "rtnetlink.h"
54 #include "rtnetlink-link.h"
55 #include "signals.h"
56 #include "sset.h"
57 #include "timeval.h"
58 #include "unixctl.h"
59 #include "util.h"
60 #include "vlog.h"
61 #include "vswitchd/vswitch-idl.h"
62
63 VLOG_DEFINE_THIS_MODULE(brcompatd);
64
65
66 /* xxx Just hangs if datapath is rmmod/insmod.  Learn to reconnect? */
67
68 /* Actions to modify bridge compatibility configuration. */
69 enum bmc_action {
70     BMC_ADD_DP,
71     BMC_DEL_DP,
72     BMC_ADD_PORT,
73     BMC_DEL_PORT
74 };
75
76 static const char *parse_options(int argc, char *argv[]);
77 static void usage(void) NO_RETURN;
78
79 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
80
81 /* Maximum number of milliseconds to wait before pruning port entries that
82  * no longer exist.  If set to zero, ports are never pruned. */
83 static int prune_timeout = 5000;
84
85 /* Shell command to execute (via popen()) to send a control command to the
86  * running ovs-vswitchd process.  The string must contain one instance of %s,
87  * which is replaced by the control command. */
88 static char *appctl_command;
89
90 /* Netlink socket to bridge compatibility kernel module. */
91 static struct nl_sock *brc_sock;
92
93 /* The Generic Netlink family number used for bridge compatibility. */
94 static int brc_family;
95
96 static const struct nl_policy brc_multicast_policy[] = {
97     [BRC_GENL_A_MC_GROUP] = {.type = NL_A_U32 }
98 };
99
100 static int
101 lookup_brc_multicast_group(int *multicast_group)
102 {
103     struct nl_sock *sock;
104     struct ofpbuf request, *reply;
105     struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
106     int retval;
107
108     retval = nl_sock_create(NETLINK_GENERIC, &sock);
109     if (retval) {
110         return retval;
111     }
112     ofpbuf_init(&request, 0);
113     nl_msg_put_genlmsghdr(&request, 0, brc_family,
114             NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
115     retval = nl_sock_transact(sock, &request, &reply);
116     ofpbuf_uninit(&request);
117     if (retval) {
118         nl_sock_destroy(sock);
119         return retval;
120     }
121     if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
122                          brc_multicast_policy, attrs,
123                          ARRAY_SIZE(brc_multicast_policy))) {
124         nl_sock_destroy(sock);
125         ofpbuf_delete(reply);
126         return EPROTO;
127     }
128     *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
129     nl_sock_destroy(sock);
130     ofpbuf_delete(reply);
131
132     return 0;
133 }
134
135 /* Opens a socket for brcompat notifications.  Returns 0 if successful,
136  * otherwise a positive errno value. */
137 static int
138 brc_open(struct nl_sock **sock)
139 {
140     int multicast_group = 0;
141     int retval;
142
143     retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
144     if (retval) {
145         return retval;
146     }
147
148     retval = lookup_brc_multicast_group(&multicast_group);
149     if (retval) {
150         return retval;
151     }
152
153     retval = nl_sock_create(NETLINK_GENERIC, sock);
154     if (retval) {
155         return retval;
156     }
157
158     retval = nl_sock_join_mcgroup(*sock, multicast_group);
159     if (retval) {
160         nl_sock_destroy(*sock);
161         *sock = NULL;
162     }
163     return retval;
164 }
165
166 static const struct nl_policy brc_dp_policy[] = {
167     [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
168 };
169
170 static struct ovsrec_bridge *
171 find_bridge(const struct ovsrec_open_vswitch *ovs, const char *br_name)
172 {
173     size_t i;
174
175     for (i = 0; i < ovs->n_bridges; i++) {
176         if (!strcmp(br_name, ovs->bridges[i]->name)) {
177             return ovs->bridges[i];
178         }
179     }
180
181     return NULL;
182 }
183
184 static int
185 execute_appctl_command(const char *unixctl_command, char **output)
186 {
187     char *stdout_log, *stderr_log;
188     int error, status;
189     char *argv[5];
190
191     argv[0] = "/bin/sh";
192     argv[1] = "-c";
193     argv[2] = xasprintf(appctl_command, unixctl_command);
194     argv[3] = NULL;
195
196     /* Run process and log status. */
197     error = process_run_capture(argv, &stdout_log, &stderr_log, 65536,
198                                 &status);
199     if (error) {
200         VLOG_ERR("failed to execute %s command via ovs-appctl: %s",
201                  unixctl_command, strerror(error));
202     } else if (status) {
203         char *msg = process_status_msg(status);
204         VLOG_ERR("ovs-appctl exited with error (%s)", msg);
205         free(msg);
206         error = ECHILD;
207     }
208
209     /* Deal with stdout_log. */
210     if (output) {
211         *output = stdout_log;
212     } else {
213         free(stdout_log);
214     }
215
216     /* Deal with stderr_log */
217     if (stderr_log && *stderr_log) {
218         VLOG_INFO("ovs-appctl wrote to stderr:\n%s", stderr_log);
219     }
220     free(stderr_log);
221
222     free(argv[2]);
223
224     return error;
225 }
226
227 static void
228 do_get_bridge_parts(const struct ovsrec_bridge *br, struct sset *parts,
229                     int vlan, bool break_down_bonds)
230 {
231     size_t i, j;
232
233     for (i = 0; i < br->n_ports; i++) {
234         const struct ovsrec_port *port = br->ports[i];
235
236         if (vlan >= 0) {
237             int port_vlan = port->n_tag ? *port->tag : 0;
238             if (vlan != port_vlan) {
239                 continue;
240             }
241         }
242         if (break_down_bonds) {
243             for (j = 0; j < port->n_interfaces; j++) {
244                 const struct ovsrec_interface *iface = port->interfaces[j];
245                 sset_add(parts, iface->name);
246             }
247         } else {
248             sset_add(parts, port->name);
249         }
250     }
251 }
252
253 /* Add all the interfaces for 'bridge' to 'ifaces', breaking bonded interfaces
254  * down into their constituent parts.
255  *
256  * If 'vlan' < 0, all interfaces on 'bridge' are reported.  If 'vlan' == 0,
257  * then only interfaces for trunk ports or ports with implicit VLAN 0 are
258  * reported.  If 'vlan' > 0, only interfaces with implicit VLAN 'vlan' are
259  * reported.  */
260 static void
261 get_bridge_ifaces(const struct ovsrec_bridge *br, struct sset *ifaces,
262                   int vlan)
263 {
264     do_get_bridge_parts(br, ifaces, vlan, true);
265 }
266
267 /* Add all the ports for 'bridge' to 'ports'.  Bonded ports are reported under
268  * the bond name, not broken down into their constituent interfaces.
269  *
270  * If 'vlan' < 0, all ports on 'bridge' are reported.  If 'vlan' == 0, then
271  * only trunk ports or ports with implicit VLAN 0 are reported.  If 'vlan' > 0,
272  * only port with implicit VLAN 'vlan' are reported.  */
273 static void
274 get_bridge_ports(const struct ovsrec_bridge *br, struct sset *ports,
275                  int vlan)
276 {
277     do_get_bridge_parts(br, ports, vlan, false);
278 }
279
280 static struct ovsdb_idl_txn *
281 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
282 {
283     return ovsdb_idl_txn_get(&ovs->header_);
284 }
285
286 static bool
287 port_is_fake_bridge(const struct ovsrec_port *port)
288 {
289     return (port->fake_bridge
290             && port->tag
291             && *port->tag >= 1 && *port->tag <= 4095);
292 }
293
294 static void
295 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
296                   struct ovsrec_bridge *bridge)
297 {
298     struct ovsrec_bridge **bridges;
299     size_t i;
300
301     bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
302     for (i = 0; i < ovs->n_bridges; i++) {
303         bridges[i] = ovs->bridges[i];
304     }
305     bridges[ovs->n_bridges] = bridge;
306     ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
307     free(bridges);
308 }
309
310 static struct json *
311 where_uuid_equals(const struct uuid *uuid)
312 {
313     return
314         json_array_create_1(
315             json_array_create_3(
316                 json_string_create("_uuid"),
317                 json_string_create("=="),
318                 json_array_create_2(
319                     json_string_create("uuid"),
320                     json_string_create_nocopy(
321                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
322 }
323
324 /* Commits 'txn'.  If 'wait_for_reload' is true, also waits for Open vSwitch to
325    reload the configuration before returning.
326
327    Returns EAGAIN if the caller should try the operation again, 0 on success,
328    otherwise a positive errno value. */
329 static int
330 commit_txn(struct ovsdb_idl_txn *txn, bool wait_for_reload)
331 {
332     struct ovsdb_idl *idl = ovsdb_idl_txn_get_idl (txn);
333     enum ovsdb_idl_txn_status status;
334     int64_t next_cfg = 0;
335
336     if (wait_for_reload) {
337         const struct ovsrec_open_vswitch *ovs = ovsrec_open_vswitch_first(idl);
338         struct json *where = where_uuid_equals(&ovs->header_.uuid);
339         ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
340         json_destroy(where);
341     }
342     status = ovsdb_idl_txn_commit_block(txn);
343     if (wait_for_reload && status == TXN_SUCCESS) {
344         next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
345     }
346     ovsdb_idl_txn_destroy(txn);
347
348     switch (status) {
349     case TXN_INCOMPLETE:
350         NOT_REACHED();
351
352     case TXN_ABORTED:
353         VLOG_ERR_RL(&rl, "OVSDB transaction unexpectedly aborted");
354         return ECONNABORTED;
355
356     case TXN_UNCHANGED:
357         return 0;
358
359     case TXN_SUCCESS:
360         if (wait_for_reload) {
361             for (;;) {
362                 /* We can't use 'ovs' any longer because ovsdb_idl_run() can
363                  * destroy it. */
364                 const struct ovsrec_open_vswitch *ovs2;
365
366                 ovsdb_idl_run(idl);
367                 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs2, idl) {
368                     if (ovs2->cur_cfg >= next_cfg) {
369                         goto done;
370                     }
371                 }
372                 ovsdb_idl_wait(idl);
373                 poll_block();
374             }
375         done: ;
376         }
377         return 0;
378
379     case TXN_TRY_AGAIN:
380         VLOG_ERR_RL(&rl, "OVSDB transaction needs retry");
381         return EAGAIN;
382
383     case TXN_ERROR:
384         VLOG_ERR_RL(&rl, "OVSDB transaction failed: %s",
385                     ovsdb_idl_txn_get_error(txn));
386         return EBUSY;
387
388     default:
389         NOT_REACHED();
390     }
391 }
392
393 static int
394 add_bridge(struct ovsdb_idl *idl, const struct ovsrec_open_vswitch *ovs,
395            const char *br_name)
396 {
397     struct ovsrec_bridge *br;
398     struct ovsrec_port *port;
399     struct ovsrec_interface *iface;
400     struct ovsdb_idl_txn *txn;
401
402     if (find_bridge(ovs, br_name)) {
403         VLOG_WARN("addbr %s: bridge %s exists", br_name, br_name);
404         return EEXIST;
405     } else if (netdev_exists(br_name)) {
406         size_t i;
407
408         for (i = 0; i < ovs->n_bridges; i++) {
409             size_t j;
410             struct ovsrec_bridge *br_cfg = ovs->bridges[i];
411
412             for (j = 0; j < br_cfg->n_ports; j++) {
413                 if (port_is_fake_bridge(br_cfg->ports[j])) {
414                     VLOG_WARN("addbr %s: %s exists as a fake bridge",
415                               br_name, br_name);
416                     return 0;
417                 }
418             }
419         }
420
421         VLOG_WARN("addbr %s: cannot create bridge %s because a network "
422                   "device named %s already exists",
423                   br_name, br_name, br_name);
424         return EEXIST;
425     }
426
427     txn = ovsdb_idl_txn_create(idl);
428
429     ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: addbr %s", br_name);
430
431     iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
432     ovsrec_interface_set_name(iface, br_name);
433
434     port = ovsrec_port_insert(txn_from_openvswitch(ovs));
435     ovsrec_port_set_name(port, br_name);
436     ovsrec_port_set_interfaces(port, &iface, 1);
437
438     br = ovsrec_bridge_insert(txn_from_openvswitch(ovs));
439     ovsrec_bridge_set_name(br, br_name);
440     ovsrec_bridge_set_ports(br, &port, 1);
441
442     ovs_insert_bridge(ovs, br);
443
444     return commit_txn(txn, true);
445 }
446
447 static void
448 add_port(const struct ovsrec_open_vswitch *ovs,
449          const struct ovsrec_bridge *br, const char *port_name)
450 {
451     struct ovsrec_interface *iface;
452     struct ovsrec_port *port;
453     struct ovsrec_port **ports;
454     size_t i;
455
456     /* xxx Check conflicts? */
457     iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
458     ovsrec_interface_set_name(iface, port_name);
459
460     port = ovsrec_port_insert(txn_from_openvswitch(ovs));
461     ovsrec_port_set_name(port, port_name);
462     ovsrec_port_set_interfaces(port, &iface, 1);
463
464     ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
465     for (i = 0; i < br->n_ports; i++) {
466         ports[i] = br->ports[i];
467     }
468     ports[br->n_ports] = port;
469     ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
470     free(ports);
471 }
472
473 /* Deletes 'port' from 'br'.
474  *
475  * After calling this function, 'port' must not be referenced again. */
476 static void
477 del_port(const struct ovsrec_bridge *br, const struct ovsrec_port *port)
478 {
479     struct ovsrec_port **ports;
480     size_t i, n;
481
482     /* Remove 'port' from the bridge's list of ports. */
483     ports = xmalloc(sizeof *br->ports * br->n_ports);
484     for (i = n = 0; i < br->n_ports; i++) {
485         if (br->ports[i] != port) {
486             ports[n++] = br->ports[i];
487         }
488     }
489     ovsrec_bridge_set_ports(br, ports, n);
490     free(ports);
491 }
492
493 /* Delete 'iface' from 'port' (which must be within 'br').  If 'iface' was
494  * 'port''s only interface, delete 'port' from 'br' also.
495  *
496  * After calling this function, 'iface' must not be referenced again. */
497 static void
498 del_interface(const struct ovsrec_bridge *br,
499               const struct ovsrec_port *port,
500               const struct ovsrec_interface *iface)
501 {
502     if (port->n_interfaces == 1) {
503         del_port(br, port);
504     } else {
505         struct ovsrec_interface **ifaces;
506         size_t i, n;
507
508         ifaces = xmalloc(sizeof *port->interfaces * port->n_interfaces);
509         for (i = n = 0; i < port->n_interfaces; i++) {
510             if (port->interfaces[i] != iface) {
511                 ifaces[n++] = port->interfaces[i];
512             }
513         }
514         ovsrec_port_set_interfaces(port, ifaces, n);
515         free(ifaces);
516     }
517 }
518
519 /* Find and return a port within 'br' named 'port_name'. */
520 static const struct ovsrec_port *
521 find_port(const struct ovsrec_bridge *br, const char *port_name)
522 {
523     size_t i;
524
525     for (i = 0; i < br->n_ports; i++) {
526         struct ovsrec_port *port = br->ports[i];
527         if (!strcmp(port_name, port->name)) {
528             return port;
529         }
530     }
531     return NULL;
532 }
533
534 /* Find and return an interface within 'br' named 'iface_name'. */
535 static const struct ovsrec_interface *
536 find_interface(const struct ovsrec_bridge *br, const char *iface_name,
537                struct ovsrec_port **portp)
538 {
539     size_t i;
540
541     for (i = 0; i < br->n_ports; i++) {
542         struct ovsrec_port *port = br->ports[i];
543         size_t j;
544
545         for (j = 0; j < port->n_interfaces; j++) {
546             struct ovsrec_interface *iface = port->interfaces[j];
547             if (!strcmp(iface->name, iface_name)) {
548                 *portp = port;
549                 return iface;
550             }
551         }
552     }
553
554     *portp = NULL;
555     return NULL;
556 }
557
558 static int
559 del_bridge(struct ovsdb_idl *idl,
560            const struct ovsrec_open_vswitch *ovs, const char *br_name)
561 {
562     struct ovsrec_bridge *br = find_bridge(ovs, br_name);
563     struct ovsrec_bridge **bridges;
564     struct ovsdb_idl_txn *txn;
565     size_t i, n;
566
567     if (!br) {
568         VLOG_WARN("delbr %s: no bridge named %s", br_name, br_name);
569         return ENXIO;
570     }
571
572     txn = ovsdb_idl_txn_create(idl);
573
574     ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: delbr %s", br_name);
575
576     /* Remove 'br' from the vswitch's list of bridges. */
577     bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
578     for (i = n = 0; i < ovs->n_bridges; i++) {
579         if (ovs->bridges[i] != br) {
580             bridges[n++] = ovs->bridges[i];
581         }
582     }
583     ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
584     free(bridges);
585
586     return commit_txn(txn, true);
587 }
588
589 static int
590 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
591               const char **port_name, uint64_t *count, uint64_t *skip)
592 {
593     static const struct nl_policy policy[] = {
594         [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING, .optional = true },
595         [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
596         [BRC_GENL_A_FDB_COUNT] = { .type = NL_A_U64, .optional = true },
597         [BRC_GENL_A_FDB_SKIP] = { .type = NL_A_U64, .optional = true },
598     };
599     struct nlattr *attrs[ARRAY_SIZE(policy)];
600
601     if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
602                          attrs, ARRAY_SIZE(policy))
603         || (br_name && !attrs[BRC_GENL_A_DP_NAME])
604         || (port_name && !attrs[BRC_GENL_A_PORT_NAME])
605         || (count && !attrs[BRC_GENL_A_FDB_COUNT])
606         || (skip && !attrs[BRC_GENL_A_FDB_SKIP])) {
607         return EINVAL;
608     }
609
610     *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
611     if (br_name) {
612         *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
613     }
614     if (port_name) {
615         *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
616     }
617     if (count) {
618         *count = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_COUNT]);
619     }
620     if (skip) {
621         *skip = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_SKIP]);
622     }
623     return 0;
624 }
625
626 /* Composes and returns a reply to a request made by the datapath with Netlink
627  * sequence number 'seq' and error code 'error'.  The caller may add additional
628  * attributes to the message, then it may send it with send_reply(). */
629 static struct ofpbuf *
630 compose_reply(uint32_t seq, int error)
631 {
632     struct ofpbuf *reply = ofpbuf_new(4096);
633     nl_msg_put_genlmsghdr(reply, 32, brc_family, NLM_F_REQUEST,
634                           BRC_GENL_C_DP_RESULT, 1);
635     ((struct nlmsghdr *) reply->data)->nlmsg_seq = seq;
636     nl_msg_put_u32(reply, BRC_GENL_A_ERR_CODE, error);
637     return reply;
638 }
639
640 /* Sends 'reply' to the datapath and frees it. */
641 static void
642 send_reply(struct ofpbuf *reply)
643 {
644     int retval = nl_sock_send(brc_sock, reply, false);
645     if (retval) {
646         VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
647                      strerror(retval));
648     }
649     ofpbuf_delete(reply);
650 }
651
652 /* Composes and sends a reply to a request made by the datapath with Netlink
653  * sequence number 'seq' and error code 'error'. */
654 static void
655 send_simple_reply(uint32_t seq, int error)
656 {
657     send_reply(compose_reply(seq, error));
658 }
659
660 static int
661 handle_bridge_cmd(struct ovsdb_idl *idl,
662                   const struct ovsrec_open_vswitch *ovs,
663                   struct ofpbuf *buffer, bool add)
664 {
665     const char *br_name;
666     uint32_t seq;
667     int error;
668
669     error = parse_command(buffer, &seq, &br_name, NULL, NULL, NULL);
670     if (!error) {
671         int retval;
672
673         do {
674             retval = (add ? add_bridge : del_bridge)(idl, ovs, br_name);
675             VLOG_INFO_RL(&rl, "%sbr %s: %s",
676                          add ? "add" : "del", br_name, strerror(retval));
677         } while (retval == EAGAIN);
678
679         send_simple_reply(seq, error);
680     }
681     return error;
682 }
683
684 static const struct nl_policy brc_port_policy[] = {
685     [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
686     [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
687 };
688
689 static int
690 handle_port_cmd(struct ovsdb_idl *idl,
691                 const struct ovsrec_open_vswitch *ovs,
692                 struct ofpbuf *buffer, bool add)
693 {
694     const char *cmd_name = add ? "add-if" : "del-if";
695     const char *br_name, *port_name;
696     uint32_t seq;
697     int error;
698
699     error = parse_command(buffer, &seq, &br_name, &port_name, NULL, NULL);
700     if (!error) {
701         struct ovsrec_bridge *br = find_bridge(ovs, br_name);
702
703         if (!br) {
704             VLOG_WARN("%s %s %s: no bridge named %s",
705                       cmd_name, br_name, port_name, br_name);
706             error = EINVAL;
707         } else if (!netdev_exists(port_name)) {
708             VLOG_WARN("%s %s %s: no network device named %s",
709                       cmd_name, br_name, port_name, port_name);
710             error = EINVAL;
711         } else {
712             do {
713                 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
714
715                 if (add) {
716                     ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: add-if %s",
717                                               port_name);
718                     add_port(ovs, br, port_name);
719                 } else {
720                     const struct ovsrec_port *port = find_port(br, port_name);
721                     if (port) {
722                         ovsdb_idl_txn_add_comment(txn,
723                                                   "ovs-brcompatd: del-if %s",
724                                                   port_name);
725                         del_port(br, port);
726                     }
727                 }
728
729                 error = commit_txn(txn, true);
730                 VLOG_INFO_RL(&rl, "%s %s %s: %s",
731                              cmd_name, br_name, port_name, strerror(error));
732             } while (error == EAGAIN);
733         }
734         send_simple_reply(seq, error);
735     }
736
737     return error;
738 }
739
740 /* The caller is responsible for freeing '*ovs_name' if the call is
741  * successful. */
742 static int
743 linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch *ovs,
744                            const char *linux_name,
745                            const struct ovsrec_bridge **ovs_bridge,
746                            int *br_vlan)
747 {
748     *ovs_bridge = find_bridge(ovs, linux_name);
749     if (*ovs_bridge) {
750         /* Bridge name is the same.  We are interested in VLAN 0. */
751         *br_vlan = 0;
752         return 0;
753     } else {
754         /* No such Open vSwitch bridge 'linux_name', but there might be an
755          * internal port named 'linux_name' on some other bridge
756          * 'ovs_bridge'.  If so then we are interested in the VLAN assigned to
757          * port 'linux_name' on the bridge named 'ovs_bridge'. */
758         size_t i, j;
759
760         for (i = 0; i < ovs->n_bridges; i++) {
761             const struct ovsrec_bridge *br = ovs->bridges[i];
762
763             for (j = 0; j < br->n_ports; j++) {
764                 const struct ovsrec_port *port = br->ports[j];
765
766                 if (!strcmp(port->name, linux_name)) {
767                     *ovs_bridge = br;
768                     *br_vlan = port->n_tag ? *port->tag : -1;
769                     return 0;
770                 }
771             }
772
773         }
774         return ENODEV;
775     }
776 }
777
778 static int
779 handle_fdb_query_cmd(const struct ovsrec_open_vswitch *ovs,
780                      struct ofpbuf *buffer)
781 {
782     /* This structure is copied directly from the Linux 2.6.30 header files.
783      * It would be more straightforward to #include <linux/if_bridge.h>, but
784      * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
785      * with old header files won't have it. */
786     struct __fdb_entry {
787         __u8 mac_addr[6];
788         __u8 port_no;
789         __u8 is_local;
790         __u32 ageing_timer_value;
791         __u8 port_hi;
792         __u8 pad0;
793         __u16 unused;
794     };
795
796     struct mac {
797         uint8_t addr[6];
798     };
799     struct mac *local_macs;
800     int n_local_macs;
801     int i;
802
803     /* Impedance matching between the vswitchd and Linux kernel notions of what
804      * a bridge is.  The kernel only handles a single VLAN per bridge, but
805      * vswitchd can deal with all the VLANs on a single bridge.  We have to
806      * pretend that the former is the case even though the latter is the
807      * implementation. */
808     const char *linux_name;   /* Name used by brctl. */
809     const struct ovsrec_bridge *ovs_bridge;  /* Bridge used by ovs-vswitchd. */
810     int br_vlan;                /* VLAN tag. */
811     struct sset ifaces;
812
813     struct ofpbuf query_data;
814     const char *iface_name;
815     struct ofpbuf *reply;
816     char *unixctl_command;
817     uint64_t count, skip;
818     char *output;
819     char *save_ptr;
820     uint32_t seq;
821     int error;
822
823     /* Parse the command received from brcompat_mod. */
824     error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
825     if (error) {
826         return error;
827     }
828
829     /* Figure out vswitchd bridge and VLAN. */
830     error = linux_bridge_to_ovs_bridge(ovs, linux_name,
831                                        &ovs_bridge, &br_vlan);
832     if (error) {
833         send_simple_reply(seq, error);
834         return error;
835     }
836
837     /* Fetch the forwarding database using ovs-appctl. */
838     unixctl_command = xasprintf("fdb/show %s", ovs_bridge->name);
839     error = execute_appctl_command(unixctl_command, &output);
840     free(unixctl_command);
841     if (error) {
842         send_simple_reply(seq, error);
843         return error;
844     }
845
846     /* Fetch the MAC address for each interface on the bridge, so that we can
847      * fill in the is_local field in the response. */
848     sset_init(&ifaces);
849     get_bridge_ifaces(ovs_bridge, &ifaces, br_vlan);
850     local_macs = xmalloc(sset_count(&ifaces) * sizeof *local_macs);
851     n_local_macs = 0;
852     SSET_FOR_EACH (iface_name, &ifaces) {
853         struct mac *mac = &local_macs[n_local_macs];
854         struct netdev *netdev;
855
856         error = netdev_open_default(iface_name, &netdev);
857         if (!error) {
858             if (!netdev_get_etheraddr(netdev, mac->addr)) {
859                 n_local_macs++;
860             }
861             netdev_close(netdev);
862         }
863     }
864     sset_destroy(&ifaces);
865
866     /* Parse the response from ovs-appctl and convert it to binary format to
867      * pass back to the kernel. */
868     ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
869     save_ptr = NULL;
870     strtok_r(output, "\n", &save_ptr); /* Skip header line. */
871     while (count > 0) {
872         struct __fdb_entry *entry;
873         int port, vlan, age;
874         uint8_t mac[ETH_ADDR_LEN];
875         char *line;
876         bool is_local;
877
878         line = strtok_r(NULL, "\n", &save_ptr);
879         if (!line) {
880             break;
881         }
882
883         if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
884                    &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
885             != 2 + ETH_ADDR_SCAN_COUNT + 1) {
886             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
887             VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
888             continue;
889         }
890
891         if (vlan != br_vlan) {
892             continue;
893         }
894
895         if (skip > 0) {
896             skip--;
897             continue;
898         }
899
900         /* Is this the MAC address of an interface on the bridge? */
901         is_local = false;
902         for (i = 0; i < n_local_macs; i++) {
903             if (eth_addr_equals(local_macs[i].addr, mac)) {
904                 is_local = true;
905                 break;
906             }
907         }
908
909         entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
910         memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
911         entry->port_no = port & 0xff;
912         entry->is_local = is_local;
913         entry->ageing_timer_value = age * HZ;
914         entry->port_hi = (port & 0xff00) >> 8;
915         entry->pad0 = 0;
916         entry->unused = 0;
917         count--;
918     }
919     free(output);
920
921     /* Compose and send reply to datapath. */
922     reply = compose_reply(seq, 0);
923     nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
924                       query_data.data, query_data.size);
925     send_reply(reply);
926
927     /* Free memory. */
928     ofpbuf_uninit(&query_data);
929     free(local_macs);
930
931     return 0;
932 }
933
934 static void
935 send_ifindex_reply(uint32_t seq, struct sset *ifaces)
936 {
937     struct ofpbuf *reply;
938     const char *iface;
939     size_t n_indices;
940     int *indices;
941
942     /* Convert 'ifaces' into ifindexes. */
943     n_indices = 0;
944     indices = xmalloc(sset_count(ifaces) * sizeof *indices);
945     SSET_FOR_EACH (iface, ifaces) {
946         int ifindex = if_nametoindex(iface);
947         if (ifindex) {
948             indices[n_indices++] = ifindex;
949         }
950     }
951
952     /* Compose and send reply. */
953     reply = compose_reply(seq, 0);
954     nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
955                       indices, n_indices * sizeof *indices);
956     send_reply(reply);
957
958     /* Free memory. */
959     free(indices);
960 }
961
962 static int
963 handle_get_bridges_cmd(const struct ovsrec_open_vswitch *ovs,
964                        struct ofpbuf *buffer)
965 {
966     struct sset bridges;
967     size_t i, j;
968
969     uint32_t seq;
970
971     int error;
972
973     /* Parse Netlink command.
974      *
975      * The command doesn't actually have any arguments, but we need the
976      * sequence number to send the reply. */
977     error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
978     if (error) {
979         return error;
980     }
981
982     /* Get all the real bridges and all the fake ones. */
983     sset_init(&bridges);
984     for (i = 0; i < ovs->n_bridges; i++) {
985         const struct ovsrec_bridge *br = ovs->bridges[i];
986
987         sset_add(&bridges, br->name);
988         for (j = 0; j < br->n_ports; j++) {
989             const struct ovsrec_port *port = br->ports[j];
990
991             if (port->fake_bridge) {
992                 sset_add(&bridges, port->name);
993             }
994         }
995     }
996
997     send_ifindex_reply(seq, &bridges);
998     sset_destroy(&bridges);
999
1000     return 0;
1001 }
1002
1003 static int
1004 handle_get_ports_cmd(const struct ovsrec_open_vswitch *ovs,
1005                      struct ofpbuf *buffer)
1006 {
1007     uint32_t seq;
1008
1009     const char *linux_name;
1010     const struct ovsrec_bridge *ovs_bridge;
1011     int br_vlan;
1012
1013     struct sset ports;
1014
1015     int error;
1016
1017     /* Parse Netlink command. */
1018     error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
1019     if (error) {
1020         return error;
1021     }
1022
1023     error = linux_bridge_to_ovs_bridge(ovs, linux_name,
1024                                        &ovs_bridge, &br_vlan);
1025     if (error) {
1026         send_simple_reply(seq, error);
1027         return error;
1028     }
1029
1030     sset_init(&ports);
1031     get_bridge_ports(ovs_bridge, &ports, br_vlan);
1032     sset_find_and_delete(&ports, linux_name);
1033     send_ifindex_reply(seq, &ports); /* XXX bonds won't show up */
1034     sset_destroy(&ports);
1035
1036     return 0;
1037 }
1038
1039 static struct ofpbuf *
1040 brc_recv_update__(void)
1041 {
1042     for (;;) {
1043         struct ofpbuf *buffer;
1044         int retval;
1045
1046         retval = nl_sock_recv(brc_sock, &buffer, false);
1047         switch (retval) {
1048         case 0:
1049             if (nl_msg_nlmsgerr(buffer, NULL)
1050                 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE) {
1051                 break;
1052             }
1053             return buffer;
1054
1055         case ENOBUFS:
1056             break;
1057
1058         case EAGAIN:
1059             return NULL;
1060
1061         default:
1062             VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
1063             return NULL;
1064         }
1065         ofpbuf_delete(buffer);
1066     }
1067 }
1068
1069 static void
1070 brc_recv_update(struct ovsdb_idl *idl)
1071 {
1072     struct ofpbuf *buffer;
1073     struct genlmsghdr *genlmsghdr;
1074     const struct ovsrec_open_vswitch *ovs;
1075
1076     buffer = brc_recv_update__();
1077     if (!buffer) {
1078         return;
1079     }
1080
1081     genlmsghdr = nl_msg_genlmsghdr(buffer);
1082     if (!genlmsghdr) {
1083         VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
1084         goto error;
1085     }
1086
1087     if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
1088         VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
1089                 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
1090         goto error;
1091     }
1092
1093     /* Get the Open vSwitch configuration.  Just drop the request on the floor
1094      * if a valid configuration doesn't exist.  (We could check this earlier,
1095      * but we want to drain pending Netlink messages even when there is no Open
1096      * vSwitch configuration.) */
1097     ovs = ovsrec_open_vswitch_first(idl);
1098     if (!ovs) {
1099         VLOG_WARN_RL(&rl, "could not find valid configuration to update");
1100         goto error;
1101     }
1102
1103     switch (genlmsghdr->cmd) {
1104     case BRC_GENL_C_DP_ADD:
1105         handle_bridge_cmd(idl, ovs, buffer, true);
1106         break;
1107
1108     case BRC_GENL_C_DP_DEL:
1109         handle_bridge_cmd(idl, ovs, buffer, false);
1110         break;
1111
1112     case BRC_GENL_C_PORT_ADD:
1113         handle_port_cmd(idl, ovs, buffer, true);
1114         break;
1115
1116     case BRC_GENL_C_PORT_DEL:
1117         handle_port_cmd(idl, ovs, buffer, false);
1118         break;
1119
1120     case BRC_GENL_C_FDB_QUERY:
1121         handle_fdb_query_cmd(ovs, buffer);
1122         break;
1123
1124     case BRC_GENL_C_GET_BRIDGES:
1125         handle_get_bridges_cmd(ovs, buffer);
1126         break;
1127
1128     case BRC_GENL_C_GET_PORTS:
1129         handle_get_ports_cmd(ovs, buffer);
1130         break;
1131
1132     default:
1133         VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
1134                      genlmsghdr->cmd);
1135         break;
1136     }
1137
1138 error:
1139     ofpbuf_delete(buffer);
1140     return;
1141 }
1142
1143 static void
1144 netdev_changed_cb(const struct rtnetlink_link_change *change, void *idl_)
1145 {
1146     struct ovsdb_idl *idl = idl_;
1147     const struct ovsrec_open_vswitch *ovs;
1148     const struct ovsrec_interface *iface;
1149     struct ovsdb_idl_txn *txn;
1150     struct ovsrec_port *port;
1151     struct ovsrec_bridge *br;
1152     char br_name[IFNAMSIZ];
1153     const char *port_name;
1154
1155     if (!change) {
1156         VLOG_WARN_RL(&rl, "network monitor socket overflowed");
1157         return;
1158     }
1159
1160     if (change->nlmsg_type != RTM_DELLINK || !change->master_ifindex) {
1161         return;
1162     }
1163
1164     ovs = ovsrec_open_vswitch_first(idl);
1165     if (!ovs) {
1166         return;
1167     }
1168
1169     port_name = change->ifname;
1170     if (!if_indextoname(change->master_ifindex, br_name)) {
1171         return;
1172     }
1173
1174     if (netdev_exists(port_name)) {
1175         /* A network device by that name exists even though the kernel
1176          * told us it had disappeared.  Probably, what happened was
1177          * this:
1178          *
1179          *      1. Device destroyed.
1180          *      2. Notification sent to us.
1181          *      3. New device created with same name as old one.
1182          *      4. ovs-brcompatd notified, removes device from bridge.
1183          *
1184          * There's no a priori reason that in this situation that the
1185          * new device with the same name should remain in the bridge;
1186          * on the contrary, that would be unexpected.  *But* there is
1187          * one important situation where, if we do this, bad things
1188          * happen.  This is the case of XenServer Tools version 5.0.0,
1189          * which on boot of a Windows VM cause something like this to
1190          * happen on the Xen host:
1191          *
1192          *      i. Create tap1.0 and vif1.0.
1193          *      ii. Delete tap1.0.
1194          *      iii. Delete vif1.0.
1195          *      iv. Re-create vif1.0.
1196          *
1197          * (XenServer Tools 5.5.0 does not exhibit this behavior, and
1198          * neither does a VM without Tools installed at all.)
1199          *
1200          * Steps iii and iv happen within a few seconds of each other.
1201          * Step iv causes /etc/xensource/scripts/vif to run, which in
1202          * turn calls ovs-cfg-mod to add the new device to the bridge.
1203          * If step iv happens after step 4 (in our first list of
1204          * steps), then all is well, but if it happens between 3 and 4
1205          * (which can easily happen if ovs-brcompatd has to wait to
1206          * lock the configuration file), then we will remove the new
1207          * incarnation from the bridge instead of the old one!
1208          *
1209          * So, to avoid this problem, we do nothing here.  This is
1210          * strictly incorrect except for this one particular case, and
1211          * perhaps that will bite us someday.  If that happens, then we
1212          * will have to somehow track network devices by ifindex, since
1213          * a new device will have a new ifindex even if it has the same
1214          * name as an old device.
1215          */
1216         VLOG_INFO("kernel reported network device %s removed but "
1217                   "a device by that name exists (XS Tools 5.0.0?)",
1218                   port_name);
1219         return;
1220     }
1221
1222     VLOG_INFO("network device %s destroyed, removing from bridge %s",
1223               port_name, br_name);
1224
1225     br = find_bridge(ovs, br_name);
1226     if (!br) {
1227         VLOG_WARN("no bridge named %s from which to remove %s",
1228                   br_name, port_name);
1229         return;
1230     }
1231
1232     iface = find_interface(br, port_name, &port);
1233     if (!iface) {
1234         return;
1235     }
1236
1237     txn = ovsdb_idl_txn_create(idl);
1238     del_interface(br, port, iface);
1239     ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: destroy port %s",
1240                               port_name);
1241     commit_txn(txn, false);
1242 }
1243
1244 int
1245 main(int argc, char *argv[])
1246 {
1247     extern struct vlog_module VLM_reconnect;
1248     struct rtnetlink_notifier link_notifier;
1249     struct unixctl_server *unixctl;
1250     const char *remote;
1251     struct ovsdb_idl *idl;
1252     int retval;
1253
1254     proctitle_init(argc, argv);
1255     set_program_name(argv[0]);
1256     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
1257     vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
1258
1259     remote = parse_options(argc, argv);
1260     signal(SIGPIPE, SIG_IGN);
1261     process_init();
1262     ovsrec_init();
1263
1264     daemonize_start();
1265
1266     retval = unixctl_server_create(NULL, &unixctl);
1267     if (retval) {
1268         exit(EXIT_FAILURE);
1269     }
1270
1271     if (brc_open(&brc_sock)) {
1272         VLOG_FATAL("could not open brcompat socket.  Check "
1273                    "\"brcompat\" kernel module.");
1274     }
1275
1276
1277     if (prune_timeout) {
1278         rtnetlink_link_notifier_register(&link_notifier,
1279                                          netdev_changed_cb, NULL);
1280     }
1281
1282     daemonize_complete();
1283
1284     idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
1285
1286     for (;;) {
1287         const struct ovsrec_open_vswitch *ovs;
1288
1289         ovsdb_idl_run(idl);
1290
1291         unixctl_server_run(unixctl);
1292         rtnetlink_link_notifier_run();
1293         brc_recv_update(idl);
1294
1295         ovs = ovsrec_open_vswitch_first(idl);
1296         if (!ovs && ovsdb_idl_has_ever_connected(idl)) {
1297             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1298             VLOG_WARN_RL(&rl, "%s: database does not contain any Open vSwitch "
1299                          "configuration", remote);
1300         }
1301         netdev_run();
1302
1303         /* If 'prune_timeout' is non-zero, we actively prune from the
1304          * configuration of port entries that are no longer valid.  We
1305          * use two methods:
1306          *
1307          *   1) The kernel explicitly notifies us of removed ports
1308          *      through the RTNL messages.
1309          *
1310          *   2) We periodically check all ports associated with bridges
1311          *      to see if they no longer exist.
1312          */
1313         if (ovs && prune_timeout) {
1314             rtnetlink_link_notifier_run();
1315             poll_timer_wait(prune_timeout);
1316         }
1317
1318         nl_sock_wait(brc_sock, POLLIN);
1319         ovsdb_idl_wait(idl);
1320         unixctl_server_wait(unixctl);
1321         rtnetlink_link_notifier_wait();
1322         netdev_wait();
1323         poll_block();
1324     }
1325
1326     if (prune_timeout) {
1327         rtnetlink_link_notifier_unregister(&link_notifier);
1328     }
1329     ovsdb_idl_destroy(idl);
1330
1331     return 0;
1332 }
1333
1334 static void
1335 validate_appctl_command(void)
1336 {
1337     const char *p;
1338     int n;
1339
1340     n = 0;
1341     for (p = strchr(appctl_command, '%'); p; p = strchr(p + 2, '%')) {
1342         if (p[1] == '%') {
1343             /* Nothing to do. */
1344         } else if (p[1] == 's') {
1345             n++;
1346         } else {
1347             VLOG_FATAL("only '%%s' and '%%%%' allowed in --appctl-command");
1348         }
1349     }
1350     if (n != 1) {
1351         VLOG_FATAL("'%%s' must appear exactly once in --appctl-command");
1352     }
1353 }
1354
1355 static const char *
1356 parse_options(int argc, char *argv[])
1357 {
1358     enum {
1359         OPT_PRUNE_TIMEOUT,
1360         OPT_APPCTL_COMMAND,
1361         VLOG_OPTION_ENUMS,
1362         LEAK_CHECKER_OPTION_ENUMS,
1363         DAEMON_OPTION_ENUMS
1364     };
1365     static struct option long_options[] = {
1366         {"help",             no_argument, NULL, 'h'},
1367         {"version",          no_argument, NULL, 'V'},
1368         {"prune-timeout",    required_argument, NULL, OPT_PRUNE_TIMEOUT},
1369         {"appctl-command",   required_argument, NULL, OPT_APPCTL_COMMAND},
1370         DAEMON_LONG_OPTIONS,
1371         VLOG_LONG_OPTIONS,
1372         LEAK_CHECKER_LONG_OPTIONS,
1373         {NULL, 0, NULL, 0},
1374     };
1375     char *short_options = long_options_to_short_options(long_options);
1376
1377     appctl_command = xasprintf("%s/ovs-appctl %%s", ovs_bindir());
1378     for (;;) {
1379         int c;
1380
1381         c = getopt_long(argc, argv, short_options, long_options, NULL);
1382         if (c == -1) {
1383             break;
1384         }
1385
1386         switch (c) {
1387         case 'H':
1388         case 'h':
1389             usage();
1390
1391         case 'V':
1392             OVS_PRINT_VERSION(0, 0);
1393             exit(EXIT_SUCCESS);
1394
1395         case OPT_PRUNE_TIMEOUT:
1396             prune_timeout = atoi(optarg) * 1000;
1397             break;
1398
1399         case OPT_APPCTL_COMMAND:
1400             appctl_command = optarg;
1401             break;
1402
1403         VLOG_OPTION_HANDLERS
1404         DAEMON_OPTION_HANDLERS
1405         LEAK_CHECKER_OPTION_HANDLERS
1406
1407         case '?':
1408             exit(EXIT_FAILURE);
1409
1410         default:
1411             abort();
1412         }
1413     }
1414     free(short_options);
1415
1416     validate_appctl_command();
1417
1418     argc -= optind;
1419     argv += optind;
1420
1421     if (argc != 1) {
1422         VLOG_FATAL("database socket is non-option argument; "
1423                    "use --help for usage");
1424     }
1425
1426     return argv[0];
1427 }
1428
1429 static void
1430 usage(void)
1431 {
1432     printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
1433            "usage: %s [OPTIONS] CONFIG\n"
1434            "CONFIG is the configuration file used by ovs-vswitchd.\n",
1435            program_name, program_name);
1436     printf("\nConfiguration options:\n"
1437            "  --appctl-command=COMMAND  shell command to run ovs-appctl\n"
1438            "  --prune-timeout=SECS    wait at most SECS before pruning ports\n"
1439           );
1440     daemon_usage();
1441     vlog_usage();
1442     printf("\nOther options:\n"
1443            "  -h, --help              display this help message\n"
1444            "  -V, --version           display version information\n");
1445     leak_checker_usage();
1446     printf("\nThe default appctl command is:\n%s\n", appctl_command);
1447     exit(EXIT_SUCCESS);
1448 }