2 * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "netdev-vport.h"
23 #include <sys/socket.h>
25 #include <sys/ioctl.h>
27 #include "byte-order.h"
34 #include "netdev-provider.h"
37 #include "route-table.h"
39 #include "socket-util.h"
42 VLOG_DEFINE_THIS_MODULE(netdev_vport);
44 /* Default to the OTV port, per the VXLAN IETF draft. */
45 #define VXLAN_DST_PORT 8472
47 #define LISP_DST_PORT 4341
49 #define DEFAULT_TTL 64
51 struct netdev_dev_vport {
52 struct netdev_dev netdev_dev;
53 unsigned int change_seq;
54 uint8_t etheraddr[ETH_ADDR_LEN];
55 struct netdev_stats stats;
58 struct netdev_tunnel_config tnl_cfg;
65 const char *dpif_port;
66 struct netdev_class netdev_class;
69 static int netdev_vport_create(const struct netdev_class *, const char *,
70 struct netdev_dev **);
71 static int get_patch_config(struct netdev_dev *, struct smap *args);
72 static int get_tunnel_config(struct netdev_dev *, struct smap *args);
73 static void netdev_vport_poll_notify(struct netdev_dev_vport *);
76 is_vport_class(const struct netdev_class *class)
78 return class->create == netdev_vport_create;
81 static const struct vport_class *
82 vport_class_cast(const struct netdev_class *class)
84 ovs_assert(is_vport_class(class));
85 return CONTAINER_OF(class, struct vport_class, netdev_class);
88 static struct netdev_dev_vport *
89 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
91 ovs_assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
92 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
95 static struct netdev_dev_vport *
96 netdev_vport_get_dev(const struct netdev *netdev)
98 return netdev_dev_vport_cast(netdev_get_dev(netdev));
101 static const struct netdev_tunnel_config *
102 get_netdev_tunnel_config(const struct netdev_dev *netdev_dev)
104 return &netdev_dev_vport_cast(netdev_dev)->tnl_cfg;
108 netdev_vport_is_patch(const struct netdev *netdev)
110 const struct netdev_dev *dev = netdev_get_dev(netdev);
111 const struct netdev_class *class = netdev_dev_get_class(dev);
113 return class->get_config == get_patch_config;
117 netdev_vport_needs_dst_port(const struct netdev_dev *dev)
119 const struct netdev_class *class = netdev_dev_get_class(dev);
120 const char *type = netdev_dev_get_type(dev);
122 return (class->get_config == get_tunnel_config &&
123 (!strcmp("vxlan", type) || !strcmp("lisp", type)));
127 netdev_vport_get_dpif_port(const struct netdev *netdev)
129 const struct netdev_dev *dev = netdev_get_dev(netdev);
130 const struct netdev_class *class = netdev_dev_get_class(dev);
131 const char *dpif_port;
133 if (netdev_vport_needs_dst_port(dev)) {
134 const struct netdev_dev_vport *vport = netdev_vport_get_dev(netdev);
135 const char *type = netdev_dev_get_type(dev);
136 static char dpif_port_combined[IFNAMSIZ];
139 * Note: IFNAMSIZ is 16 bytes long. The maximum length of a VXLAN
140 * or LISP port name below is 15 or 14 bytes respectively. Still,
141 * assert here on the size of strlen(type) in case that changes
144 ovs_assert(strlen(type) + 10 < IFNAMSIZ);
145 snprintf(dpif_port_combined, IFNAMSIZ, "%s_sys_%d", type,
146 ntohs(vport->tnl_cfg.dst_port));
147 return dpif_port_combined;
149 dpif_port = (is_vport_class(class)
150 ? vport_class_cast(class)->dpif_port
154 return dpif_port ? dpif_port : netdev_get_name(netdev);
158 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
159 struct netdev_dev **netdev_devp)
161 struct netdev_dev_vport *dev;
163 dev = xzalloc(sizeof *dev);
164 netdev_dev_init(&dev->netdev_dev, name, netdev_class);
166 eth_addr_random(dev->etheraddr);
168 *netdev_devp = &dev->netdev_dev;
169 route_table_register();
175 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
177 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
179 route_table_unregister();
180 free(netdev_dev->peer);
185 netdev_vport_open(struct netdev_dev *netdev_dev, struct netdev **netdevp)
187 *netdevp = xmalloc(sizeof **netdevp);
188 netdev_init(*netdevp, netdev_dev);
193 netdev_vport_close(struct netdev *netdev)
199 netdev_vport_set_etheraddr(struct netdev *netdev,
200 const uint8_t mac[ETH_ADDR_LEN])
202 struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
203 memcpy(dev->etheraddr, mac, ETH_ADDR_LEN);
204 netdev_vport_poll_notify(dev);
209 netdev_vport_get_etheraddr(const struct netdev *netdev,
210 uint8_t mac[ETH_ADDR_LEN])
212 memcpy(mac, netdev_vport_get_dev(netdev)->etheraddr, ETH_ADDR_LEN);
217 tunnel_get_status(const struct netdev *netdev, struct smap *smap)
219 static char iface[IFNAMSIZ];
222 route = netdev_vport_get_dev(netdev)->tnl_cfg.ip_dst;
223 if (route_table_get_name(route, iface)) {
224 struct netdev *egress_netdev;
226 smap_add(smap, "tunnel_egress_iface", iface);
228 if (!netdev_open(iface, "system", &egress_netdev)) {
229 smap_add(smap, "tunnel_egress_iface_carrier",
230 netdev_get_carrier(egress_netdev) ? "up" : "down");
231 netdev_close(egress_netdev);
239 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
240 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
241 enum netdev_flags *old_flagsp)
243 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
247 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
252 netdev_vport_change_seq(const struct netdev *netdev)
254 return netdev_vport_get_dev(netdev)->change_seq;
258 netdev_vport_run(void)
264 netdev_vport_wait(void)
269 /* Helper functions. */
272 netdev_vport_poll_notify(struct netdev_dev_vport *ndv)
275 if (!ndv->change_seq) {
280 /* Code specific to tunnel types. */
283 parse_key(const struct smap *args, const char *name,
284 bool *present, bool *flow)
291 s = smap_get(args, name);
293 s = smap_get(args, "key");
301 if (!strcmp(s, "flow")) {
305 return htonll(strtoull(s, NULL, 0));
310 set_tunnel_config(struct netdev_dev *dev_, const struct smap *args)
312 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
313 const char *name = netdev_dev_get_name(dev_);
314 const char *type = netdev_dev_get_type(dev_);
315 bool ipsec_mech_set, needs_dst_port, has_csum;
316 struct netdev_tunnel_config tnl_cfg;
317 struct smap_node *node;
319 has_csum = strstr(type, "gre");
320 ipsec_mech_set = false;
321 memset(&tnl_cfg, 0, sizeof tnl_cfg);
323 needs_dst_port = netdev_vport_needs_dst_port(dev_);
324 tnl_cfg.ipsec = strstr(type, "ipsec");
325 tnl_cfg.dont_fragment = true;
327 SMAP_FOR_EACH (node, args) {
328 if (!strcmp(node->key, "remote_ip")) {
329 struct in_addr in_addr;
330 if (lookup_ip(node->value, &in_addr)) {
331 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
333 tnl_cfg.ip_dst = in_addr.s_addr;
335 } else if (!strcmp(node->key, "local_ip")) {
336 struct in_addr in_addr;
337 if (lookup_ip(node->value, &in_addr)) {
338 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
340 tnl_cfg.ip_src = in_addr.s_addr;
342 } else if (!strcmp(node->key, "tos")) {
343 if (!strcmp(node->value, "inherit")) {
344 tnl_cfg.tos_inherit = true;
348 tos = strtol(node->value, &endptr, 0);
349 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
352 VLOG_WARN("%s: invalid TOS %s", name, node->value);
355 } else if (!strcmp(node->key, "ttl")) {
356 if (!strcmp(node->value, "inherit")) {
357 tnl_cfg.ttl_inherit = true;
359 tnl_cfg.ttl = atoi(node->value);
361 } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
362 tnl_cfg.dst_port = htons(atoi(node->value));
363 } else if (!strcmp(node->key, "csum") && has_csum) {
364 if (!strcmp(node->value, "true")) {
367 } else if (!strcmp(node->key, "df_default")) {
368 if (!strcmp(node->value, "false")) {
369 tnl_cfg.dont_fragment = false;
371 } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
372 if (smap_get(args, "certificate")) {
373 ipsec_mech_set = true;
375 const char *use_ssl_cert;
377 /* If the "use_ssl_cert" is true, then "certificate" and
378 * "private_key" will be pulled from the SSL table. The
379 * use of this option is strongly discouraged, since it
380 * will like be removed when multiple SSL configurations
381 * are supported by OVS.
383 use_ssl_cert = smap_get(args, "use_ssl_cert");
384 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
385 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
389 ipsec_mech_set = true;
391 } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
392 ipsec_mech_set = true;
393 } else if (tnl_cfg.ipsec
394 && (!strcmp(node->key, "certificate")
395 || !strcmp(node->key, "private_key")
396 || !strcmp(node->key, "use_ssl_cert"))) {
397 /* Ignore options not used by the netdev. */
398 } else if (!strcmp(node->key, "key") ||
399 !strcmp(node->key, "in_key") ||
400 !strcmp(node->key, "out_key")) {
401 /* Handled separately below. */
403 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
407 /* Add a default destination port for VXLAN if none specified. */
408 if (!strcmp(type, "vxlan") && !tnl_cfg.dst_port) {
409 tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
412 /* Add a default destination port for LISP if none specified. */
413 if (!strcmp(type, "lisp") && !tnl_cfg.dst_port) {
414 tnl_cfg.dst_port = htons(LISP_DST_PORT);
418 static pid_t pid = 0;
420 char *file_name = xasprintf("%s/%s", ovs_rundir(),
421 "ovs-monitor-ipsec.pid");
422 pid = read_pidfile(file_name);
427 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
432 if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
433 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
437 if (!ipsec_mech_set) {
438 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
444 if (!tnl_cfg.ip_dst) {
445 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
450 if (tnl_cfg.ip_src) {
451 if (ip_is_multicast(tnl_cfg.ip_dst)) {
452 VLOG_WARN("%s: remote_ip is multicast, ignoring local_ip", name);
458 tnl_cfg.ttl = DEFAULT_TTL;
461 tnl_cfg.in_key = parse_key(args, "in_key",
462 &tnl_cfg.in_key_present,
463 &tnl_cfg.in_key_flow);
465 tnl_cfg.out_key = parse_key(args, "out_key",
466 &tnl_cfg.out_key_present,
467 &tnl_cfg.out_key_flow);
469 dev->tnl_cfg = tnl_cfg;
470 netdev_vport_poll_notify(dev);
476 get_tunnel_config(struct netdev_dev *dev, struct smap *args)
478 const struct netdev_tunnel_config *tnl_cfg =
479 &netdev_dev_vport_cast(dev)->tnl_cfg;
481 if (tnl_cfg->ip_dst) {
482 smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(tnl_cfg->ip_dst));
485 if (tnl_cfg->ip_src) {
486 smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(tnl_cfg->ip_src));
489 if (tnl_cfg->in_key_flow && tnl_cfg->out_key_flow) {
490 smap_add(args, "key", "flow");
491 } else if (tnl_cfg->in_key_present && tnl_cfg->out_key_present
492 && tnl_cfg->in_key == tnl_cfg->out_key) {
493 smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg->in_key));
495 if (tnl_cfg->in_key_flow) {
496 smap_add(args, "in_key", "flow");
497 } else if (tnl_cfg->in_key_present) {
498 smap_add_format(args, "in_key", "%"PRIu64,
499 ntohll(tnl_cfg->in_key));
502 if (tnl_cfg->out_key_flow) {
503 smap_add(args, "out_key", "flow");
504 } else if (tnl_cfg->out_key_present) {
505 smap_add_format(args, "out_key", "%"PRIu64,
506 ntohll(tnl_cfg->out_key));
510 if (tnl_cfg->ttl_inherit) {
511 smap_add(args, "ttl", "inherit");
512 } else if (tnl_cfg->ttl != DEFAULT_TTL) {
513 smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg->ttl);
516 if (tnl_cfg->tos_inherit) {
517 smap_add(args, "tos", "inherit");
518 } else if (tnl_cfg->tos) {
519 smap_add_format(args, "tos", "0x%x", tnl_cfg->tos);
522 if (tnl_cfg->dst_port) {
523 uint16_t dst_port = ntohs(tnl_cfg->dst_port);
524 const char *type = netdev_dev_get_type(dev);
526 if ((!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
527 (!strcmp("lisp", type) && dst_port != LISP_DST_PORT)) {
528 smap_add_format(args, "dst_port", "%d", dst_port);
533 smap_add(args, "csum", "true");
536 if (!tnl_cfg->dont_fragment) {
537 smap_add(args, "df_default", "false");
543 /* Code specific to patch ports. */
546 netdev_vport_patch_peer(const struct netdev *netdev)
548 return netdev_vport_is_patch(netdev)
549 ? netdev_vport_get_dev(netdev)->peer
554 netdev_vport_inc_rx(const struct netdev *netdev,
555 const struct dpif_flow_stats *stats)
557 if (is_vport_class(netdev_dev_get_class(netdev_get_dev(netdev)))) {
558 struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
559 dev->stats.rx_packets += stats->n_packets;
560 dev->stats.rx_bytes += stats->n_bytes;
565 netdev_vport_inc_tx(const struct netdev *netdev,
566 const struct dpif_flow_stats *stats)
568 if (is_vport_class(netdev_dev_get_class(netdev_get_dev(netdev)))) {
569 struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
570 dev->stats.tx_packets += stats->n_packets;
571 dev->stats.tx_bytes += stats->n_bytes;
576 get_patch_config(struct netdev_dev *dev_, struct smap *args)
578 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
581 smap_add(args, "peer", dev->peer);
587 set_patch_config(struct netdev_dev *dev_, const struct smap *args)
589 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
590 const char *name = netdev_dev_get_name(dev_);
593 peer = smap_get(args, "peer");
595 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
599 if (smap_count(args) > 1) {
600 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
604 if (!strcmp(name, peer)) {
605 VLOG_ERR("%s: patch peer must not be self", name);
610 dev->peer = xstrdup(peer);
616 get_stats(const struct netdev *netdev, struct netdev_stats *stats)
618 struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
619 memcpy(stats, &dev->stats, sizeof *stats);
623 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \
624 GET_TUNNEL_CONFIG, GET_STATUS) \
629 netdev_vport_create, \
630 netdev_vport_destroy, \
636 netdev_vport_close, \
640 NULL, /* recv_wait */ \
644 NULL, /* send_wait */ \
646 netdev_vport_set_etheraddr, \
647 netdev_vport_get_etheraddr, \
648 NULL, /* get_mtu */ \
649 NULL, /* set_mtu */ \
650 NULL, /* get_ifindex */ \
651 NULL, /* get_carrier */ \
652 NULL, /* get_carrier_resets */ \
653 NULL, /* get_miimon */ \
655 NULL, /* set_stats */ \
657 NULL, /* get_features */ \
658 NULL, /* set_advertisements */ \
660 NULL, /* set_policing */ \
661 NULL, /* get_qos_types */ \
662 NULL, /* get_qos_capabilities */ \
663 NULL, /* get_qos */ \
664 NULL, /* set_qos */ \
665 NULL, /* get_queue */ \
666 NULL, /* set_queue */ \
667 NULL, /* delete_queue */ \
668 NULL, /* get_queue_stats */ \
669 NULL, /* dump_queues */ \
670 NULL, /* dump_queue_stats */ \
672 NULL, /* get_in4 */ \
673 NULL, /* set_in4 */ \
674 NULL, /* get_in6 */ \
675 NULL, /* add_router */ \
676 NULL, /* get_next_hop */ \
678 NULL, /* arp_lookup */ \
680 netdev_vport_update_flags, \
682 netdev_vport_change_seq
684 #define TUNNEL_CLASS(NAME, DPIF_PORT) \
686 { NAME, VPORT_FUNCTIONS(get_tunnel_config, \
688 get_netdev_tunnel_config, \
689 tunnel_get_status) }}
692 netdev_vport_tunnel_register(void)
694 static const struct vport_class vport_classes[] = {
695 TUNNEL_CLASS("gre", "gre_system"),
696 TUNNEL_CLASS("ipsec_gre", "gre_system"),
697 TUNNEL_CLASS("gre64", "gre64_system"),
698 TUNNEL_CLASS("ipsec_gre64", "gre64_system"),
699 TUNNEL_CLASS("vxlan", "vxlan_system"),
700 TUNNEL_CLASS("lisp", "lisp_system")
705 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
706 netdev_register_provider(&vport_classes[i].netdev_class);
711 netdev_vport_patch_register(void)
713 static const struct vport_class patch_class =
715 { "patch", VPORT_FUNCTIONS(get_patch_config,
719 netdev_register_provider(&patch_class.netdev_class);