]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-vport.c
netdev-linux, netdev-bsd: Make access to AF_INET socket thread-safe.
[mirror_ovs.git] / lib / netdev-vport.c
CommitLineData
777ece09 1/*
9284baa2 2 * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
777ece09
JG
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 *
6fcfff1b 10 * Unless required by applicable law or agreed to in writing, software
777ece09
JG
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>
2b9d6589
BP
18
19#include "netdev-vport.h"
20
777ece09
JG
21#include <errno.h>
22#include <fcntl.h>
ea83a2fc 23#include <sys/socket.h>
2b9d6589 24#include <net/if.h>
777ece09
JG
25#include <sys/ioctl.h>
26
b9298d3f 27#include "byte-order.h"
5059eff3
JP
28#include "daemon.h"
29#include "dirs.h"
0a740f48 30#include "dpif.h"
ea83a2fc
EJ
31#include "hash.h"
32#include "hmap.h"
777ece09 33#include "list.h"
2b9d6589 34#include "netdev-provider.h"
ea83a2fc 35#include "ofpbuf.h"
2b9d6589 36#include "packets.h"
a132aa96 37#include "route-table.h"
777ece09
JG
38#include "shash.h"
39#include "socket-util.h"
777ece09
JG
40#include "vlog.h"
41
d98e6007 42VLOG_DEFINE_THIS_MODULE(netdev_vport);
5136ce49 43
4f2abb7b 44#define VXLAN_DST_PORT 4789
a6ae068b
LJ
45#define LISP_DST_PORT 4341
46
f431bf7d
EJ
47#define DEFAULT_TTL 64
48
b5d57fc8
BP
49struct netdev_vport {
50 struct netdev up;
ac4d3bcb 51 unsigned int change_seq;
35b769cb 52 uint8_t etheraddr[ETH_ADDR_LEN];
b9ad7294 53 struct netdev_stats stats;
0a740f48
EJ
54
55 /* Tunnels. */
f431bf7d 56 struct netdev_tunnel_config tnl_cfg;
0a740f48
EJ
57
58 /* Patch Ports. */
0a740f48 59 char *peer;
2b9d6589
BP
60};
61
2b9d6589 62struct vport_class {
b9ad7294 63 const char *dpif_port;
c3827f61 64 struct netdev_class netdev_class;
2b9d6589
BP
65};
66
2b9d6589 67static int netdev_vport_create(const struct netdev_class *, const char *,
b5d57fc8
BP
68 struct netdev **);
69static int get_patch_config(const struct netdev *, struct smap *args);
70static int get_tunnel_config(const struct netdev *, struct smap *args);
71static void netdev_vport_poll_notify(struct netdev_vport *);
2b9d6589
BP
72
73static bool
74is_vport_class(const struct netdev_class *class)
777ece09 75{
2b9d6589
BP
76 return class->create == netdev_vport_create;
77}
777ece09 78
2b9d6589
BP
79static const struct vport_class *
80vport_class_cast(const struct netdev_class *class)
81{
cb22974d 82 ovs_assert(is_vport_class(class));
2b9d6589
BP
83 return CONTAINER_OF(class, struct vport_class, netdev_class);
84}
85
b5d57fc8
BP
86static struct netdev_vport *
87netdev_vport_cast(const struct netdev *netdev)
2b9d6589 88{
b5d57fc8
BP
89 ovs_assert(is_vport_class(netdev_get_class(netdev)));
90 return CONTAINER_OF(netdev, struct netdev_vport, up);
df67d7ae
EJ
91}
92
f431bf7d 93static const struct netdev_tunnel_config *
b5d57fc8 94get_netdev_tunnel_config(const struct netdev *netdev)
f431bf7d 95{
b5d57fc8 96 return &netdev_vport_cast(netdev)->tnl_cfg;
f431bf7d
EJ
97}
98
0a740f48
EJ
99bool
100netdev_vport_is_patch(const struct netdev *netdev)
101{
b5d57fc8 102 const struct netdev_class *class = netdev_get_class(netdev);
f18a39b7 103
c060c4cf 104 return class->get_config == get_patch_config;
0a740f48
EJ
105}
106
56b11f0b 107static bool
b5d57fc8 108netdev_vport_needs_dst_port(const struct netdev *dev)
56b11f0b 109{
b5d57fc8
BP
110 const struct netdev_class *class = netdev_get_class(dev);
111 const char *type = netdev_get_type(dev);
56b11f0b 112
a6ae068b
LJ
113 return (class->get_config == get_tunnel_config &&
114 (!strcmp("vxlan", type) || !strcmp("lisp", type)));
56b11f0b
KM
115}
116
94a53842
AW
117const char *
118netdev_vport_class_get_dpif_port(const struct netdev_class *class)
119{
120 return is_vport_class(class) ? vport_class_cast(class)->dpif_port : NULL;
121}
122
de281153 123const char *
3aa30359
BP
124netdev_vport_get_dpif_port(const struct netdev *netdev,
125 char namebuf[], size_t bufsize)
de281153 126{
b5d57fc8
BP
127 if (netdev_vport_needs_dst_port(netdev)) {
128 const struct netdev_vport *vport = netdev_vport_cast(netdev);
129 const char *type = netdev_get_type(netdev);
56b11f0b
KM
130
131 /*
132 * Note: IFNAMSIZ is 16 bytes long. The maximum length of a VXLAN
a6ae068b
LJ
133 * or LISP port name below is 15 or 14 bytes respectively. Still,
134 * assert here on the size of strlen(type) in case that changes
135 * in the future.
56b11f0b 136 */
3aa30359 137 BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ);
56b11f0b 138 ovs_assert(strlen(type) + 10 < IFNAMSIZ);
3aa30359 139 snprintf(namebuf, bufsize, "%s_sys_%d", type,
56b11f0b 140 ntohs(vport->tnl_cfg.dst_port));
3aa30359 141 return namebuf;
56b11f0b 142 } else {
b5d57fc8 143 const struct netdev_class *class = netdev_get_class(netdev);
b1612a86
BP
144 const char *dpif_port = netdev_vport_class_get_dpif_port(class);
145 return dpif_port ? dpif_port : netdev_get_name(netdev);
56b11f0b 146 }
2b9d6589 147}
777ece09 148
3aa30359
BP
149char *
150netdev_vport_get_dpif_port_strdup(const struct netdev *netdev)
151{
152 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
153
154 return xstrdup(netdev_vport_get_dpif_port(netdev, namebuf,
155 sizeof namebuf));
156}
157
2b9d6589 158static int
c3827f61 159netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
b5d57fc8 160 struct netdev **netdevp)
2b9d6589 161{
b5d57fc8 162 struct netdev_vport *dev;
6d9e6eb4 163
f431bf7d 164 dev = xzalloc(sizeof *dev);
b5d57fc8 165 netdev_init(&dev->up, name, netdev_class);
de5cdb90 166 dev->change_seq = 1;
35b769cb 167 eth_addr_random(dev->etheraddr);
6d9e6eb4 168
b5d57fc8 169 *netdevp = &dev->up;
de5cdb90 170 route_table_register();
6d9e6eb4 171
de5cdb90 172 return 0;
777ece09
JG
173}
174
2b9d6589 175static void
b5d57fc8 176netdev_vport_destroy(struct netdev *netdev_)
2b9d6589 177{
b5d57fc8 178 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
2b9d6589 179
a132aa96 180 route_table_unregister();
b5d57fc8 181 free(netdev->peer);
2b9d6589
BP
182 free(netdev);
183}
184
2b9d6589 185static int
b5d57fc8 186netdev_vport_set_etheraddr(struct netdev *netdev_,
777ece09
JG
187 const uint8_t mac[ETH_ADDR_LEN])
188{
b5d57fc8
BP
189 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
190 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
191 netdev_vport_poll_notify(netdev);
35b769cb 192 return 0;
777ece09
JG
193}
194
2b9d6589 195static int
777ece09
JG
196netdev_vport_get_etheraddr(const struct netdev *netdev,
197 uint8_t mac[ETH_ADDR_LEN])
198{
b5d57fc8 199 memcpy(mac, netdev_vport_cast(netdev)->etheraddr, ETH_ADDR_LEN);
35b769cb 200 return 0;
777ece09
JG
201}
202
ea763e0e 203static int
275707c3 204tunnel_get_status(const struct netdev *netdev, struct smap *smap)
ea763e0e 205{
3dea0874 206 char iface[IFNAMSIZ];
275707c3 207 ovs_be32 route;
ea763e0e 208
b5d57fc8 209 route = netdev_vport_cast(netdev)->tnl_cfg.ip_dst;
275707c3 210 if (route_table_get_name(route, iface)) {
a404826e
AE
211 struct netdev *egress_netdev;
212
79f1cbe9 213 smap_add(smap, "tunnel_egress_iface", iface);
a404826e 214
18812dff 215 if (!netdev_open(iface, "system", &egress_netdev)) {
79f1cbe9
EJ
216 smap_add(smap, "tunnel_egress_iface_carrier",
217 netdev_get_carrier(egress_netdev) ? "up" : "down");
a404826e
AE
218 netdev_close(egress_netdev);
219 }
ea763e0e
EJ
220 }
221
222 return 0;
223}
224
2b9d6589 225static int
b5d57fc8
BP
226netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
227 enum netdev_flags off,
228 enum netdev_flags on OVS_UNUSED,
229 enum netdev_flags *old_flagsp)
777ece09
JG
230{
231 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
232 return EOPNOTSUPP;
233 }
234
235 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
236 return 0;
237}
238
ac4d3bcb
EJ
239static unsigned int
240netdev_vport_change_seq(const struct netdev *netdev)
241{
b5d57fc8 242 return netdev_vport_cast(netdev)->change_seq;
ac4d3bcb
EJ
243}
244
ea83a2fc
EJ
245static void
246netdev_vport_run(void)
247{
a132aa96 248 route_table_run();
ea83a2fc
EJ
249}
250
251static void
252netdev_vport_wait(void)
253{
a132aa96 254 route_table_wait();
ea83a2fc
EJ
255}
256\f
2b9d6589 257/* Helper functions. */
777ece09 258
2b9d6589 259static void
b5d57fc8 260netdev_vport_poll_notify(struct netdev_vport *ndv)
777ece09 261{
ac4d3bcb
EJ
262 ndv->change_seq++;
263 if (!ndv->change_seq) {
264 ndv->change_seq++;
265 }
777ece09 266}
2b9d6589 267\f
0a740f48 268/* Code specific to tunnel types. */
2b9d6589 269
f431bf7d
EJ
270static ovs_be64
271parse_key(const struct smap *args, const char *name,
272 bool *present, bool *flow)
c19e6535
BP
273{
274 const char *s;
275
f431bf7d
EJ
276 *present = false;
277 *flow = false;
278
79f1cbe9 279 s = smap_get(args, name);
c19e6535 280 if (!s) {
79f1cbe9 281 s = smap_get(args, "key");
c19e6535 282 if (!s) {
f431bf7d 283 return 0;
c19e6535
BP
284 }
285 }
286
f431bf7d
EJ
287 *present = true;
288
c19e6535 289 if (!strcmp(s, "flow")) {
f431bf7d
EJ
290 *flow = true;
291 return 0;
c19e6535 292 } else {
f431bf7d 293 return htonll(strtoull(s, NULL, 0));
c19e6535
BP
294 }
295}
296
2b9d6589 297static int
b5d57fc8 298set_tunnel_config(struct netdev *dev_, const struct smap *args)
2b9d6589 299{
b5d57fc8
BP
300 struct netdev_vport *dev = netdev_vport_cast(dev_);
301 const char *name = netdev_get_name(dev_);
302 const char *type = netdev_get_type(dev_);
f431bf7d
EJ
303 bool ipsec_mech_set, needs_dst_port, has_csum;
304 struct netdev_tunnel_config tnl_cfg;
79f1cbe9 305 struct smap_node *node;
f431bf7d 306
f431bf7d
EJ
307 has_csum = strstr(type, "gre");
308 ipsec_mech_set = false;
309 memset(&tnl_cfg, 0, sizeof tnl_cfg);
2b9d6589 310
a6ae068b 311 needs_dst_port = netdev_vport_needs_dst_port(dev_);
f431bf7d 312 tnl_cfg.ipsec = strstr(type, "ipsec");
f431bf7d 313 tnl_cfg.dont_fragment = true;
e16a28b5 314
79f1cbe9
EJ
315 SMAP_FOR_EACH (node, args) {
316 if (!strcmp(node->key, "remote_ip")) {
2b9d6589 317 struct in_addr in_addr;
0ad90c84
JR
318 if (!strcmp(node->value, "flow")) {
319 tnl_cfg.ip_dst_flow = true;
320 tnl_cfg.ip_dst = htonl(0);
321 } else if (lookup_ip(node->value, &in_addr)) {
c3827f61 322 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
85c9de19
PS
323 } else if (ip_is_multicast(in_addr.s_addr)) {
324 VLOG_WARN("%s: multicast remote_ip="IP_FMT" not allowed",
325 name, IP_ARGS(in_addr.s_addr));
326 return EINVAL;
2b9d6589 327 } else {
f431bf7d 328 tnl_cfg.ip_dst = in_addr.s_addr;
2b9d6589 329 }
79f1cbe9 330 } else if (!strcmp(node->key, "local_ip")) {
2b9d6589 331 struct in_addr in_addr;
0ad90c84
JR
332 if (!strcmp(node->value, "flow")) {
333 tnl_cfg.ip_src_flow = true;
334 tnl_cfg.ip_src = htonl(0);
335 } else if (lookup_ip(node->value, &in_addr)) {
c3827f61 336 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
2b9d6589 337 } else {
f431bf7d 338 tnl_cfg.ip_src = in_addr.s_addr;
2b9d6589 339 }
79f1cbe9
EJ
340 } else if (!strcmp(node->key, "tos")) {
341 if (!strcmp(node->value, "inherit")) {
f431bf7d 342 tnl_cfg.tos_inherit = true;
2b9d6589 343 } else {
3fca7064
PS
344 char *endptr;
345 int tos;
79f1cbe9 346 tos = strtol(node->value, &endptr, 0);
91aff446 347 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
f431bf7d 348 tnl_cfg.tos = tos;
91aff446
BP
349 } else {
350 VLOG_WARN("%s: invalid TOS %s", name, node->value);
3fca7064 351 }
2b9d6589 352 }
79f1cbe9
EJ
353 } else if (!strcmp(node->key, "ttl")) {
354 if (!strcmp(node->value, "inherit")) {
f431bf7d 355 tnl_cfg.ttl_inherit = true;
2b9d6589 356 } else {
f431bf7d 357 tnl_cfg.ttl = atoi(node->value);
2b9d6589 358 }
79f827fa 359 } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
f431bf7d 360 tnl_cfg.dst_port = htons(atoi(node->value));
f431bf7d 361 } else if (!strcmp(node->key, "csum") && has_csum) {
79f1cbe9 362 if (!strcmp(node->value, "true")) {
f431bf7d 363 tnl_cfg.csum = true;
2b9d6589 364 }
79f1cbe9
EJ
365 } else if (!strcmp(node->key, "df_default")) {
366 if (!strcmp(node->value, "false")) {
f431bf7d 367 tnl_cfg.dont_fragment = false;
66409d1b 368 }
f431bf7d 369 } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
79f1cbe9 370 if (smap_get(args, "certificate")) {
3c52fa7b
JP
371 ipsec_mech_set = true;
372 } else {
ef7ee76a
JP
373 const char *use_ssl_cert;
374
375 /* If the "use_ssl_cert" is true, then "certificate" and
376 * "private_key" will be pulled from the SSL table. The
377 * use of this option is strongly discouraged, since it
378 * will like be removed when multiple SSL configurations
379 * are supported by OVS.
380 */
79f1cbe9 381 use_ssl_cert = smap_get(args, "use_ssl_cert");
ef7ee76a 382 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
8283e514
JP
383 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
384 name);
b9ad7294 385 return EINVAL;
ef7ee76a
JP
386 }
387 ipsec_mech_set = true;
3c52fa7b 388 }
f431bf7d 389 } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
2b9d6589 390 ipsec_mech_set = true;
f431bf7d 391 } else if (tnl_cfg.ipsec
79f1cbe9
EJ
392 && (!strcmp(node->key, "certificate")
393 || !strcmp(node->key, "private_key")
394 || !strcmp(node->key, "use_ssl_cert"))) {
3c52fa7b 395 /* Ignore options not used by the netdev. */
79f1cbe9
EJ
396 } else if (!strcmp(node->key, "key") ||
397 !strcmp(node->key, "in_key") ||
398 !strcmp(node->key, "out_key")) {
c19e6535 399 /* Handled separately below. */
2b9d6589 400 } else {
79f1cbe9 401 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
2b9d6589
BP
402 }
403 }
404
79f827fa 405 /* Add a default destination port for VXLAN if none specified. */
a6ae068b 406 if (!strcmp(type, "vxlan") && !tnl_cfg.dst_port) {
f431bf7d 407 tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
79f827fa
KM
408 }
409
a6ae068b
LJ
410 /* Add a default destination port for LISP if none specified. */
411 if (!strcmp(type, "lisp") && !tnl_cfg.dst_port) {
412 tnl_cfg.dst_port = htons(LISP_DST_PORT);
413 }
414
f431bf7d 415 if (tnl_cfg.ipsec) {
9366380a 416 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
2a586a5c 417 static pid_t pid = 0;
6027d03d 418
9366380a 419 ovs_mutex_lock(&mutex);
900f7601 420 if (pid <= 0) {
2a586a5c
AS
421 char *file_name = xasprintf("%s/%s", ovs_rundir(),
422 "ovs-monitor-ipsec.pid");
423 pid = read_pidfile(file_name);
424 free(file_name);
425 }
9366380a 426 ovs_mutex_unlock(&mutex);
2a586a5c 427
e7009c36 428 if (pid < 0) {
8283e514
JP
429 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
430 name);
b9ad7294 431 return EINVAL;
e7009c36 432 }
5059eff3 433
79f1cbe9 434 if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
8283e514 435 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
b9ad7294 436 return EINVAL;
3c52fa7b
JP
437 }
438
439 if (!ipsec_mech_set) {
8283e514
JP
440 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
441 name);
b9ad7294 442 return EINVAL;
3c52fa7b 443 }
2b9d6589
BP
444 }
445
0ad90c84 446 if (!tnl_cfg.ip_dst && !tnl_cfg.ip_dst_flow) {
8283e514
JP
447 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
448 name, type);
b9ad7294 449 return EINVAL;
2b9d6589 450 }
0ad90c84
JR
451 if (tnl_cfg.ip_src_flow && !tnl_cfg.ip_dst_flow) {
452 VLOG_ERR("%s: %s type requires 'remote_ip=flow' with 'local_ip=flow'",
453 name, type);
454 return EINVAL;
455 }
f431bf7d
EJ
456 if (!tnl_cfg.ttl) {
457 tnl_cfg.ttl = DEFAULT_TTL;
458 }
459
460 tnl_cfg.in_key = parse_key(args, "in_key",
461 &tnl_cfg.in_key_present,
462 &tnl_cfg.in_key_flow);
f431bf7d
EJ
463
464 tnl_cfg.out_key = parse_key(args, "out_key",
465 &tnl_cfg.out_key_present,
466 &tnl_cfg.out_key_flow);
2b9d6589 467
0a740f48 468 dev->tnl_cfg = tnl_cfg;
b9ad7294 469 netdev_vport_poll_notify(dev);
f431bf7d 470
c19e6535
BP
471 return 0;
472}
473
2b9d6589 474static int
b5d57fc8 475get_tunnel_config(const struct netdev *dev, struct smap *args)
6d9e6eb4 476{
b9ad7294 477 const struct netdev_tunnel_config *tnl_cfg =
b5d57fc8 478 &netdev_vport_cast(dev)->tnl_cfg;
6d9e6eb4 479
b9ad7294
EJ
480 if (tnl_cfg->ip_dst) {
481 smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(tnl_cfg->ip_dst));
0ad90c84
JR
482 } else if (tnl_cfg->ip_dst_flow) {
483 smap_add(args, "remote_ip", "flow");
0a740f48
EJ
484 }
485
b9ad7294
EJ
486 if (tnl_cfg->ip_src) {
487 smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(tnl_cfg->ip_src));
0ad90c84
JR
488 } else if (tnl_cfg->ip_src_flow) {
489 smap_add(args, "local_ip", "flow");
7f804ea5 490 }
c19e6535 491
b9ad7294 492 if (tnl_cfg->in_key_flow && tnl_cfg->out_key_flow) {
6d9e6eb4 493 smap_add(args, "key", "flow");
b9ad7294
EJ
494 } else if (tnl_cfg->in_key_present && tnl_cfg->out_key_present
495 && tnl_cfg->in_key == tnl_cfg->out_key) {
496 smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg->in_key));
6d9e6eb4 497 } else {
b9ad7294
EJ
498 if (tnl_cfg->in_key_flow) {
499 smap_add(args, "in_key", "flow");
500 } else if (tnl_cfg->in_key_present) {
501 smap_add_format(args, "in_key", "%"PRIu64,
502 ntohll(tnl_cfg->in_key));
503 }
6d9e6eb4 504
b9ad7294
EJ
505 if (tnl_cfg->out_key_flow) {
506 smap_add(args, "out_key", "flow");
507 } else if (tnl_cfg->out_key_present) {
508 smap_add_format(args, "out_key", "%"PRIu64,
509 ntohll(tnl_cfg->out_key));
6d9e6eb4
BP
510 }
511 }
512
b9ad7294 513 if (tnl_cfg->ttl_inherit) {
62827e6a 514 smap_add(args, "ttl", "inherit");
b9ad7294
EJ
515 } else if (tnl_cfg->ttl != DEFAULT_TTL) {
516 smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg->ttl);
c19e6535
BP
517 }
518
b9ad7294 519 if (tnl_cfg->tos_inherit) {
6d9e6eb4 520 smap_add(args, "tos", "inherit");
b9ad7294
EJ
521 } else if (tnl_cfg->tos) {
522 smap_add_format(args, "tos", "0x%x", tnl_cfg->tos);
6d9e6eb4
BP
523 }
524
b9ad7294
EJ
525 if (tnl_cfg->dst_port) {
526 uint16_t dst_port = ntohs(tnl_cfg->dst_port);
b5d57fc8 527 const char *type = netdev_get_type(dev);
9eeb949b
KM
528
529 if ((!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
530 (!strcmp("lisp", type) && dst_port != LISP_DST_PORT)) {
79f827fa
KM
531 smap_add_format(args, "dst_port", "%d", dst_port);
532 }
533 }
534
b9ad7294 535 if (tnl_cfg->csum) {
6d9e6eb4
BP
536 smap_add(args, "csum", "true");
537 }
8a9ff93a 538
b9ad7294 539 if (!tnl_cfg->dont_fragment) {
66409d1b
AE
540 smap_add(args, "df_default", "false");
541 }
6d9e6eb4
BP
542
543 return 0;
544}
0a740f48
EJ
545\f
546/* Code specific to patch ports. */
547
548const char *
549netdev_vport_patch_peer(const struct netdev *netdev)
550{
b5d57fc8
BP
551 return (netdev_vport_is_patch(netdev)
552 ? netdev_vport_cast(netdev)->peer
553 : NULL);
0a740f48
EJ
554}
555
556void
b9ad7294 557netdev_vport_inc_rx(const struct netdev *netdev,
0a740f48
EJ
558 const struct dpif_flow_stats *stats)
559{
b5d57fc8
BP
560 if (is_vport_class(netdev_get_class(netdev))) {
561 struct netdev_vport *dev = netdev_vport_cast(netdev);
0a740f48
EJ
562 dev->stats.rx_packets += stats->n_packets;
563 dev->stats.rx_bytes += stats->n_bytes;
564 }
565}
566
567void
b9ad7294
EJ
568netdev_vport_inc_tx(const struct netdev *netdev,
569 const struct dpif_flow_stats *stats)
0a740f48 570{
b5d57fc8
BP
571 if (is_vport_class(netdev_get_class(netdev))) {
572 struct netdev_vport *dev = netdev_vport_cast(netdev);
0a740f48
EJ
573 dev->stats.tx_packets += stats->n_packets;
574 dev->stats.tx_bytes += stats->n_bytes;
575 }
576}
577
578static int
b5d57fc8 579get_patch_config(const struct netdev *dev_, struct smap *args)
0a740f48 580{
b5d57fc8 581 struct netdev_vport *dev = netdev_vport_cast(dev_);
0a740f48
EJ
582
583 if (dev->peer) {
584 smap_add(args, "peer", dev->peer);
585 }
586 return 0;
587}
6d9e6eb4
BP
588
589static int
b5d57fc8 590set_patch_config(struct netdev *dev_, const struct smap *args)
2b9d6589 591{
b5d57fc8
BP
592 struct netdev_vport *dev = netdev_vport_cast(dev_);
593 const char *name = netdev_get_name(dev_);
2b9d6589
BP
594 const char *peer;
595
79f1cbe9 596 peer = smap_get(args, "peer");
2b9d6589 597 if (!peer) {
8283e514 598 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
2b9d6589
BP
599 return EINVAL;
600 }
601
79f1cbe9 602 if (smap_count(args) > 1) {
8283e514 603 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
2b9d6589
BP
604 return EINVAL;
605 }
606
2b9d6589 607 if (!strcmp(name, peer)) {
8283e514 608 VLOG_ERR("%s: patch peer must not be self", name);
2b9d6589
BP
609 return EINVAL;
610 }
611
0a740f48
EJ
612 free(dev->peer);
613 dev->peer = xstrdup(peer);
6cbbf4fa 614 netdev_vport_poll_notify(dev);
2b9d6589
BP
615 return 0;
616}
6d9e6eb4
BP
617
618static int
b9ad7294 619get_stats(const struct netdev *netdev, struct netdev_stats *stats)
0a740f48 620{
b5d57fc8 621 struct netdev_vport *dev = netdev_vport_cast(netdev);
0a740f48 622 memcpy(stats, &dev->stats, sizeof *stats);
6d9e6eb4
BP
623 return 0;
624}
2b9d6589 625\f
0a740f48 626#define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \
b9ad7294 627 GET_TUNNEL_CONFIG, GET_STATUS) \
b46ccdf5 628 NULL, \
ea83a2fc
EJ
629 netdev_vport_run, \
630 netdev_vport_wait, \
2b9d6589
BP
631 \
632 netdev_vport_create, \
633 netdev_vport_destroy, \
0a740f48
EJ
634 GET_CONFIG, \
635 SET_CONFIG, \
f431bf7d 636 GET_TUNNEL_CONFIG, \
2b9d6589 637 \
796223f5 638 NULL, /* rx_open */ \
2b9d6589 639 \
552e20d0 640 NULL, /* send */ \
2b9d6589
BP
641 NULL, /* send_wait */ \
642 \
643 netdev_vport_set_etheraddr, \
644 netdev_vport_get_etheraddr, \
14622f22
BP
645 NULL, /* get_mtu */ \
646 NULL, /* set_mtu */ \
2b9d6589 647 NULL, /* get_ifindex */ \
85da620e 648 NULL, /* get_carrier */ \
65c3058c 649 NULL, /* get_carrier_resets */ \
63331829 650 NULL, /* get_miimon */ \
b9ad7294 651 get_stats, \
2f31a822 652 NULL, /* set_stats */ \
2b9d6589
BP
653 \
654 NULL, /* get_features */ \
655 NULL, /* set_advertisements */ \
2b9d6589
BP
656 \
657 NULL, /* set_policing */ \
658 NULL, /* get_qos_types */ \
659 NULL, /* get_qos_capabilities */ \
660 NULL, /* get_qos */ \
661 NULL, /* set_qos */ \
662 NULL, /* get_queue */ \
663 NULL, /* set_queue */ \
664 NULL, /* delete_queue */ \
665 NULL, /* get_queue_stats */ \
666 NULL, /* dump_queues */ \
667 NULL, /* dump_queue_stats */ \
668 \
669 NULL, /* get_in4 */ \
670 NULL, /* set_in4 */ \
671 NULL, /* get_in6 */ \
672 NULL, /* add_router */ \
673 NULL, /* get_next_hop */ \
ea763e0e 674 GET_STATUS, \
2b9d6589
BP
675 NULL, /* arp_lookup */ \
676 \
677 netdev_vport_update_flags, \
678 \
ac4d3bcb 679 netdev_vport_change_seq
2b9d6589 680
c060c4cf
EJ
681#define TUNNEL_CLASS(NAME, DPIF_PORT) \
682 { DPIF_PORT, \
0a740f48
EJ
683 { NAME, VPORT_FUNCTIONS(get_tunnel_config, \
684 set_tunnel_config, \
685 get_netdev_tunnel_config, \
0a740f48 686 tunnel_get_status) }}
db078f85 687
2b9d6589 688void
c060c4cf 689netdev_vport_tunnel_register(void)
2b9d6589 690{
c3827f61 691 static const struct vport_class vport_classes[] = {
c060c4cf
EJ
692 TUNNEL_CLASS("gre", "gre_system"),
693 TUNNEL_CLASS("ipsec_gre", "gre_system"),
694 TUNNEL_CLASS("gre64", "gre64_system"),
695 TUNNEL_CLASS("ipsec_gre64", "gre64_system"),
a6ae068b
LJ
696 TUNNEL_CLASS("vxlan", "vxlan_system"),
697 TUNNEL_CLASS("lisp", "lisp_system")
c3827f61 698 };
7c54c27f 699 static bool inited;
c3827f61
BP
700
701 int i;
702
7c54c27f
BP
703 if (!inited) {
704 inited = true;
705 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
706 netdev_register_provider(&vport_classes[i].netdev_class);
707 }
c3827f61 708 }
2b9d6589 709}
c060c4cf
EJ
710
711void
712netdev_vport_patch_register(void)
713{
714 static const struct vport_class patch_class =
715 { NULL,
716 { "patch", VPORT_FUNCTIONS(get_patch_config,
717 set_patch_config,
718 NULL,
719 NULL) }};
720 netdev_register_provider(&patch_class.netdev_class);
721}