]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-vport.c
datapath: Factor out allocation and verification of actions.
[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"
41ca1e0a 37#include "poll-loop.h"
a132aa96 38#include "route-table.h"
777ece09
JG
39#include "shash.h"
40#include "socket-util.h"
777ece09
JG
41#include "vlog.h"
42
d98e6007 43VLOG_DEFINE_THIS_MODULE(netdev_vport);
5136ce49 44
4f2abb7b 45#define VXLAN_DST_PORT 4789
a6ae068b
LJ
46#define LISP_DST_PORT 4341
47
f431bf7d
EJ
48#define DEFAULT_TTL 64
49
b5d57fc8
BP
50struct netdev_vport {
51 struct netdev up;
86383816
BP
52
53 /* Protects all members below. */
54 struct ovs_mutex mutex;
55
35b769cb 56 uint8_t etheraddr[ETH_ADDR_LEN];
b9ad7294 57 struct netdev_stats stats;
0a740f48
EJ
58
59 /* Tunnels. */
f431bf7d 60 struct netdev_tunnel_config tnl_cfg;
41ca1e0a
AW
61 char egress_iface[IFNAMSIZ];
62 bool carrier_status;
0a740f48
EJ
63
64 /* Patch Ports. */
0a740f48 65 char *peer;
2b9d6589
BP
66};
67
2b9d6589 68struct vport_class {
b9ad7294 69 const char *dpif_port;
c3827f61 70 struct netdev_class netdev_class;
2b9d6589
BP
71};
72
41ca1e0a
AW
73/* Last read of the route-table's change number. */
74static uint64_t rt_change_seqno;
75
9dc63482 76static int netdev_vport_construct(struct netdev *);
86383816 77static int get_patch_config(const struct netdev *netdev, struct smap *args);
b5d57fc8 78static int get_tunnel_config(const struct netdev *, struct smap *args);
41ca1e0a 79static bool tunnel_check_status_change__(struct netdev_vport *);
2b9d6589
BP
80
81static bool
82is_vport_class(const struct netdev_class *class)
777ece09 83{
9dc63482 84 return class->construct == netdev_vport_construct;
2b9d6589 85}
777ece09 86
41ca1e0a
AW
87bool
88netdev_vport_is_vport_class(const struct netdev_class *class)
89{
90 return is_vport_class(class);
91}
92
2b9d6589
BP
93static const struct vport_class *
94vport_class_cast(const struct netdev_class *class)
95{
cb22974d 96 ovs_assert(is_vport_class(class));
2b9d6589
BP
97 return CONTAINER_OF(class, struct vport_class, netdev_class);
98}
99
b5d57fc8
BP
100static struct netdev_vport *
101netdev_vport_cast(const struct netdev *netdev)
2b9d6589 102{
b5d57fc8
BP
103 ovs_assert(is_vport_class(netdev_get_class(netdev)));
104 return CONTAINER_OF(netdev, struct netdev_vport, up);
df67d7ae
EJ
105}
106
f431bf7d 107static const struct netdev_tunnel_config *
b5d57fc8 108get_netdev_tunnel_config(const struct netdev *netdev)
f431bf7d 109{
b5d57fc8 110 return &netdev_vport_cast(netdev)->tnl_cfg;
f431bf7d
EJ
111}
112
0a740f48
EJ
113bool
114netdev_vport_is_patch(const struct netdev *netdev)
115{
b5d57fc8 116 const struct netdev_class *class = netdev_get_class(netdev);
f18a39b7 117
c060c4cf 118 return class->get_config == get_patch_config;
0a740f48
EJ
119}
120
a6363cfd
LJ
121bool
122netdev_vport_is_layer3(const struct netdev *dev)
123{
124 const char *type = netdev_get_type(dev);
125
126 return (!strcmp("lisp", type));
127}
128
56b11f0b 129static bool
b5d57fc8 130netdev_vport_needs_dst_port(const struct netdev *dev)
56b11f0b 131{
b5d57fc8
BP
132 const struct netdev_class *class = netdev_get_class(dev);
133 const char *type = netdev_get_type(dev);
56b11f0b 134
a6ae068b
LJ
135 return (class->get_config == get_tunnel_config &&
136 (!strcmp("vxlan", type) || !strcmp("lisp", type)));
56b11f0b
KM
137}
138
94a53842
AW
139const char *
140netdev_vport_class_get_dpif_port(const struct netdev_class *class)
141{
142 return is_vport_class(class) ? vport_class_cast(class)->dpif_port : NULL;
143}
144
de281153 145const char *
3aa30359
BP
146netdev_vport_get_dpif_port(const struct netdev *netdev,
147 char namebuf[], size_t bufsize)
de281153 148{
a5d4fadd
JG
149 const struct netdev_class *class = netdev_get_class(netdev);
150 const char *dpif_port = netdev_vport_class_get_dpif_port(class);
151
152 if (!dpif_port) {
153 return netdev_get_name(netdev);
154 }
155
b5d57fc8
BP
156 if (netdev_vport_needs_dst_port(netdev)) {
157 const struct netdev_vport *vport = netdev_vport_cast(netdev);
56b11f0b
KM
158
159 /*
a5d4fadd
JG
160 * Note: IFNAMSIZ is 16 bytes long. Implementations should choose
161 * a dpif port name that is short enough to fit including any
162 * port numbers but assert just in case.
56b11f0b 163 */
3aa30359 164 BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ);
a5d4fadd
JG
165 ovs_assert(strlen(dpif_port) + 6 < IFNAMSIZ);
166 snprintf(namebuf, bufsize, "%s_%d", dpif_port,
56b11f0b 167 ntohs(vport->tnl_cfg.dst_port));
3aa30359 168 return namebuf;
56b11f0b 169 } else {
a5d4fadd 170 return dpif_port;
56b11f0b 171 }
2b9d6589 172}
777ece09 173
3aa30359
BP
174char *
175netdev_vport_get_dpif_port_strdup(const struct netdev *netdev)
176{
177 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
178
179 return xstrdup(netdev_vport_get_dpif_port(netdev, namebuf,
180 sizeof namebuf));
181}
182
41ca1e0a
AW
183/* Whenever the route-table change number is incremented,
184 * netdev_vport_route_changed() should be called to update
185 * the corresponding tunnel interface status. */
186static void
187netdev_vport_route_changed(void)
188{
189 struct netdev **vports;
190 size_t i, n_vports;
191
192 vports = netdev_get_vports(&n_vports);
193 for (i = 0; i < n_vports; i++) {
194 struct netdev *netdev_ = vports[i];
195 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
196
197 ovs_mutex_lock(&netdev->mutex);
198 /* Finds all tunnel vports. */
199 if (netdev->tnl_cfg.ip_dst) {
200 if (tunnel_check_status_change__(netdev)) {
201 netdev_change_seq_changed(netdev_);
202 }
203 }
204 netdev_close(netdev_);
205 ovs_mutex_unlock(&netdev->mutex);
206 }
207
208 free(vports);
209}
210
9dc63482
BP
211static struct netdev *
212netdev_vport_alloc(void)
213{
214 struct netdev_vport *netdev = xzalloc(sizeof *netdev);
215 return &netdev->up;
216}
217
2b9d6589 218static int
9dc63482 219netdev_vport_construct(struct netdev *netdev_)
2b9d6589 220{
9dc63482 221 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
6d9e6eb4 222
834d6caf 223 ovs_mutex_init(&netdev->mutex);
9dc63482 224 eth_addr_random(netdev->etheraddr);
6d9e6eb4 225
de5cdb90 226 route_table_register();
6d9e6eb4 227
de5cdb90 228 return 0;
777ece09
JG
229}
230
2b9d6589 231static void
9dc63482 232netdev_vport_destruct(struct netdev *netdev_)
2b9d6589 233{
b5d57fc8 234 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
2b9d6589 235
a132aa96 236 route_table_unregister();
b5d57fc8 237 free(netdev->peer);
86383816 238 ovs_mutex_destroy(&netdev->mutex);
9dc63482
BP
239}
240
241static void
242netdev_vport_dealloc(struct netdev *netdev_)
243{
244 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
2b9d6589
BP
245 free(netdev);
246}
247
2b9d6589 248static int
b5d57fc8 249netdev_vport_set_etheraddr(struct netdev *netdev_,
777ece09
JG
250 const uint8_t mac[ETH_ADDR_LEN])
251{
b5d57fc8 252 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
86383816
BP
253
254 ovs_mutex_lock(&netdev->mutex);
b5d57fc8 255 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
86383816 256 ovs_mutex_unlock(&netdev->mutex);
3e912ffc 257 netdev_change_seq_changed(netdev_);
86383816 258
35b769cb 259 return 0;
777ece09
JG
260}
261
2b9d6589 262static int
86383816 263netdev_vport_get_etheraddr(const struct netdev *netdev_,
777ece09
JG
264 uint8_t mac[ETH_ADDR_LEN])
265{
86383816
BP
266 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
267
268 ovs_mutex_lock(&netdev->mutex);
269 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
270 ovs_mutex_unlock(&netdev->mutex);
271
35b769cb 272 return 0;
777ece09
JG
273}
274
41ca1e0a
AW
275/* Checks if the tunnel status has changed and returns a boolean.
276 * Updates the tunnel status if it has changed. */
277static bool
278tunnel_check_status_change__(struct netdev_vport *netdev)
279 OVS_REQUIRES(netdev->mutex)
ea763e0e 280{
3dea0874 281 char iface[IFNAMSIZ];
41ca1e0a 282 bool status = false;
275707c3 283 ovs_be32 route;
ea763e0e 284
41ca1e0a 285 iface[0] = '\0';
86383816 286 route = netdev->tnl_cfg.ip_dst;
275707c3 287 if (route_table_get_name(route, iface)) {
a404826e
AE
288 struct netdev *egress_netdev;
289
18812dff 290 if (!netdev_open(iface, "system", &egress_netdev)) {
41ca1e0a 291 status = netdev_get_carrier(egress_netdev);
a404826e
AE
292 netdev_close(egress_netdev);
293 }
ea763e0e
EJ
294 }
295
41ca1e0a
AW
296 if (strcmp(netdev->egress_iface, iface)
297 || netdev->carrier_status != status) {
298 ovs_strlcpy(netdev->egress_iface, iface, IFNAMSIZ);
299 netdev->carrier_status = status;
300
301 return true;
302 }
303
304 return false;
305}
306
307static int
308tunnel_get_status(const struct netdev *netdev_, struct smap *smap)
309{
310 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
311
312 if (netdev->egress_iface[0]) {
313 smap_add(smap, "tunnel_egress_iface", netdev->egress_iface);
314
315 smap_add(smap, "tunnel_egress_iface_carrier",
316 netdev->carrier_status ? "up" : "down");
317 }
318
ea763e0e
EJ
319 return 0;
320}
321
2b9d6589 322static int
b5d57fc8
BP
323netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
324 enum netdev_flags off,
325 enum netdev_flags on OVS_UNUSED,
326 enum netdev_flags *old_flagsp)
777ece09
JG
327{
328 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
329 return EOPNOTSUPP;
330 }
331
332 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
333 return 0;
334}
335
ea83a2fc
EJ
336static void
337netdev_vport_run(void)
338{
41ca1e0a
AW
339 uint64_t seq;
340
a132aa96 341 route_table_run();
41ca1e0a
AW
342 seq = route_table_get_change_seq();
343 if (rt_change_seqno != seq) {
344 rt_change_seqno = seq;
345 netdev_vport_route_changed();
346 }
ea83a2fc
EJ
347}
348
349static void
350netdev_vport_wait(void)
351{
41ca1e0a
AW
352 uint64_t seq;
353
a132aa96 354 route_table_wait();
41ca1e0a
AW
355 seq = route_table_get_change_seq();
356 if (rt_change_seqno != seq) {
357 poll_immediate_wake();
358 }
ea83a2fc
EJ
359}
360\f
0a740f48 361/* Code specific to tunnel types. */
2b9d6589 362
f431bf7d
EJ
363static ovs_be64
364parse_key(const struct smap *args, const char *name,
365 bool *present, bool *flow)
c19e6535
BP
366{
367 const char *s;
368
f431bf7d
EJ
369 *present = false;
370 *flow = false;
371
79f1cbe9 372 s = smap_get(args, name);
c19e6535 373 if (!s) {
79f1cbe9 374 s = smap_get(args, "key");
c19e6535 375 if (!s) {
f431bf7d 376 return 0;
c19e6535
BP
377 }
378 }
379
f431bf7d
EJ
380 *present = true;
381
c19e6535 382 if (!strcmp(s, "flow")) {
f431bf7d
EJ
383 *flow = true;
384 return 0;
c19e6535 385 } else {
f431bf7d 386 return htonll(strtoull(s, NULL, 0));
c19e6535
BP
387 }
388}
389
2b9d6589 390static int
b5d57fc8 391set_tunnel_config(struct netdev *dev_, const struct smap *args)
2b9d6589 392{
b5d57fc8
BP
393 struct netdev_vport *dev = netdev_vport_cast(dev_);
394 const char *name = netdev_get_name(dev_);
395 const char *type = netdev_get_type(dev_);
f431bf7d
EJ
396 bool ipsec_mech_set, needs_dst_port, has_csum;
397 struct netdev_tunnel_config tnl_cfg;
79f1cbe9 398 struct smap_node *node;
f431bf7d 399
f431bf7d
EJ
400 has_csum = strstr(type, "gre");
401 ipsec_mech_set = false;
402 memset(&tnl_cfg, 0, sizeof tnl_cfg);
2b9d6589 403
a6ae068b 404 needs_dst_port = netdev_vport_needs_dst_port(dev_);
f431bf7d 405 tnl_cfg.ipsec = strstr(type, "ipsec");
f431bf7d 406 tnl_cfg.dont_fragment = true;
e16a28b5 407
79f1cbe9
EJ
408 SMAP_FOR_EACH (node, args) {
409 if (!strcmp(node->key, "remote_ip")) {
2b9d6589 410 struct in_addr in_addr;
0ad90c84
JR
411 if (!strcmp(node->value, "flow")) {
412 tnl_cfg.ip_dst_flow = true;
413 tnl_cfg.ip_dst = htonl(0);
414 } else if (lookup_ip(node->value, &in_addr)) {
c3827f61 415 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
85c9de19
PS
416 } else if (ip_is_multicast(in_addr.s_addr)) {
417 VLOG_WARN("%s: multicast remote_ip="IP_FMT" not allowed",
418 name, IP_ARGS(in_addr.s_addr));
419 return EINVAL;
2b9d6589 420 } else {
f431bf7d 421 tnl_cfg.ip_dst = in_addr.s_addr;
2b9d6589 422 }
79f1cbe9 423 } else if (!strcmp(node->key, "local_ip")) {
2b9d6589 424 struct in_addr in_addr;
0ad90c84
JR
425 if (!strcmp(node->value, "flow")) {
426 tnl_cfg.ip_src_flow = true;
427 tnl_cfg.ip_src = htonl(0);
428 } else if (lookup_ip(node->value, &in_addr)) {
c3827f61 429 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
2b9d6589 430 } else {
f431bf7d 431 tnl_cfg.ip_src = in_addr.s_addr;
2b9d6589 432 }
79f1cbe9
EJ
433 } else if (!strcmp(node->key, "tos")) {
434 if (!strcmp(node->value, "inherit")) {
f431bf7d 435 tnl_cfg.tos_inherit = true;
2b9d6589 436 } else {
3fca7064
PS
437 char *endptr;
438 int tos;
79f1cbe9 439 tos = strtol(node->value, &endptr, 0);
91aff446 440 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
f431bf7d 441 tnl_cfg.tos = tos;
91aff446
BP
442 } else {
443 VLOG_WARN("%s: invalid TOS %s", name, node->value);
3fca7064 444 }
2b9d6589 445 }
79f1cbe9
EJ
446 } else if (!strcmp(node->key, "ttl")) {
447 if (!strcmp(node->value, "inherit")) {
f431bf7d 448 tnl_cfg.ttl_inherit = true;
2b9d6589 449 } else {
f431bf7d 450 tnl_cfg.ttl = atoi(node->value);
2b9d6589 451 }
79f827fa 452 } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
f431bf7d 453 tnl_cfg.dst_port = htons(atoi(node->value));
f431bf7d 454 } else if (!strcmp(node->key, "csum") && has_csum) {
79f1cbe9 455 if (!strcmp(node->value, "true")) {
f431bf7d 456 tnl_cfg.csum = true;
2b9d6589 457 }
79f1cbe9
EJ
458 } else if (!strcmp(node->key, "df_default")) {
459 if (!strcmp(node->value, "false")) {
f431bf7d 460 tnl_cfg.dont_fragment = false;
66409d1b 461 }
f431bf7d 462 } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
79f1cbe9 463 if (smap_get(args, "certificate")) {
3c52fa7b
JP
464 ipsec_mech_set = true;
465 } else {
ef7ee76a
JP
466 const char *use_ssl_cert;
467
468 /* If the "use_ssl_cert" is true, then "certificate" and
469 * "private_key" will be pulled from the SSL table. The
470 * use of this option is strongly discouraged, since it
471 * will like be removed when multiple SSL configurations
472 * are supported by OVS.
473 */
79f1cbe9 474 use_ssl_cert = smap_get(args, "use_ssl_cert");
ef7ee76a 475 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
8283e514
JP
476 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
477 name);
b9ad7294 478 return EINVAL;
ef7ee76a
JP
479 }
480 ipsec_mech_set = true;
3c52fa7b 481 }
f431bf7d 482 } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
2b9d6589 483 ipsec_mech_set = true;
f431bf7d 484 } else if (tnl_cfg.ipsec
79f1cbe9
EJ
485 && (!strcmp(node->key, "certificate")
486 || !strcmp(node->key, "private_key")
487 || !strcmp(node->key, "use_ssl_cert"))) {
3c52fa7b 488 /* Ignore options not used by the netdev. */
79f1cbe9
EJ
489 } else if (!strcmp(node->key, "key") ||
490 !strcmp(node->key, "in_key") ||
491 !strcmp(node->key, "out_key")) {
c19e6535 492 /* Handled separately below. */
2b9d6589 493 } else {
79f1cbe9 494 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
2b9d6589
BP
495 }
496 }
497
79f827fa 498 /* Add a default destination port for VXLAN if none specified. */
a6ae068b 499 if (!strcmp(type, "vxlan") && !tnl_cfg.dst_port) {
f431bf7d 500 tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
79f827fa
KM
501 }
502
a6ae068b
LJ
503 /* Add a default destination port for LISP if none specified. */
504 if (!strcmp(type, "lisp") && !tnl_cfg.dst_port) {
505 tnl_cfg.dst_port = htons(LISP_DST_PORT);
506 }
507
f431bf7d 508 if (tnl_cfg.ipsec) {
9366380a 509 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
2a586a5c 510 static pid_t pid = 0;
6027d03d 511
c89809c9 512#ifndef _WIN32
9366380a 513 ovs_mutex_lock(&mutex);
900f7601 514 if (pid <= 0) {
2a586a5c
AS
515 char *file_name = xasprintf("%s/%s", ovs_rundir(),
516 "ovs-monitor-ipsec.pid");
517 pid = read_pidfile(file_name);
518 free(file_name);
519 }
9366380a 520 ovs_mutex_unlock(&mutex);
c89809c9 521#endif
2a586a5c 522
e7009c36 523 if (pid < 0) {
8283e514
JP
524 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
525 name);
b9ad7294 526 return EINVAL;
e7009c36 527 }
5059eff3 528
79f1cbe9 529 if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
8283e514 530 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
b9ad7294 531 return EINVAL;
3c52fa7b
JP
532 }
533
534 if (!ipsec_mech_set) {
8283e514
JP
535 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
536 name);
b9ad7294 537 return EINVAL;
3c52fa7b 538 }
2b9d6589
BP
539 }
540
0ad90c84 541 if (!tnl_cfg.ip_dst && !tnl_cfg.ip_dst_flow) {
8283e514
JP
542 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
543 name, type);
b9ad7294 544 return EINVAL;
2b9d6589 545 }
0ad90c84
JR
546 if (tnl_cfg.ip_src_flow && !tnl_cfg.ip_dst_flow) {
547 VLOG_ERR("%s: %s type requires 'remote_ip=flow' with 'local_ip=flow'",
548 name, type);
549 return EINVAL;
550 }
f431bf7d
EJ
551 if (!tnl_cfg.ttl) {
552 tnl_cfg.ttl = DEFAULT_TTL;
553 }
554
555 tnl_cfg.in_key = parse_key(args, "in_key",
556 &tnl_cfg.in_key_present,
557 &tnl_cfg.in_key_flow);
f431bf7d
EJ
558
559 tnl_cfg.out_key = parse_key(args, "out_key",
560 &tnl_cfg.out_key_present,
561 &tnl_cfg.out_key_flow);
2b9d6589 562
86383816 563 ovs_mutex_lock(&dev->mutex);
0a740f48 564 dev->tnl_cfg = tnl_cfg;
41ca1e0a 565 tunnel_check_status_change__(dev);
3e912ffc 566 netdev_change_seq_changed(dev_);
86383816 567 ovs_mutex_unlock(&dev->mutex);
f431bf7d 568
c19e6535
BP
569 return 0;
570}
571
2b9d6589 572static int
b5d57fc8 573get_tunnel_config(const struct netdev *dev, struct smap *args)
6d9e6eb4 574{
86383816
BP
575 struct netdev_vport *netdev = netdev_vport_cast(dev);
576 struct netdev_tunnel_config tnl_cfg;
577
578 ovs_mutex_lock(&netdev->mutex);
579 tnl_cfg = netdev->tnl_cfg;
580 ovs_mutex_unlock(&netdev->mutex);
6d9e6eb4 581
86383816
BP
582 if (tnl_cfg.ip_dst) {
583 smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(tnl_cfg.ip_dst));
584 } else if (tnl_cfg.ip_dst_flow) {
0ad90c84 585 smap_add(args, "remote_ip", "flow");
0a740f48
EJ
586 }
587
86383816
BP
588 if (tnl_cfg.ip_src) {
589 smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(tnl_cfg.ip_src));
590 } else if (tnl_cfg.ip_src_flow) {
0ad90c84 591 smap_add(args, "local_ip", "flow");
7f804ea5 592 }
c19e6535 593
86383816 594 if (tnl_cfg.in_key_flow && tnl_cfg.out_key_flow) {
6d9e6eb4 595 smap_add(args, "key", "flow");
86383816
BP
596 } else if (tnl_cfg.in_key_present && tnl_cfg.out_key_present
597 && tnl_cfg.in_key == tnl_cfg.out_key) {
598 smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg.in_key));
6d9e6eb4 599 } else {
86383816 600 if (tnl_cfg.in_key_flow) {
b9ad7294 601 smap_add(args, "in_key", "flow");
86383816 602 } else if (tnl_cfg.in_key_present) {
b9ad7294 603 smap_add_format(args, "in_key", "%"PRIu64,
86383816 604 ntohll(tnl_cfg.in_key));
b9ad7294 605 }
6d9e6eb4 606
86383816 607 if (tnl_cfg.out_key_flow) {
b9ad7294 608 smap_add(args, "out_key", "flow");
86383816 609 } else if (tnl_cfg.out_key_present) {
b9ad7294 610 smap_add_format(args, "out_key", "%"PRIu64,
86383816 611 ntohll(tnl_cfg.out_key));
6d9e6eb4
BP
612 }
613 }
614
86383816 615 if (tnl_cfg.ttl_inherit) {
62827e6a 616 smap_add(args, "ttl", "inherit");
86383816
BP
617 } else if (tnl_cfg.ttl != DEFAULT_TTL) {
618 smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg.ttl);
c19e6535
BP
619 }
620
86383816 621 if (tnl_cfg.tos_inherit) {
6d9e6eb4 622 smap_add(args, "tos", "inherit");
86383816
BP
623 } else if (tnl_cfg.tos) {
624 smap_add_format(args, "tos", "0x%x", tnl_cfg.tos);
6d9e6eb4
BP
625 }
626
86383816
BP
627 if (tnl_cfg.dst_port) {
628 uint16_t dst_port = ntohs(tnl_cfg.dst_port);
b5d57fc8 629 const char *type = netdev_get_type(dev);
9eeb949b
KM
630
631 if ((!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
632 (!strcmp("lisp", type) && dst_port != LISP_DST_PORT)) {
79f827fa
KM
633 smap_add_format(args, "dst_port", "%d", dst_port);
634 }
635 }
636
86383816 637 if (tnl_cfg.csum) {
6d9e6eb4
BP
638 smap_add(args, "csum", "true");
639 }
8a9ff93a 640
86383816 641 if (!tnl_cfg.dont_fragment) {
66409d1b
AE
642 smap_add(args, "df_default", "false");
643 }
6d9e6eb4
BP
644
645 return 0;
646}
0a740f48
EJ
647\f
648/* Code specific to patch ports. */
649
161b6042
BP
650/* If 'netdev' is a patch port, returns the name of its peer as a malloc()'d
651 * string that the caller must free.
652 *
653 * If 'netdev' is not a patch port, returns NULL. */
654char *
655netdev_vport_patch_peer(const struct netdev *netdev_)
0a740f48 656{
161b6042
BP
657 char *peer = NULL;
658
659 if (netdev_vport_is_patch(netdev_)) {
660 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
86383816
BP
661
662 ovs_mutex_lock(&netdev->mutex);
161b6042
BP
663 if (netdev->peer) {
664 peer = xstrdup(netdev->peer);
665 }
86383816 666 ovs_mutex_unlock(&netdev->mutex);
161b6042
BP
667 }
668
669 return peer;
0a740f48
EJ
670}
671
672void
b9ad7294 673netdev_vport_inc_rx(const struct netdev *netdev,
9e04d6f6 674 const struct dpif_flow_stats *stats)
0a740f48 675{
b5d57fc8
BP
676 if (is_vport_class(netdev_get_class(netdev))) {
677 struct netdev_vport *dev = netdev_vport_cast(netdev);
86383816
BP
678
679 ovs_mutex_lock(&dev->mutex);
0a740f48
EJ
680 dev->stats.rx_packets += stats->n_packets;
681 dev->stats.rx_bytes += stats->n_bytes;
86383816 682 ovs_mutex_unlock(&dev->mutex);
0a740f48
EJ
683 }
684}
685
686void
b9ad7294
EJ
687netdev_vport_inc_tx(const struct netdev *netdev,
688 const struct dpif_flow_stats *stats)
0a740f48 689{
b5d57fc8
BP
690 if (is_vport_class(netdev_get_class(netdev))) {
691 struct netdev_vport *dev = netdev_vport_cast(netdev);
86383816
BP
692
693 ovs_mutex_lock(&dev->mutex);
0a740f48
EJ
694 dev->stats.tx_packets += stats->n_packets;
695 dev->stats.tx_bytes += stats->n_bytes;
86383816 696 ovs_mutex_unlock(&dev->mutex);
0a740f48
EJ
697 }
698}
699
700static int
b5d57fc8 701get_patch_config(const struct netdev *dev_, struct smap *args)
0a740f48 702{
b5d57fc8 703 struct netdev_vport *dev = netdev_vport_cast(dev_);
0a740f48 704
86383816 705 ovs_mutex_lock(&dev->mutex);
0a740f48
EJ
706 if (dev->peer) {
707 smap_add(args, "peer", dev->peer);
708 }
86383816
BP
709 ovs_mutex_unlock(&dev->mutex);
710
0a740f48
EJ
711 return 0;
712}
6d9e6eb4
BP
713
714static int
b5d57fc8 715set_patch_config(struct netdev *dev_, const struct smap *args)
2b9d6589 716{
b5d57fc8
BP
717 struct netdev_vport *dev = netdev_vport_cast(dev_);
718 const char *name = netdev_get_name(dev_);
2b9d6589
BP
719 const char *peer;
720
79f1cbe9 721 peer = smap_get(args, "peer");
2b9d6589 722 if (!peer) {
8283e514 723 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
2b9d6589
BP
724 return EINVAL;
725 }
726
79f1cbe9 727 if (smap_count(args) > 1) {
8283e514 728 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
2b9d6589
BP
729 return EINVAL;
730 }
731
2b9d6589 732 if (!strcmp(name, peer)) {
8283e514 733 VLOG_ERR("%s: patch peer must not be self", name);
2b9d6589
BP
734 return EINVAL;
735 }
736
86383816 737 ovs_mutex_lock(&dev->mutex);
0a740f48
EJ
738 free(dev->peer);
739 dev->peer = xstrdup(peer);
3e912ffc 740 netdev_change_seq_changed(dev_);
86383816
BP
741 ovs_mutex_unlock(&dev->mutex);
742
2b9d6589
BP
743 return 0;
744}
6d9e6eb4
BP
745
746static int
b9ad7294 747get_stats(const struct netdev *netdev, struct netdev_stats *stats)
0a740f48 748{
b5d57fc8 749 struct netdev_vport *dev = netdev_vport_cast(netdev);
86383816
BP
750
751 ovs_mutex_lock(&dev->mutex);
752 *stats = dev->stats;
753 ovs_mutex_unlock(&dev->mutex);
754
6d9e6eb4
BP
755 return 0;
756}
2b9d6589 757\f
0a740f48 758#define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \
b9ad7294 759 GET_TUNNEL_CONFIG, GET_STATUS) \
b46ccdf5 760 NULL, \
ea83a2fc
EJ
761 netdev_vport_run, \
762 netdev_vport_wait, \
2b9d6589 763 \
9dc63482
BP
764 netdev_vport_alloc, \
765 netdev_vport_construct, \
766 netdev_vport_destruct, \
767 netdev_vport_dealloc, \
0a740f48
EJ
768 GET_CONFIG, \
769 SET_CONFIG, \
f431bf7d 770 GET_TUNNEL_CONFIG, \
2b9d6589 771 \
552e20d0 772 NULL, /* send */ \
2b9d6589
BP
773 NULL, /* send_wait */ \
774 \
775 netdev_vport_set_etheraddr, \
776 netdev_vport_get_etheraddr, \
14622f22
BP
777 NULL, /* get_mtu */ \
778 NULL, /* set_mtu */ \
2b9d6589 779 NULL, /* get_ifindex */ \
85da620e 780 NULL, /* get_carrier */ \
65c3058c 781 NULL, /* get_carrier_resets */ \
63331829 782 NULL, /* get_miimon */ \
b9ad7294 783 get_stats, \
2f31a822 784 NULL, /* set_stats */ \
2b9d6589
BP
785 \
786 NULL, /* get_features */ \
787 NULL, /* set_advertisements */ \
2b9d6589
BP
788 \
789 NULL, /* set_policing */ \
790 NULL, /* get_qos_types */ \
791 NULL, /* get_qos_capabilities */ \
792 NULL, /* get_qos */ \
793 NULL, /* set_qos */ \
794 NULL, /* get_queue */ \
795 NULL, /* set_queue */ \
796 NULL, /* delete_queue */ \
797 NULL, /* get_queue_stats */ \
89454bf4
BP
798 NULL, /* queue_dump_start */ \
799 NULL, /* queue_dump_next */ \
800 NULL, /* queue_dump_done */ \
2b9d6589
BP
801 NULL, /* dump_queue_stats */ \
802 \
803 NULL, /* get_in4 */ \
804 NULL, /* set_in4 */ \
805 NULL, /* get_in6 */ \
806 NULL, /* add_router */ \
807 NULL, /* get_next_hop */ \
ea763e0e 808 GET_STATUS, \
2b9d6589
BP
809 NULL, /* arp_lookup */ \
810 \
811 netdev_vport_update_flags, \
812 \
9dc63482
BP
813 NULL, /* rx_alloc */ \
814 NULL, /* rx_construct */ \
815 NULL, /* rx_destruct */ \
816 NULL, /* rx_dealloc */ \
817 NULL, /* rx_recv */ \
818 NULL, /* rx_wait */ \
819 NULL, /* rx_drain */
2b9d6589 820
c060c4cf
EJ
821#define TUNNEL_CLASS(NAME, DPIF_PORT) \
822 { DPIF_PORT, \
0a740f48
EJ
823 { NAME, VPORT_FUNCTIONS(get_tunnel_config, \
824 set_tunnel_config, \
825 get_netdev_tunnel_config, \
0a740f48 826 tunnel_get_status) }}
db078f85 827
2b9d6589 828void
c060c4cf 829netdev_vport_tunnel_register(void)
2b9d6589 830{
a5d4fadd
JG
831 /* The name of the dpif_port should be short enough to accomodate adding
832 * a port number to the end if one is necessary. */
c3827f61 833 static const struct vport_class vport_classes[] = {
a5d4fadd
JG
834 TUNNEL_CLASS("gre", "gre_sys"),
835 TUNNEL_CLASS("ipsec_gre", "gre_sys"),
836 TUNNEL_CLASS("gre64", "gre64_sys"),
837 TUNNEL_CLASS("ipsec_gre64", "gre64_sys"),
838 TUNNEL_CLASS("vxlan", "vxlan_sys"),
839 TUNNEL_CLASS("lisp", "lisp_sys")
c3827f61 840 };
86383816 841 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
c3827f61 842
86383816
BP
843 if (ovsthread_once_start(&once)) {
844 int i;
c3827f61 845
7c54c27f
BP
846 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
847 netdev_register_provider(&vport_classes[i].netdev_class);
848 }
86383816 849 ovsthread_once_done(&once);
c3827f61 850 }
2b9d6589 851}
c060c4cf
EJ
852
853void
854netdev_vport_patch_register(void)
855{
856 static const struct vport_class patch_class =
857 { NULL,
858 { "patch", VPORT_FUNCTIONS(get_patch_config,
859 set_patch_config,
860 NULL,
861 NULL) }};
862 netdev_register_provider(&patch_class.netdev_class);
863}