]> git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-vport.c
datapath: Add multicast tunnel support.
[mirror_ovs.git] / lib / netdev-vport.c
1 /*
2 * Copyright (c) 2010, 2011 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include "netdev-vport.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/socket.h>
24 #include <linux/openvswitch.h>
25 #include <linux/rtnetlink.h>
26 #include <net/if.h>
27 #include <sys/ioctl.h>
28
29 #include "byte-order.h"
30 #include "daemon.h"
31 #include "dirs.h"
32 #include "dpif-linux.h"
33 #include "hash.h"
34 #include "hmap.h"
35 #include "list.h"
36 #include "netdev-linux.h"
37 #include "netdev-provider.h"
38 #include "netlink.h"
39 #include "netlink-notifier.h"
40 #include "netlink-socket.h"
41 #include "ofpbuf.h"
42 #include "openvswitch/tunnel.h"
43 #include "packets.h"
44 #include "route-table.h"
45 #include "shash.h"
46 #include "socket-util.h"
47 #include "unaligned.h"
48 #include "vlog.h"
49
50 VLOG_DEFINE_THIS_MODULE(netdev_vport);
51
52 struct netdev_dev_vport {
53 struct netdev_dev netdev_dev;
54 struct ofpbuf *options;
55 int dp_ifindex; /* -1 if unknown. */
56 uint32_t port_no; /* UINT32_MAX if unknown. */
57 unsigned int change_seq;
58 };
59
60 struct netdev_vport {
61 struct netdev netdev;
62 };
63
64 struct vport_class {
65 enum ovs_vport_type type;
66 struct netdev_class netdev_class;
67 int (*parse_config)(const char *name, const char *type,
68 const struct shash *args, struct ofpbuf *options);
69 int (*unparse_config)(const char *name, const char *type,
70 const struct nlattr *options, size_t options_len,
71 struct shash *args);
72 };
73
74 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
75
76 static int netdev_vport_create(const struct netdev_class *, const char *,
77 struct netdev_dev **);
78 static void netdev_vport_poll_notify(const struct netdev *);
79 static int tnl_port_config_from_nlattr(const struct nlattr *options,
80 size_t options_len,
81 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1]);
82
83 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
84
85 static bool
86 is_vport_class(const struct netdev_class *class)
87 {
88 return class->create == netdev_vport_create;
89 }
90
91 static const struct vport_class *
92 vport_class_cast(const struct netdev_class *class)
93 {
94 assert(is_vport_class(class));
95 return CONTAINER_OF(class, struct vport_class, netdev_class);
96 }
97
98 static struct netdev_dev_vport *
99 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
100 {
101 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
102 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
103 }
104
105 static struct netdev_vport *
106 netdev_vport_cast(const struct netdev *netdev)
107 {
108 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
109 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
110 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
111 }
112
113 /* If 'netdev' is a vport netdev, returns an ofpbuf that contains Netlink
114 * options to include in OVS_VPORT_ATTR_OPTIONS for configuring that vport.
115 * Otherwise returns NULL. */
116 const struct ofpbuf *
117 netdev_vport_get_options(const struct netdev *netdev)
118 {
119 const struct netdev_dev *dev = netdev_get_dev(netdev);
120
121 return (is_vport_class(netdev_dev_get_class(dev))
122 ? netdev_dev_vport_cast(dev)->options
123 : NULL);
124 }
125
126 enum ovs_vport_type
127 netdev_vport_get_vport_type(const struct netdev *netdev)
128 {
129 const struct netdev_dev *dev = netdev_get_dev(netdev);
130 const struct netdev_class *class = netdev_dev_get_class(dev);
131
132 return (is_vport_class(class) ? vport_class_cast(class)->type
133 : class == &netdev_internal_class ? OVS_VPORT_TYPE_INTERNAL
134 : class == &netdev_linux_class ? OVS_VPORT_TYPE_NETDEV
135 : OVS_VPORT_TYPE_UNSPEC);
136 }
137
138 const char *
139 netdev_vport_get_netdev_type(const struct dpif_linux_vport *vport)
140 {
141 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
142
143 switch (vport->type) {
144 case OVS_VPORT_TYPE_UNSPEC:
145 break;
146
147 case OVS_VPORT_TYPE_NETDEV:
148 return "system";
149
150 case OVS_VPORT_TYPE_INTERNAL:
151 return "internal";
152
153 case OVS_VPORT_TYPE_PATCH:
154 return "patch";
155
156 case OVS_VPORT_TYPE_GRE:
157 if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
158 a)) {
159 break;
160 }
161 return (nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
162 ? "ipsec_gre" : "gre");
163
164 case OVS_VPORT_TYPE_CAPWAP:
165 return "capwap";
166
167 case __OVS_VPORT_TYPE_MAX:
168 break;
169 }
170
171 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
172 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
173 return "unknown";
174 }
175
176 static int
177 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
178 struct netdev_dev **netdev_devp)
179 {
180 struct netdev_dev_vport *dev;
181
182 dev = xmalloc(sizeof *dev);
183 netdev_dev_init(&dev->netdev_dev, name, netdev_class);
184 dev->options = NULL;
185 dev->dp_ifindex = -1;
186 dev->port_no = UINT32_MAX;
187 dev->change_seq = 1;
188
189 *netdev_devp = &dev->netdev_dev;
190 route_table_register();
191
192 return 0;
193 }
194
195 static void
196 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
197 {
198 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
199
200 route_table_unregister();
201 free(netdev_dev);
202 }
203
204 static int
205 netdev_vport_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
206 {
207 struct netdev_vport *netdev;
208
209 netdev = xmalloc(sizeof *netdev);
210 netdev_init(&netdev->netdev, netdev_dev_);
211
212 *netdevp = &netdev->netdev;
213 return 0;
214 }
215
216 static void
217 netdev_vport_close(struct netdev *netdev_)
218 {
219 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
220 free(netdev);
221 }
222
223 static int
224 netdev_vport_get_config(struct netdev_dev *dev_, struct shash *args)
225 {
226 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
227 const struct vport_class *vport_class = vport_class_cast(netdev_class);
228 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
229 const char *name = netdev_dev_get_name(dev_);
230 int error;
231
232 if (!dev->options) {
233 struct dpif_linux_vport reply;
234 struct ofpbuf *buf;
235
236 error = dpif_linux_vport_get(name, &reply, &buf);
237 if (error) {
238 VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
239 name, strerror(error));
240 return error;
241 }
242
243 dev->options = ofpbuf_clone_data(reply.options, reply.options_len);
244 dev->dp_ifindex = reply.dp_ifindex;
245 dev->port_no = reply.port_no;
246 ofpbuf_delete(buf);
247 }
248
249 error = vport_class->unparse_config(name, netdev_class->type,
250 dev->options->data,
251 dev->options->size,
252 args);
253 if (error) {
254 VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
255 name, strerror(error));
256 }
257 return error;
258 }
259
260 static int
261 netdev_vport_set_config(struct netdev_dev *dev_, const struct shash *args)
262 {
263 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
264 const struct vport_class *vport_class = vport_class_cast(netdev_class);
265 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
266 const char *name = netdev_dev_get_name(dev_);
267 struct ofpbuf *options;
268 int error;
269
270 options = ofpbuf_new(64);
271 error = vport_class->parse_config(name, netdev_dev_get_type(dev_),
272 args, options);
273 if (!error
274 && (!dev->options
275 || options->size != dev->options->size
276 || memcmp(options->data, dev->options->data, options->size))) {
277 struct dpif_linux_vport vport;
278
279 dpif_linux_vport_init(&vport);
280 vport.cmd = OVS_VPORT_CMD_SET;
281 vport.name = name;
282 vport.options = options->data;
283 vport.options_len = options->size;
284 error = dpif_linux_vport_transact(&vport, NULL, NULL);
285 if (!error || error == ENODEV) {
286 /* Either reconfiguration succeeded or this vport is not installed
287 * in the kernel (e.g. it hasn't been added to a dpif yet with
288 * dpif_port_add()). */
289 ofpbuf_delete(dev->options);
290 dev->options = options;
291 options = NULL;
292 error = 0;
293 }
294 }
295 ofpbuf_delete(options);
296
297 return error;
298 }
299
300 static int
301 netdev_vport_send(struct netdev *netdev, const void *data, size_t size)
302 {
303 struct netdev_dev *dev_ = netdev_get_dev(netdev);
304 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
305
306 if (dev->dp_ifindex == -1) {
307 const char *name = netdev_get_name(netdev);
308 struct dpif_linux_vport reply;
309 struct ofpbuf *buf;
310 int error;
311
312 error = dpif_linux_vport_get(name, &reply, &buf);
313 if (error) {
314 VLOG_ERR_RL(&rl, "%s: failed to query vport for send (%s)",
315 name, strerror(error));
316 return error;
317 }
318 dev->dp_ifindex = reply.dp_ifindex;
319 dev->port_no = reply.port_no;
320 ofpbuf_delete(buf);
321 }
322
323 return dpif_linux_vport_send(dev->dp_ifindex, dev->port_no, data, size);
324 }
325
326 static int
327 netdev_vport_set_etheraddr(struct netdev *netdev,
328 const uint8_t mac[ETH_ADDR_LEN])
329 {
330 struct dpif_linux_vport vport;
331 int error;
332
333 dpif_linux_vport_init(&vport);
334 vport.cmd = OVS_VPORT_CMD_SET;
335 vport.name = netdev_get_name(netdev);
336 vport.address = mac;
337
338 error = dpif_linux_vport_transact(&vport, NULL, NULL);
339 if (!error) {
340 netdev_vport_poll_notify(netdev);
341 }
342 return error;
343 }
344
345 static int
346 netdev_vport_get_etheraddr(const struct netdev *netdev,
347 uint8_t mac[ETH_ADDR_LEN])
348 {
349 struct dpif_linux_vport reply;
350 struct ofpbuf *buf;
351 int error;
352
353 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
354 if (!error) {
355 if (reply.address) {
356 memcpy(mac, reply.address, ETH_ADDR_LEN);
357 } else {
358 error = EOPNOTSUPP;
359 }
360 ofpbuf_delete(buf);
361 }
362 return error;
363 }
364
365 /* Copies 'src' into 'dst', performing format conversion in the process.
366 *
367 * 'src' is allowed to be misaligned. */
368 static void
369 netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst,
370 const struct ovs_vport_stats *src)
371 {
372 dst->rx_packets = get_unaligned_u64(&src->rx_packets);
373 dst->tx_packets = get_unaligned_u64(&src->tx_packets);
374 dst->rx_bytes = get_unaligned_u64(&src->rx_bytes);
375 dst->tx_bytes = get_unaligned_u64(&src->tx_bytes);
376 dst->rx_errors = get_unaligned_u64(&src->rx_errors);
377 dst->tx_errors = get_unaligned_u64(&src->tx_errors);
378 dst->rx_dropped = get_unaligned_u64(&src->rx_dropped);
379 dst->tx_dropped = get_unaligned_u64(&src->tx_dropped);
380 dst->multicast = 0;
381 dst->collisions = 0;
382 dst->rx_length_errors = 0;
383 dst->rx_over_errors = 0;
384 dst->rx_crc_errors = 0;
385 dst->rx_frame_errors = 0;
386 dst->rx_fifo_errors = 0;
387 dst->rx_missed_errors = 0;
388 dst->tx_aborted_errors = 0;
389 dst->tx_carrier_errors = 0;
390 dst->tx_fifo_errors = 0;
391 dst->tx_heartbeat_errors = 0;
392 dst->tx_window_errors = 0;
393 }
394
395 /* Copies 'src' into 'dst', performing format conversion in the process. */
396 static void
397 netdev_stats_to_ovs_vport_stats(struct ovs_vport_stats *dst,
398 const struct netdev_stats *src)
399 {
400 dst->rx_packets = src->rx_packets;
401 dst->tx_packets = src->tx_packets;
402 dst->rx_bytes = src->rx_bytes;
403 dst->tx_bytes = src->tx_bytes;
404 dst->rx_errors = src->rx_errors;
405 dst->tx_errors = src->tx_errors;
406 dst->rx_dropped = src->rx_dropped;
407 dst->tx_dropped = src->tx_dropped;
408 }
409
410 int
411 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
412 {
413 struct dpif_linux_vport reply;
414 struct ofpbuf *buf;
415 int error;
416
417 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
418 if (error) {
419 return error;
420 } else if (!reply.stats) {
421 ofpbuf_delete(buf);
422 return EOPNOTSUPP;
423 }
424
425 netdev_stats_from_ovs_vport_stats(stats, reply.stats);
426
427 ofpbuf_delete(buf);
428
429 return 0;
430 }
431
432 int
433 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
434 {
435 struct ovs_vport_stats rtnl_stats;
436 struct dpif_linux_vport vport;
437 int err;
438
439 netdev_stats_to_ovs_vport_stats(&rtnl_stats, stats);
440
441 dpif_linux_vport_init(&vport);
442 vport.cmd = OVS_VPORT_CMD_SET;
443 vport.name = netdev_get_name(netdev);
444 vport.stats = &rtnl_stats;
445
446 err = dpif_linux_vport_transact(&vport, NULL, NULL);
447
448 /* If the vport layer doesn't know about the device, that doesn't mean it
449 * doesn't exist (after all were able to open it when netdev_open() was
450 * called), it just means that it isn't attached and we'll be getting
451 * stats a different way. */
452 if (err == ENODEV) {
453 err = EOPNOTSUPP;
454 }
455
456 return err;
457 }
458
459 static int
460 netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
461 {
462 const char *iface = netdev_vport_get_tnl_iface(netdev);
463
464 if (iface) {
465 struct netdev *egress_netdev;
466
467 shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
468
469 if (!netdev_open(iface, "system", &egress_netdev)) {
470 shash_add(sh, "tunnel_egress_iface_carrier",
471 xstrdup(netdev_get_carrier(egress_netdev)
472 ? "up" : "down"));
473 netdev_close(egress_netdev);
474 }
475 }
476
477 return 0;
478 }
479
480 static int
481 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
482 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
483 enum netdev_flags *old_flagsp)
484 {
485 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
486 return EOPNOTSUPP;
487 }
488
489 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
490 return 0;
491 }
492
493 static unsigned int
494 netdev_vport_change_seq(const struct netdev *netdev)
495 {
496 return netdev_dev_vport_cast(netdev_get_dev(netdev))->change_seq;
497 }
498
499 static void
500 netdev_vport_run(void)
501 {
502 route_table_run();
503 }
504
505 static void
506 netdev_vport_wait(void)
507 {
508 route_table_wait();
509 }
510 \f
511 /* get_tnl_iface() implementation. */
512 static const char *
513 netdev_vport_get_tnl_iface(const struct netdev *netdev)
514 {
515 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
516 ovs_be32 route;
517 struct netdev_dev_vport *ndv;
518 static char name[IFNAMSIZ];
519
520 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
521 if (tnl_port_config_from_nlattr(ndv->options->data, ndv->options->size,
522 a)) {
523 return NULL;
524 }
525 route = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
526
527 if (route_table_get_name(route, name)) {
528 return name;
529 }
530
531 return NULL;
532 }
533 \f
534 /* Helper functions. */
535
536 static void
537 netdev_vport_poll_notify(const struct netdev *netdev)
538 {
539 struct netdev_dev_vport *ndv;
540
541 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
542
543 ndv->change_seq++;
544 if (!ndv->change_seq) {
545 ndv->change_seq++;
546 }
547 }
548 \f
549 /* Code specific to individual vport types. */
550
551 static void
552 set_key(const struct shash *args, const char *name, uint16_t type,
553 struct ofpbuf *options)
554 {
555 const char *s;
556
557 s = shash_find_data(args, name);
558 if (!s) {
559 s = shash_find_data(args, "key");
560 if (!s) {
561 s = "0";
562 }
563 }
564
565 if (!strcmp(s, "flow")) {
566 /* This is the default if no attribute is present. */
567 } else {
568 nl_msg_put_be64(options, type, htonll(strtoull(s, NULL, 0)));
569 }
570 }
571
572 static int
573 parse_tunnel_config(const char *name, const char *type,
574 const struct shash *args, struct ofpbuf *options)
575 {
576 bool is_gre = false;
577 bool is_ipsec = false;
578 struct shash_node *node;
579 bool ipsec_mech_set = false;
580 ovs_be32 daddr = htonl(0);
581 ovs_be32 saddr = htonl(0);
582 uint32_t flags;
583
584 flags = TNL_F_DF_DEFAULT | TNL_F_PMTUD | TNL_F_HDR_CACHE;
585 if (!strcmp(type, "gre")) {
586 is_gre = true;
587 } else if (!strcmp(type, "ipsec_gre")) {
588 is_gre = true;
589 is_ipsec = true;
590 flags |= TNL_F_IPSEC;
591 flags &= ~TNL_F_HDR_CACHE;
592 }
593
594 SHASH_FOR_EACH (node, args) {
595 if (!strcmp(node->name, "remote_ip")) {
596 struct in_addr in_addr;
597 if (lookup_ip(node->data, &in_addr)) {
598 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
599 } else {
600 daddr = in_addr.s_addr;
601 }
602 } else if (!strcmp(node->name, "local_ip")) {
603 struct in_addr in_addr;
604 if (lookup_ip(node->data, &in_addr)) {
605 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
606 } else {
607 saddr = in_addr.s_addr;
608 }
609 } else if (!strcmp(node->name, "tos")) {
610 if (!strcmp(node->data, "inherit")) {
611 flags |= TNL_F_TOS_INHERIT;
612 } else {
613 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TOS, atoi(node->data));
614 }
615 } else if (!strcmp(node->name, "ttl")) {
616 if (!strcmp(node->data, "inherit")) {
617 flags |= TNL_F_TTL_INHERIT;
618 } else {
619 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TTL, atoi(node->data));
620 }
621 } else if (!strcmp(node->name, "csum") && is_gre) {
622 if (!strcmp(node->data, "true")) {
623 flags |= TNL_F_CSUM;
624 }
625 } else if (!strcmp(node->name, "df_inherit")) {
626 if (!strcmp(node->data, "true")) {
627 flags |= TNL_F_DF_INHERIT;
628 }
629 } else if (!strcmp(node->name, "df_default")) {
630 if (!strcmp(node->data, "false")) {
631 flags &= ~TNL_F_DF_DEFAULT;
632 }
633 } else if (!strcmp(node->name, "pmtud")) {
634 if (!strcmp(node->data, "false")) {
635 flags &= ~TNL_F_PMTUD;
636 }
637 } else if (!strcmp(node->name, "header_cache")) {
638 if (!strcmp(node->data, "false")) {
639 flags &= ~TNL_F_HDR_CACHE;
640 }
641 } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
642 if (shash_find(args, "certificate")) {
643 ipsec_mech_set = true;
644 } else {
645 const char *use_ssl_cert;
646
647 /* If the "use_ssl_cert" is true, then "certificate" and
648 * "private_key" will be pulled from the SSL table. The
649 * use of this option is strongly discouraged, since it
650 * will like be removed when multiple SSL configurations
651 * are supported by OVS.
652 */
653 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
654 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
655 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
656 name);
657 return EINVAL;
658 }
659 ipsec_mech_set = true;
660 }
661 } else if (!strcmp(node->name, "psk") && is_ipsec) {
662 ipsec_mech_set = true;
663 } else if (is_ipsec
664 && (!strcmp(node->name, "certificate")
665 || !strcmp(node->name, "private_key")
666 || !strcmp(node->name, "use_ssl_cert"))) {
667 /* Ignore options not used by the netdev. */
668 } else if (!strcmp(node->name, "key") ||
669 !strcmp(node->name, "in_key") ||
670 !strcmp(node->name, "out_key")) {
671 /* Handled separately below. */
672 } else {
673 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
674 }
675 }
676
677 if (is_ipsec) {
678 char *file_name = xasprintf("%s/%s", ovs_rundir(),
679 "ovs-monitor-ipsec.pid");
680 pid_t pid = read_pidfile(file_name);
681 free(file_name);
682 if (pid < 0) {
683 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
684 name);
685 return EINVAL;
686 }
687
688 if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
689 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
690 return EINVAL;
691 }
692
693 if (!ipsec_mech_set) {
694 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
695 name);
696 return EINVAL;
697 }
698 }
699
700 set_key(args, "in_key", OVS_TUNNEL_ATTR_IN_KEY, options);
701 set_key(args, "out_key", OVS_TUNNEL_ATTR_OUT_KEY, options);
702
703 if (!daddr) {
704 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
705 name, type);
706 return EINVAL;
707 }
708 nl_msg_put_be32(options, OVS_TUNNEL_ATTR_DST_IPV4, daddr);
709
710 if (saddr) {
711 if (ip_is_multicast(daddr)) {
712 VLOG_WARN("%s: remote_ip is multicast, ignoring local_ip", name);
713 } else {
714 nl_msg_put_be32(options, OVS_TUNNEL_ATTR_SRC_IPV4, saddr);
715 }
716 }
717
718 nl_msg_put_u32(options, OVS_TUNNEL_ATTR_FLAGS, flags);
719
720 return 0;
721 }
722
723 static int
724 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
725 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1])
726 {
727 static const struct nl_policy ovs_tunnel_policy[] = {
728 [OVS_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32 },
729 [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32 },
730 [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
731 [OVS_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
732 [OVS_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
733 [OVS_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
734 [OVS_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
735 };
736 struct ofpbuf buf;
737
738 ofpbuf_use_const(&buf, options, options_len);
739 if (!nl_policy_parse(&buf, 0, ovs_tunnel_policy,
740 a, ARRAY_SIZE(ovs_tunnel_policy))) {
741 return EINVAL;
742 }
743 return 0;
744 }
745
746 static uint64_t
747 get_be64_or_zero(const struct nlattr *a)
748 {
749 return a ? ntohll(nl_attr_get_be64(a)) : 0;
750 }
751
752 static int
753 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
754 const struct nlattr *options, size_t options_len,
755 struct shash *args)
756 {
757 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
758 ovs_be32 daddr;
759 uint32_t flags;
760 int error;
761
762 error = tnl_port_config_from_nlattr(options, options_len, a);
763 if (error) {
764 return error;
765 }
766
767 flags = nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]);
768 if (!(flags & TNL_F_HDR_CACHE) == !(flags & TNL_F_IPSEC)) {
769 smap_add(args, "header_cache",
770 flags & TNL_F_HDR_CACHE ? "true" : "false");
771 }
772
773 daddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
774 shash_add(args, "remote_ip", xasprintf(IP_FMT, IP_ARGS(&daddr)));
775
776 if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
777 ovs_be32 saddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
778 shash_add(args, "local_ip", xasprintf(IP_FMT, IP_ARGS(&saddr)));
779 }
780
781 if (!a[OVS_TUNNEL_ATTR_IN_KEY] && !a[OVS_TUNNEL_ATTR_OUT_KEY]) {
782 smap_add(args, "key", "flow");
783 } else {
784 uint64_t in_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_IN_KEY]);
785 uint64_t out_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_OUT_KEY]);
786
787 if (in_key && in_key == out_key) {
788 shash_add(args, "key", xasprintf("%"PRIu64, in_key));
789 } else {
790 if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
791 smap_add(args, "in_key", "flow");
792 } else if (in_key) {
793 shash_add(args, "in_key", xasprintf("%"PRIu64, in_key));
794 }
795
796 if (!a[OVS_TUNNEL_ATTR_OUT_KEY]) {
797 smap_add(args, "out_key", "flow");
798 } else if (out_key) {
799 shash_add(args, "out_key", xasprintf("%"PRIu64, out_key));
800 }
801 }
802 }
803
804 if (flags & TNL_F_TTL_INHERIT) {
805 smap_add(args, "tos", "inherit");
806 } else if (a[OVS_TUNNEL_ATTR_TTL]) {
807 int ttl = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
808 shash_add(args, "tos", xasprintf("%d", ttl));
809 }
810
811 if (flags & TNL_F_TOS_INHERIT) {
812 smap_add(args, "tos", "inherit");
813 } else if (a[OVS_TUNNEL_ATTR_TOS]) {
814 int tos = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
815 shash_add(args, "tos", xasprintf("%d", tos));
816 }
817
818 if (flags & TNL_F_CSUM) {
819 smap_add(args, "csum", "true");
820 }
821 if (flags & TNL_F_DF_INHERIT) {
822 smap_add(args, "df_inherit", "true");
823 }
824 if (!(flags & TNL_F_DF_DEFAULT)) {
825 smap_add(args, "df_default", "false");
826 }
827 if (!(flags & TNL_F_PMTUD)) {
828 smap_add(args, "pmtud", "false");
829 }
830
831 return 0;
832 }
833
834 static int
835 parse_patch_config(const char *name, const char *type OVS_UNUSED,
836 const struct shash *args, struct ofpbuf *options)
837 {
838 const char *peer;
839
840 peer = shash_find_data(args, "peer");
841 if (!peer) {
842 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
843 return EINVAL;
844 }
845
846 if (shash_count(args) > 1) {
847 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
848 return EINVAL;
849 }
850
851 if (strlen(peer) >= IFNAMSIZ) {
852 VLOG_ERR("%s: patch 'peer' arg too long", name);
853 return EINVAL;
854 }
855
856 if (!strcmp(name, peer)) {
857 VLOG_ERR("%s: patch peer must not be self", name);
858 return EINVAL;
859 }
860
861 nl_msg_put_string(options, OVS_PATCH_ATTR_PEER, peer);
862
863 return 0;
864 }
865
866 static int
867 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
868 const struct nlattr *options, size_t options_len,
869 struct shash *args)
870 {
871 static const struct nl_policy ovs_patch_policy[] = {
872 [OVS_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
873 .max_len = IFNAMSIZ,
874 .optional = false }
875 };
876
877 struct nlattr *a[ARRAY_SIZE(ovs_patch_policy)];
878 struct ofpbuf buf;
879
880 ofpbuf_use_const(&buf, options, options_len);
881 if (!nl_policy_parse(&buf, 0, ovs_patch_policy,
882 a, ARRAY_SIZE(ovs_patch_policy))) {
883 return EINVAL;
884 }
885
886 smap_add(args, "peer", nl_attr_get_string(a[OVS_PATCH_ATTR_PEER]));
887 return 0;
888 }
889 \f
890 #define VPORT_FUNCTIONS(GET_STATUS) \
891 NULL, \
892 netdev_vport_run, \
893 netdev_vport_wait, \
894 \
895 netdev_vport_create, \
896 netdev_vport_destroy, \
897 netdev_vport_get_config, \
898 netdev_vport_set_config, \
899 \
900 netdev_vport_open, \
901 netdev_vport_close, \
902 \
903 NULL, /* listen */ \
904 NULL, /* recv */ \
905 NULL, /* recv_wait */ \
906 NULL, /* drain */ \
907 \
908 netdev_vport_send, /* send */ \
909 NULL, /* send_wait */ \
910 \
911 netdev_vport_set_etheraddr, \
912 netdev_vport_get_etheraddr, \
913 NULL, /* get_mtu */ \
914 NULL, /* set_mtu */ \
915 NULL, /* get_ifindex */ \
916 NULL, /* get_carrier */ \
917 NULL, /* get_carrier_resets */ \
918 NULL, /* get_miimon */ \
919 netdev_vport_get_stats, \
920 netdev_vport_set_stats, \
921 \
922 NULL, /* get_features */ \
923 NULL, /* set_advertisements */ \
924 NULL, /* get_vlan_vid */ \
925 \
926 NULL, /* set_policing */ \
927 NULL, /* get_qos_types */ \
928 NULL, /* get_qos_capabilities */ \
929 NULL, /* get_qos */ \
930 NULL, /* set_qos */ \
931 NULL, /* get_queue */ \
932 NULL, /* set_queue */ \
933 NULL, /* delete_queue */ \
934 NULL, /* get_queue_stats */ \
935 NULL, /* dump_queues */ \
936 NULL, /* dump_queue_stats */ \
937 \
938 NULL, /* get_in4 */ \
939 NULL, /* set_in4 */ \
940 NULL, /* get_in6 */ \
941 NULL, /* add_router */ \
942 NULL, /* get_next_hop */ \
943 GET_STATUS, \
944 NULL, /* arp_lookup */ \
945 \
946 netdev_vport_update_flags, \
947 \
948 netdev_vport_change_seq
949
950 void
951 netdev_vport_register(void)
952 {
953 static const struct vport_class vport_classes[] = {
954 { OVS_VPORT_TYPE_GRE,
955 { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
956 parse_tunnel_config, unparse_tunnel_config },
957
958 { OVS_VPORT_TYPE_GRE,
959 { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
960 parse_tunnel_config, unparse_tunnel_config },
961
962 { OVS_VPORT_TYPE_CAPWAP,
963 { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
964 parse_tunnel_config, unparse_tunnel_config },
965
966 { OVS_VPORT_TYPE_PATCH,
967 { "patch", VPORT_FUNCTIONS(NULL) },
968 parse_patch_config, unparse_patch_config }
969 };
970
971 int i;
972
973 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
974 netdev_register_provider(&vport_classes[i].netdev_class);
975 }
976 }