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