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