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 #define VXLAN_DST_PORT 4789
45 #define LISP_DST_PORT 4341
47 #define DEFAULT_TTL 64
52 /* Protects all members below. */
53 struct ovs_mutex mutex
;
55 unsigned int change_seq
;
56 uint8_t etheraddr
[ETH_ADDR_LEN
];
57 struct netdev_stats stats
;
60 struct netdev_tunnel_config tnl_cfg
;
67 const char *dpif_port
;
68 struct netdev_class netdev_class
;
71 static int netdev_vport_construct(struct netdev
*);
72 static int get_patch_config(const struct netdev
*netdev
, struct smap
*args
);
73 static int get_tunnel_config(const struct netdev
*, struct smap
*args
);
74 static void netdev_vport_poll_notify(struct netdev_vport
*netdev
)
75 OVS_REQUIRES(netdev
->mutex
);
78 is_vport_class(const struct netdev_class
*class)
80 return class->construct
== netdev_vport_construct
;
83 static const struct vport_class
*
84 vport_class_cast(const struct netdev_class
*class)
86 ovs_assert(is_vport_class(class));
87 return CONTAINER_OF(class, struct vport_class
, netdev_class
);
90 static struct netdev_vport
*
91 netdev_vport_cast(const struct netdev
*netdev
)
93 ovs_assert(is_vport_class(netdev_get_class(netdev
)));
94 return CONTAINER_OF(netdev
, struct netdev_vport
, up
);
97 static const struct netdev_tunnel_config
*
98 get_netdev_tunnel_config(const struct netdev
*netdev
)
100 return &netdev_vport_cast(netdev
)->tnl_cfg
;
104 netdev_vport_is_patch(const struct netdev
*netdev
)
106 const struct netdev_class
*class = netdev_get_class(netdev
);
108 return class->get_config
== get_patch_config
;
112 netdev_vport_needs_dst_port(const struct netdev
*dev
)
114 const struct netdev_class
*class = netdev_get_class(dev
);
115 const char *type
= netdev_get_type(dev
);
117 return (class->get_config
== get_tunnel_config
&&
118 (!strcmp("vxlan", type
) || !strcmp("lisp", type
)));
122 netdev_vport_class_get_dpif_port(const struct netdev_class
*class)
124 return is_vport_class(class) ? vport_class_cast(class)->dpif_port
: NULL
;
128 netdev_vport_get_dpif_port(const struct netdev
*netdev
,
129 char namebuf
[], size_t bufsize
)
131 if (netdev_vport_needs_dst_port(netdev
)) {
132 const struct netdev_vport
*vport
= netdev_vport_cast(netdev
);
133 const char *type
= netdev_get_type(netdev
);
136 * Note: IFNAMSIZ is 16 bytes long. The maximum length of a VXLAN
137 * or LISP port name below is 15 or 14 bytes respectively. Still,
138 * assert here on the size of strlen(type) in case that changes
141 BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE
>= IFNAMSIZ
);
142 ovs_assert(strlen(type
) + 10 < IFNAMSIZ
);
143 snprintf(namebuf
, bufsize
, "%s_sys_%d", type
,
144 ntohs(vport
->tnl_cfg
.dst_port
));
147 const struct netdev_class
*class = netdev_get_class(netdev
);
148 const char *dpif_port
= netdev_vport_class_get_dpif_port(class);
149 return dpif_port
? dpif_port
: netdev_get_name(netdev
);
154 netdev_vport_get_dpif_port_strdup(const struct netdev
*netdev
)
156 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
158 return xstrdup(netdev_vport_get_dpif_port(netdev
, namebuf
,
162 static struct netdev
*
163 netdev_vport_alloc(void)
165 struct netdev_vport
*netdev
= xzalloc(sizeof *netdev
);
170 netdev_vport_construct(struct netdev
*netdev_
)
172 struct netdev_vport
*netdev
= netdev_vport_cast(netdev_
);
174 ovs_mutex_init(&netdev
->mutex
);
175 netdev
->change_seq
= 1;
176 eth_addr_random(netdev
->etheraddr
);
178 route_table_register();
184 netdev_vport_destruct(struct netdev
*netdev_
)
186 struct netdev_vport
*netdev
= netdev_vport_cast(netdev_
);
188 route_table_unregister();
190 ovs_mutex_destroy(&netdev
->mutex
);
194 netdev_vport_dealloc(struct netdev
*netdev_
)
196 struct netdev_vport
*netdev
= netdev_vport_cast(netdev_
);
201 netdev_vport_set_etheraddr(struct netdev
*netdev_
,
202 const uint8_t mac
[ETH_ADDR_LEN
])
204 struct netdev_vport
*netdev
= netdev_vport_cast(netdev_
);
206 ovs_mutex_lock(&netdev
->mutex
);
207 memcpy(netdev
->etheraddr
, mac
, ETH_ADDR_LEN
);
208 netdev_vport_poll_notify(netdev
);
209 ovs_mutex_unlock(&netdev
->mutex
);
215 netdev_vport_get_etheraddr(const struct netdev
*netdev_
,
216 uint8_t mac
[ETH_ADDR_LEN
])
218 struct netdev_vport
*netdev
= netdev_vport_cast(netdev_
);
220 ovs_mutex_lock(&netdev
->mutex
);
221 memcpy(mac
, netdev
->etheraddr
, ETH_ADDR_LEN
);
222 ovs_mutex_unlock(&netdev
->mutex
);
228 tunnel_get_status(const struct netdev
*netdev_
, struct smap
*smap
)
230 struct netdev_vport
*netdev
= netdev_vport_cast(netdev_
);
231 char iface
[IFNAMSIZ
];
234 ovs_mutex_lock(&netdev
->mutex
);
235 route
= netdev
->tnl_cfg
.ip_dst
;
236 ovs_mutex_unlock(&netdev
->mutex
);
238 if (route_table_get_name(route
, iface
)) {
239 struct netdev
*egress_netdev
;
241 smap_add(smap
, "tunnel_egress_iface", iface
);
243 if (!netdev_open(iface
, "system", &egress_netdev
)) {
244 smap_add(smap
, "tunnel_egress_iface_carrier",
245 netdev_get_carrier(egress_netdev
) ? "up" : "down");
246 netdev_close(egress_netdev
);
254 netdev_vport_update_flags(struct netdev
*netdev OVS_UNUSED
,
255 enum netdev_flags off
,
256 enum netdev_flags on OVS_UNUSED
,
257 enum netdev_flags
*old_flagsp
)
259 if (off
& (NETDEV_UP
| NETDEV_PROMISC
)) {
263 *old_flagsp
= NETDEV_UP
| NETDEV_PROMISC
;
268 netdev_vport_change_seq(const struct netdev
*netdev
)
270 return netdev_vport_cast(netdev
)->change_seq
;
274 netdev_vport_run(void)
280 netdev_vport_wait(void)
285 /* Helper functions. */
288 netdev_vport_poll_notify(struct netdev_vport
*ndv
)
291 if (!ndv
->change_seq
) {
296 /* Code specific to tunnel types. */
299 parse_key(const struct smap
*args
, const char *name
,
300 bool *present
, bool *flow
)
307 s
= smap_get(args
, name
);
309 s
= smap_get(args
, "key");
317 if (!strcmp(s
, "flow")) {
321 return htonll(strtoull(s
, NULL
, 0));
326 set_tunnel_config(struct netdev
*dev_
, const struct smap
*args
)
328 struct netdev_vport
*dev
= netdev_vport_cast(dev_
);
329 const char *name
= netdev_get_name(dev_
);
330 const char *type
= netdev_get_type(dev_
);
331 bool ipsec_mech_set
, needs_dst_port
, has_csum
;
332 struct netdev_tunnel_config tnl_cfg
;
333 struct smap_node
*node
;
335 has_csum
= strstr(type
, "gre");
336 ipsec_mech_set
= false;
337 memset(&tnl_cfg
, 0, sizeof tnl_cfg
);
339 needs_dst_port
= netdev_vport_needs_dst_port(dev_
);
340 tnl_cfg
.ipsec
= strstr(type
, "ipsec");
341 tnl_cfg
.dont_fragment
= true;
343 SMAP_FOR_EACH (node
, args
) {
344 if (!strcmp(node
->key
, "remote_ip")) {
345 struct in_addr in_addr
;
346 if (!strcmp(node
->value
, "flow")) {
347 tnl_cfg
.ip_dst_flow
= true;
348 tnl_cfg
.ip_dst
= htonl(0);
349 } else if (lookup_ip(node
->value
, &in_addr
)) {
350 VLOG_WARN("%s: bad %s 'remote_ip'", name
, type
);
351 } else if (ip_is_multicast(in_addr
.s_addr
)) {
352 VLOG_WARN("%s: multicast remote_ip="IP_FMT
" not allowed",
353 name
, IP_ARGS(in_addr
.s_addr
));
356 tnl_cfg
.ip_dst
= in_addr
.s_addr
;
358 } else if (!strcmp(node
->key
, "local_ip")) {
359 struct in_addr in_addr
;
360 if (!strcmp(node
->value
, "flow")) {
361 tnl_cfg
.ip_src_flow
= true;
362 tnl_cfg
.ip_src
= htonl(0);
363 } else if (lookup_ip(node
->value
, &in_addr
)) {
364 VLOG_WARN("%s: bad %s 'local_ip'", name
, type
);
366 tnl_cfg
.ip_src
= in_addr
.s_addr
;
368 } else if (!strcmp(node
->key
, "tos")) {
369 if (!strcmp(node
->value
, "inherit")) {
370 tnl_cfg
.tos_inherit
= true;
374 tos
= strtol(node
->value
, &endptr
, 0);
375 if (*endptr
== '\0' && tos
== (tos
& IP_DSCP_MASK
)) {
378 VLOG_WARN("%s: invalid TOS %s", name
, node
->value
);
381 } else if (!strcmp(node
->key
, "ttl")) {
382 if (!strcmp(node
->value
, "inherit")) {
383 tnl_cfg
.ttl_inherit
= true;
385 tnl_cfg
.ttl
= atoi(node
->value
);
387 } else if (!strcmp(node
->key
, "dst_port") && needs_dst_port
) {
388 tnl_cfg
.dst_port
= htons(atoi(node
->value
));
389 } else if (!strcmp(node
->key
, "csum") && has_csum
) {
390 if (!strcmp(node
->value
, "true")) {
393 } else if (!strcmp(node
->key
, "df_default")) {
394 if (!strcmp(node
->value
, "false")) {
395 tnl_cfg
.dont_fragment
= false;
397 } else if (!strcmp(node
->key
, "peer_cert") && tnl_cfg
.ipsec
) {
398 if (smap_get(args
, "certificate")) {
399 ipsec_mech_set
= true;
401 const char *use_ssl_cert
;
403 /* If the "use_ssl_cert" is true, then "certificate" and
404 * "private_key" will be pulled from the SSL table. The
405 * use of this option is strongly discouraged, since it
406 * will like be removed when multiple SSL configurations
407 * are supported by OVS.
409 use_ssl_cert
= smap_get(args
, "use_ssl_cert");
410 if (!use_ssl_cert
|| strcmp(use_ssl_cert
, "true")) {
411 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
415 ipsec_mech_set
= true;
417 } else if (!strcmp(node
->key
, "psk") && tnl_cfg
.ipsec
) {
418 ipsec_mech_set
= true;
419 } else if (tnl_cfg
.ipsec
420 && (!strcmp(node
->key
, "certificate")
421 || !strcmp(node
->key
, "private_key")
422 || !strcmp(node
->key
, "use_ssl_cert"))) {
423 /* Ignore options not used by the netdev. */
424 } else if (!strcmp(node
->key
, "key") ||
425 !strcmp(node
->key
, "in_key") ||
426 !strcmp(node
->key
, "out_key")) {
427 /* Handled separately below. */
429 VLOG_WARN("%s: unknown %s argument '%s'", name
, type
, node
->key
);
433 /* Add a default destination port for VXLAN if none specified. */
434 if (!strcmp(type
, "vxlan") && !tnl_cfg
.dst_port
) {
435 tnl_cfg
.dst_port
= htons(VXLAN_DST_PORT
);
438 /* Add a default destination port for LISP if none specified. */
439 if (!strcmp(type
, "lisp") && !tnl_cfg
.dst_port
) {
440 tnl_cfg
.dst_port
= htons(LISP_DST_PORT
);
444 static struct ovs_mutex mutex
= OVS_MUTEX_INITIALIZER
;
445 static pid_t pid
= 0;
447 ovs_mutex_lock(&mutex
);
449 char *file_name
= xasprintf("%s/%s", ovs_rundir(),
450 "ovs-monitor-ipsec.pid");
451 pid
= read_pidfile(file_name
);
454 ovs_mutex_unlock(&mutex
);
457 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
462 if (smap_get(args
, "peer_cert") && smap_get(args
, "psk")) {
463 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name
);
467 if (!ipsec_mech_set
) {
468 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
474 if (!tnl_cfg
.ip_dst
&& !tnl_cfg
.ip_dst_flow
) {
475 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
479 if (tnl_cfg
.ip_src_flow
&& !tnl_cfg
.ip_dst_flow
) {
480 VLOG_ERR("%s: %s type requires 'remote_ip=flow' with 'local_ip=flow'",
485 tnl_cfg
.ttl
= DEFAULT_TTL
;
488 tnl_cfg
.in_key
= parse_key(args
, "in_key",
489 &tnl_cfg
.in_key_present
,
490 &tnl_cfg
.in_key_flow
);
492 tnl_cfg
.out_key
= parse_key(args
, "out_key",
493 &tnl_cfg
.out_key_present
,
494 &tnl_cfg
.out_key_flow
);
496 ovs_mutex_lock(&dev
->mutex
);
497 dev
->tnl_cfg
= tnl_cfg
;
498 netdev_vport_poll_notify(dev
);
499 ovs_mutex_unlock(&dev
->mutex
);
505 get_tunnel_config(const struct netdev
*dev
, struct smap
*args
)
507 struct netdev_vport
*netdev
= netdev_vport_cast(dev
);
508 struct netdev_tunnel_config tnl_cfg
;
510 ovs_mutex_lock(&netdev
->mutex
);
511 tnl_cfg
= netdev
->tnl_cfg
;
512 ovs_mutex_unlock(&netdev
->mutex
);
514 if (tnl_cfg
.ip_dst
) {
515 smap_add_format(args
, "remote_ip", IP_FMT
, IP_ARGS(tnl_cfg
.ip_dst
));
516 } else if (tnl_cfg
.ip_dst_flow
) {
517 smap_add(args
, "remote_ip", "flow");
520 if (tnl_cfg
.ip_src
) {
521 smap_add_format(args
, "local_ip", IP_FMT
, IP_ARGS(tnl_cfg
.ip_src
));
522 } else if (tnl_cfg
.ip_src_flow
) {
523 smap_add(args
, "local_ip", "flow");
526 if (tnl_cfg
.in_key_flow
&& tnl_cfg
.out_key_flow
) {
527 smap_add(args
, "key", "flow");
528 } else if (tnl_cfg
.in_key_present
&& tnl_cfg
.out_key_present
529 && tnl_cfg
.in_key
== tnl_cfg
.out_key
) {
530 smap_add_format(args
, "key", "%"PRIu64
, ntohll(tnl_cfg
.in_key
));
532 if (tnl_cfg
.in_key_flow
) {
533 smap_add(args
, "in_key", "flow");
534 } else if (tnl_cfg
.in_key_present
) {
535 smap_add_format(args
, "in_key", "%"PRIu64
,
536 ntohll(tnl_cfg
.in_key
));
539 if (tnl_cfg
.out_key_flow
) {
540 smap_add(args
, "out_key", "flow");
541 } else if (tnl_cfg
.out_key_present
) {
542 smap_add_format(args
, "out_key", "%"PRIu64
,
543 ntohll(tnl_cfg
.out_key
));
547 if (tnl_cfg
.ttl_inherit
) {
548 smap_add(args
, "ttl", "inherit");
549 } else if (tnl_cfg
.ttl
!= DEFAULT_TTL
) {
550 smap_add_format(args
, "ttl", "%"PRIu8
, tnl_cfg
.ttl
);
553 if (tnl_cfg
.tos_inherit
) {
554 smap_add(args
, "tos", "inherit");
555 } else if (tnl_cfg
.tos
) {
556 smap_add_format(args
, "tos", "0x%x", tnl_cfg
.tos
);
559 if (tnl_cfg
.dst_port
) {
560 uint16_t dst_port
= ntohs(tnl_cfg
.dst_port
);
561 const char *type
= netdev_get_type(dev
);
563 if ((!strcmp("vxlan", type
) && dst_port
!= VXLAN_DST_PORT
) ||
564 (!strcmp("lisp", type
) && dst_port
!= LISP_DST_PORT
)) {
565 smap_add_format(args
, "dst_port", "%d", dst_port
);
570 smap_add(args
, "csum", "true");
573 if (!tnl_cfg
.dont_fragment
) {
574 smap_add(args
, "df_default", "false");
580 /* Code specific to patch ports. */
582 /* If 'netdev' is a patch port, returns the name of its peer as a malloc()'d
583 * string that the caller must free.
585 * If 'netdev' is not a patch port, returns NULL. */
587 netdev_vport_patch_peer(const struct netdev
*netdev_
)
591 if (netdev_vport_is_patch(netdev_
)) {
592 struct netdev_vport
*netdev
= netdev_vport_cast(netdev_
);
594 ovs_mutex_lock(&netdev
->mutex
);
596 peer
= xstrdup(netdev
->peer
);
598 ovs_mutex_unlock(&netdev
->mutex
);
605 netdev_vport_inc_rx(const struct netdev
*netdev
,
606 const struct dpif_flow_stats
*stats
)
608 if (is_vport_class(netdev_get_class(netdev
))) {
609 struct netdev_vport
*dev
= netdev_vport_cast(netdev
);
611 ovs_mutex_lock(&dev
->mutex
);
612 dev
->stats
.rx_packets
+= stats
->n_packets
;
613 dev
->stats
.rx_bytes
+= stats
->n_bytes
;
614 ovs_mutex_unlock(&dev
->mutex
);
619 netdev_vport_inc_tx(const struct netdev
*netdev
,
620 const struct dpif_flow_stats
*stats
)
622 if (is_vport_class(netdev_get_class(netdev
))) {
623 struct netdev_vport
*dev
= netdev_vport_cast(netdev
);
625 ovs_mutex_lock(&dev
->mutex
);
626 dev
->stats
.tx_packets
+= stats
->n_packets
;
627 dev
->stats
.tx_bytes
+= stats
->n_bytes
;
628 ovs_mutex_unlock(&dev
->mutex
);
633 get_patch_config(const struct netdev
*dev_
, struct smap
*args
)
635 struct netdev_vport
*dev
= netdev_vport_cast(dev_
);
637 ovs_mutex_lock(&dev
->mutex
);
639 smap_add(args
, "peer", dev
->peer
);
641 ovs_mutex_unlock(&dev
->mutex
);
647 set_patch_config(struct netdev
*dev_
, const struct smap
*args
)
649 struct netdev_vport
*dev
= netdev_vport_cast(dev_
);
650 const char *name
= netdev_get_name(dev_
);
653 peer
= smap_get(args
, "peer");
655 VLOG_ERR("%s: patch type requires valid 'peer' argument", name
);
659 if (smap_count(args
) > 1) {
660 VLOG_ERR("%s: patch type takes only a 'peer' argument", name
);
664 if (!strcmp(name
, peer
)) {
665 VLOG_ERR("%s: patch peer must not be self", name
);
669 ovs_mutex_lock(&dev
->mutex
);
671 dev
->peer
= xstrdup(peer
);
672 netdev_vport_poll_notify(dev
);
673 ovs_mutex_unlock(&dev
->mutex
);
679 get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
681 struct netdev_vport
*dev
= netdev_vport_cast(netdev
);
683 ovs_mutex_lock(&dev
->mutex
);
685 ovs_mutex_unlock(&dev
->mutex
);
690 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \
691 GET_TUNNEL_CONFIG, GET_STATUS) \
696 netdev_vport_alloc, \
697 netdev_vport_construct, \
698 netdev_vport_destruct, \
699 netdev_vport_dealloc, \
705 NULL, /* send_wait */ \
707 netdev_vport_set_etheraddr, \
708 netdev_vport_get_etheraddr, \
709 NULL, /* get_mtu */ \
710 NULL, /* set_mtu */ \
711 NULL, /* get_ifindex */ \
712 NULL, /* get_carrier */ \
713 NULL, /* get_carrier_resets */ \
714 NULL, /* get_miimon */ \
716 NULL, /* set_stats */ \
718 NULL, /* get_features */ \
719 NULL, /* set_advertisements */ \
721 NULL, /* set_policing */ \
722 NULL, /* get_qos_types */ \
723 NULL, /* get_qos_capabilities */ \
724 NULL, /* get_qos */ \
725 NULL, /* set_qos */ \
726 NULL, /* get_queue */ \
727 NULL, /* set_queue */ \
728 NULL, /* delete_queue */ \
729 NULL, /* get_queue_stats */ \
730 NULL, /* queue_dump_start */ \
731 NULL, /* queue_dump_next */ \
732 NULL, /* queue_dump_done */ \
733 NULL, /* dump_queue_stats */ \
735 NULL, /* get_in4 */ \
736 NULL, /* set_in4 */ \
737 NULL, /* get_in6 */ \
738 NULL, /* add_router */ \
739 NULL, /* get_next_hop */ \
741 NULL, /* arp_lookup */ \
743 netdev_vport_update_flags, \
745 netdev_vport_change_seq, \
747 NULL, /* rx_alloc */ \
748 NULL, /* rx_construct */ \
749 NULL, /* rx_destruct */ \
750 NULL, /* rx_dealloc */ \
751 NULL, /* rx_recv */ \
752 NULL, /* rx_wait */ \
755 #define TUNNEL_CLASS(NAME, DPIF_PORT) \
757 { NAME, VPORT_FUNCTIONS(get_tunnel_config, \
759 get_netdev_tunnel_config, \
760 tunnel_get_status) }}
763 netdev_vport_tunnel_register(void)
765 static const struct vport_class vport_classes
[] = {
766 TUNNEL_CLASS("gre", "gre_system"),
767 TUNNEL_CLASS("ipsec_gre", "gre_system"),
768 TUNNEL_CLASS("gre64", "gre64_system"),
769 TUNNEL_CLASS("ipsec_gre64", "gre64_system"),
770 TUNNEL_CLASS("vxlan", "vxlan_system"),
771 TUNNEL_CLASS("lisp", "lisp_system")
773 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
775 if (ovsthread_once_start(&once
)) {
778 for (i
= 0; i
< ARRAY_SIZE(vport_classes
); i
++) {
779 netdev_register_provider(&vport_classes
[i
].netdev_class
);
781 ovsthread_once_done(&once
);
786 netdev_vport_patch_register(void)
788 static const struct vport_class patch_class
=
790 { "patch", VPORT_FUNCTIONS(get_patch_config
,
794 netdev_register_provider(&patch_class
.netdev_class
);