]> git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-vport.c
userspace: Switching of L3 packets in L2 pipeline
[mirror_ovs.git] / lib / netdev-vport.c
1 /*
2 * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2017 Nicira, Inc.
3 * Copyright (c) 2016 Red Hat, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <config.h>
19
20 #include "netdev-vport.h"
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/socket.h>
25 #include <net/if.h>
26 #include <netinet/in.h>
27 #include <netinet/ip6.h>
28 #include <sys/ioctl.h>
29
30 #include "byte-order.h"
31 #include "daemon.h"
32 #include "dirs.h"
33 #include "dpif.h"
34 #include "netdev.h"
35 #include "netdev-native-tnl.h"
36 #include "netdev-provider.h"
37 #include "netdev-vport-private.h"
38 #include "openvswitch/dynamic-string.h"
39 #include "ovs-router.h"
40 #include "packets.h"
41 #include "poll-loop.h"
42 #include "route-table.h"
43 #include "smap.h"
44 #include "socket-util.h"
45 #include "unaligned.h"
46 #include "unixctl.h"
47 #include "openvswitch/vlog.h"
48
49 VLOG_DEFINE_THIS_MODULE(netdev_vport);
50
51 #define GENEVE_DST_PORT 6081
52 #define VXLAN_DST_PORT 4789
53 #define LISP_DST_PORT 4341
54 #define STT_DST_PORT 7471
55
56 #define DEFAULT_TTL 64
57
58 /* Last read of the route-table's change number. */
59 static uint64_t rt_change_seqno;
60
61 static int get_patch_config(const struct netdev *netdev, struct smap *args);
62 static int get_tunnel_config(const struct netdev *, struct smap *args);
63 static bool tunnel_check_status_change__(struct netdev_vport *);
64
65 struct vport_class {
66 const char *dpif_port;
67 struct netdev_class netdev_class;
68 };
69
70 bool
71 netdev_vport_is_vport_class(const struct netdev_class *class)
72 {
73 return is_vport_class(class);
74 }
75
76 static const struct vport_class *
77 vport_class_cast(const struct netdev_class *class)
78 {
79 ovs_assert(is_vport_class(class));
80 return CONTAINER_OF(class, struct vport_class, netdev_class);
81 }
82
83 static const struct netdev_tunnel_config *
84 get_netdev_tunnel_config(const struct netdev *netdev)
85 {
86 return &netdev_vport_cast(netdev)->tnl_cfg;
87 }
88
89 bool
90 netdev_vport_is_patch(const struct netdev *netdev)
91 {
92 const struct netdev_class *class = netdev_get_class(netdev);
93
94 return class->get_config == get_patch_config;
95 }
96
97 bool
98 netdev_vport_is_layer3(const struct netdev *dev)
99 {
100 if (is_vport_class(netdev_get_class(dev))) {
101 struct netdev_vport *vport = netdev_vport_cast(dev);
102
103 return vport->tnl_cfg.is_layer3;
104 }
105
106 return false;
107 }
108
109 static bool
110 netdev_vport_needs_dst_port(const struct netdev *dev)
111 {
112 const struct netdev_class *class = netdev_get_class(dev);
113 const char *type = netdev_get_type(dev);
114
115 return (class->get_config == get_tunnel_config &&
116 (!strcmp("geneve", type) || !strcmp("vxlan", type) ||
117 !strcmp("lisp", type) || !strcmp("stt", type)) );
118 }
119
120 const char *
121 netdev_vport_class_get_dpif_port(const struct netdev_class *class)
122 {
123 return is_vport_class(class) ? vport_class_cast(class)->dpif_port : NULL;
124 }
125
126 const char *
127 netdev_vport_get_dpif_port(const struct netdev *netdev,
128 char namebuf[], size_t bufsize)
129 {
130 const struct netdev_class *class = netdev_get_class(netdev);
131 const char *dpif_port = netdev_vport_class_get_dpif_port(class);
132
133 if (!dpif_port) {
134 return netdev_get_name(netdev);
135 }
136
137 if (netdev_vport_needs_dst_port(netdev)) {
138 const struct netdev_vport *vport = netdev_vport_cast(netdev);
139
140 /*
141 * Note: IFNAMSIZ is 16 bytes long. Implementations should choose
142 * a dpif port name that is short enough to fit including any
143 * port numbers but assert just in case.
144 */
145 BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ);
146 ovs_assert(strlen(dpif_port) + 6 < IFNAMSIZ);
147 snprintf(namebuf, bufsize, "%s_%d", dpif_port,
148 ntohs(vport->tnl_cfg.dst_port));
149 return namebuf;
150 } else {
151 return dpif_port;
152 }
153 }
154
155 /* Whenever the route-table change number is incremented,
156 * netdev_vport_route_changed() should be called to update
157 * the corresponding tunnel interface status. */
158 static void
159 netdev_vport_route_changed(void)
160 {
161 struct netdev **vports;
162 size_t i, n_vports;
163
164 vports = netdev_get_vports(&n_vports);
165 for (i = 0; i < n_vports; i++) {
166 struct netdev *netdev_ = vports[i];
167 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
168
169 ovs_mutex_lock(&netdev->mutex);
170 /* Finds all tunnel vports. */
171 if (ipv6_addr_is_set(&netdev->tnl_cfg.ipv6_dst)) {
172 if (tunnel_check_status_change__(netdev)) {
173 netdev_change_seq_changed(netdev_);
174 }
175 }
176 ovs_mutex_unlock(&netdev->mutex);
177
178 netdev_close(netdev_);
179 }
180
181 free(vports);
182 }
183
184 static struct netdev *
185 netdev_vport_alloc(void)
186 {
187 struct netdev_vport *netdev = xzalloc(sizeof *netdev);
188 return &netdev->up;
189 }
190
191 int
192 netdev_vport_construct(struct netdev *netdev_)
193 {
194 struct netdev_vport *dev = netdev_vport_cast(netdev_);
195 const char *type = netdev_get_type(netdev_);
196
197 ovs_mutex_init(&dev->mutex);
198 eth_addr_random(&dev->etheraddr);
199
200 /* Add a default destination port for tunnel ports if none specified. */
201 if (!strcmp(type, "geneve")) {
202 dev->tnl_cfg.dst_port = htons(GENEVE_DST_PORT);
203 } else if (!strcmp(type, "vxlan")) {
204 dev->tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
205 } else if (!strcmp(type, "lisp")) {
206 dev->tnl_cfg.dst_port = htons(LISP_DST_PORT);
207 } else if (!strcmp(type, "stt")) {
208 dev->tnl_cfg.dst_port = htons(STT_DST_PORT);
209 }
210
211 dev->tnl_cfg.dont_fragment = true;
212 dev->tnl_cfg.ttl = DEFAULT_TTL;
213 return 0;
214 }
215
216 static void
217 netdev_vport_destruct(struct netdev *netdev_)
218 {
219 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
220
221 free(netdev->peer);
222 ovs_mutex_destroy(&netdev->mutex);
223 }
224
225 static void
226 netdev_vport_dealloc(struct netdev *netdev_)
227 {
228 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
229 free(netdev);
230 }
231
232 static int
233 netdev_vport_set_etheraddr(struct netdev *netdev_, const struct eth_addr mac)
234 {
235 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
236
237 ovs_mutex_lock(&netdev->mutex);
238 netdev->etheraddr = mac;
239 ovs_mutex_unlock(&netdev->mutex);
240 netdev_change_seq_changed(netdev_);
241
242 return 0;
243 }
244
245 static int
246 netdev_vport_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac)
247 {
248 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
249
250 ovs_mutex_lock(&netdev->mutex);
251 *mac = netdev->etheraddr;
252 ovs_mutex_unlock(&netdev->mutex);
253
254 return 0;
255 }
256
257 /* Checks if the tunnel status has changed and returns a boolean.
258 * Updates the tunnel status if it has changed. */
259 static bool
260 tunnel_check_status_change__(struct netdev_vport *netdev)
261 OVS_REQUIRES(netdev->mutex)
262 {
263 char iface[IFNAMSIZ];
264 bool status = false;
265 struct in6_addr *route;
266 struct in6_addr gw;
267 uint32_t mark;
268
269 iface[0] = '\0';
270 route = &netdev->tnl_cfg.ipv6_dst;
271 mark = netdev->tnl_cfg.egress_pkt_mark;
272 if (ovs_router_lookup(mark, route, iface, NULL, &gw)) {
273 struct netdev *egress_netdev;
274
275 if (!netdev_open(iface, NULL, &egress_netdev)) {
276 status = netdev_get_carrier(egress_netdev);
277 netdev_close(egress_netdev);
278 }
279 }
280
281 if (strcmp(netdev->egress_iface, iface)
282 || netdev->carrier_status != status) {
283 ovs_strlcpy_arrays(netdev->egress_iface, iface);
284 netdev->carrier_status = status;
285
286 return true;
287 }
288
289 return false;
290 }
291
292 static int
293 tunnel_get_status(const struct netdev *netdev_, struct smap *smap)
294 {
295 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
296
297 if (netdev->egress_iface[0]) {
298 smap_add(smap, "tunnel_egress_iface", netdev->egress_iface);
299
300 smap_add(smap, "tunnel_egress_iface_carrier",
301 netdev->carrier_status ? "up" : "down");
302 }
303
304 return 0;
305 }
306
307 static int
308 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
309 enum netdev_flags off,
310 enum netdev_flags on OVS_UNUSED,
311 enum netdev_flags *old_flagsp)
312 {
313 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
314 return EOPNOTSUPP;
315 }
316
317 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
318 return 0;
319 }
320
321 static void
322 netdev_vport_run(const struct netdev_class *netdev_class OVS_UNUSED)
323 {
324 uint64_t seq;
325
326 route_table_run();
327 seq = route_table_get_change_seq();
328 if (rt_change_seqno != seq) {
329 rt_change_seqno = seq;
330 netdev_vport_route_changed();
331 }
332 }
333
334 static void
335 netdev_vport_wait(const struct netdev_class *netdev_class OVS_UNUSED)
336 {
337 uint64_t seq;
338
339 route_table_wait();
340 seq = route_table_get_change_seq();
341 if (rt_change_seqno != seq) {
342 poll_immediate_wake();
343 }
344 }
345 \f
346 /* Code specific to tunnel types. */
347
348 static ovs_be64
349 parse_key(const struct smap *args, const char *name,
350 bool *present, bool *flow)
351 {
352 const char *s;
353
354 *present = false;
355 *flow = false;
356
357 s = smap_get(args, name);
358 if (!s) {
359 s = smap_get(args, "key");
360 if (!s) {
361 return 0;
362 }
363 }
364
365 *present = true;
366
367 if (!strcmp(s, "flow")) {
368 *flow = true;
369 return 0;
370 } else {
371 return htonll(strtoull(s, NULL, 0));
372 }
373 }
374
375 static int
376 parse_tunnel_ip(const char *value, bool accept_mcast, bool *flow,
377 struct in6_addr *ipv6, uint16_t *protocol)
378 {
379 if (!strcmp(value, "flow")) {
380 *flow = true;
381 *protocol = 0;
382 return 0;
383 }
384 if (addr_is_ipv6(value)) {
385 if (lookup_ipv6(value, ipv6)) {
386 return ENOENT;
387 }
388 if (!accept_mcast && ipv6_addr_is_multicast(ipv6)) {
389 return EINVAL;
390 }
391 *protocol = ETH_TYPE_IPV6;
392 } else {
393 struct in_addr ip;
394 if (lookup_ip(value, &ip)) {
395 return ENOENT;
396 }
397 if (!accept_mcast && ip_is_multicast(ip.s_addr)) {
398 return EINVAL;
399 }
400 in6_addr_set_mapped_ipv4(ipv6, ip.s_addr);
401 *protocol = ETH_TYPE_IP;
402 }
403 return 0;
404 }
405
406 static int
407 set_tunnel_config(struct netdev *dev_, const struct smap *args, char **errp)
408 {
409 struct netdev_vport *dev = netdev_vport_cast(dev_);
410 const char *name = netdev_get_name(dev_);
411 const char *type = netdev_get_type(dev_);
412 struct ds errors = DS_EMPTY_INITIALIZER;
413 bool needs_dst_port, has_csum;
414 uint16_t dst_proto = 0, src_proto = 0;
415 struct netdev_tunnel_config tnl_cfg;
416 struct smap_node *node;
417 int err;
418
419 has_csum = strstr(type, "gre") || strstr(type, "geneve") ||
420 strstr(type, "stt") || strstr(type, "vxlan");
421 memset(&tnl_cfg, 0, sizeof tnl_cfg);
422
423 /* Add a default destination port for tunnel ports if none specified. */
424 if (!strcmp(type, "geneve")) {
425 tnl_cfg.dst_port = htons(GENEVE_DST_PORT);
426 }
427
428 if (!strcmp(type, "vxlan")) {
429 tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
430 }
431
432 if (!strcmp(type, "lisp")) {
433 tnl_cfg.dst_port = htons(LISP_DST_PORT);
434 }
435
436 if (!strcmp(type, "stt")) {
437 tnl_cfg.dst_port = htons(STT_DST_PORT);
438 }
439
440 needs_dst_port = netdev_vport_needs_dst_port(dev_);
441 tnl_cfg.dont_fragment = true;
442
443 SMAP_FOR_EACH (node, args) {
444 if (!strcmp(node->key, "remote_ip")) {
445 err = parse_tunnel_ip(node->value, false, &tnl_cfg.ip_dst_flow,
446 &tnl_cfg.ipv6_dst, &dst_proto);
447 switch (err) {
448 case ENOENT:
449 ds_put_format(&errors, "%s: bad %s 'remote_ip'\n", name, type);
450 break;
451 case EINVAL:
452 ds_put_format(&errors,
453 "%s: multicast remote_ip=%s not allowed\n",
454 name, node->value);
455 goto out;
456 }
457 } else if (!strcmp(node->key, "local_ip")) {
458 err = parse_tunnel_ip(node->value, true, &tnl_cfg.ip_src_flow,
459 &tnl_cfg.ipv6_src, &src_proto);
460 switch (err) {
461 case ENOENT:
462 ds_put_format(&errors, "%s: bad %s 'local_ip'\n", name, type);
463 break;
464 }
465 } else if (!strcmp(node->key, "tos")) {
466 if (!strcmp(node->value, "inherit")) {
467 tnl_cfg.tos_inherit = true;
468 } else {
469 char *endptr;
470 int tos;
471 tos = strtol(node->value, &endptr, 0);
472 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
473 tnl_cfg.tos = tos;
474 } else {
475 ds_put_format(&errors, "%s: invalid TOS %s\n", name,
476 node->value);
477 }
478 }
479 } else if (!strcmp(node->key, "ttl")) {
480 if (!strcmp(node->value, "inherit")) {
481 tnl_cfg.ttl_inherit = true;
482 } else {
483 tnl_cfg.ttl = atoi(node->value);
484 }
485 } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
486 tnl_cfg.dst_port = htons(atoi(node->value));
487 } else if (!strcmp(node->key, "csum") && has_csum) {
488 if (!strcmp(node->value, "true")) {
489 tnl_cfg.csum = true;
490 }
491 } else if (!strcmp(node->key, "df_default")) {
492 if (!strcmp(node->value, "false")) {
493 tnl_cfg.dont_fragment = false;
494 }
495 } else if (!strcmp(node->key, "key") ||
496 !strcmp(node->key, "in_key") ||
497 !strcmp(node->key, "out_key")) {
498 /* Handled separately below. */
499 } else if (!strcmp(node->key, "exts")) {
500 char *str = xstrdup(node->value);
501 char *ext, *save_ptr = NULL;
502
503 tnl_cfg.exts = 0;
504
505 ext = strtok_r(str, ",", &save_ptr);
506 while (ext) {
507 if (!strcmp(type, "vxlan") && !strcmp(ext, "gbp")) {
508 tnl_cfg.exts |= (1 << OVS_VXLAN_EXT_GBP);
509 } else {
510 ds_put_format(&errors, "%s: unknown extension '%s'\n",
511 name, ext);
512 }
513
514 ext = strtok_r(NULL, ",", &save_ptr);
515 }
516
517 free(str);
518 } else if (!strcmp(node->key, "egress_pkt_mark")) {
519 tnl_cfg.egress_pkt_mark = strtoul(node->value, NULL, 10);
520 tnl_cfg.set_egress_pkt_mark = true;
521 } else {
522 ds_put_format(&errors, "%s: unknown %s argument '%s'\n",
523 name, type, node->key);
524 }
525 }
526
527 if (!ipv6_addr_is_set(&tnl_cfg.ipv6_dst) && !tnl_cfg.ip_dst_flow) {
528 ds_put_format(&errors,
529 "%s: %s type requires valid 'remote_ip' argument\n",
530 name, type);
531 err = EINVAL;
532 goto out;
533 }
534 if (tnl_cfg.ip_src_flow && !tnl_cfg.ip_dst_flow) {
535 ds_put_format(&errors,
536 "%s: %s type requires 'remote_ip=flow' "
537 "with 'local_ip=flow'\n",
538 name, type);
539 err = EINVAL;
540 goto out;
541 }
542 if (src_proto && dst_proto && src_proto != dst_proto) {
543 ds_put_format(&errors,
544 "%s: 'remote_ip' and 'local_ip' "
545 "has to be of the same address family\n",
546 name);
547 err = EINVAL;
548 goto out;
549 }
550 if (!tnl_cfg.ttl) {
551 tnl_cfg.ttl = DEFAULT_TTL;
552 }
553
554 tnl_cfg.in_key = parse_key(args, "in_key",
555 &tnl_cfg.in_key_present,
556 &tnl_cfg.in_key_flow);
557
558 tnl_cfg.out_key = parse_key(args, "out_key",
559 &tnl_cfg.out_key_present,
560 &tnl_cfg.out_key_flow);
561
562 ovs_mutex_lock(&dev->mutex);
563 if (memcmp(&dev->tnl_cfg, &tnl_cfg, sizeof tnl_cfg)) {
564 dev->tnl_cfg = tnl_cfg;
565 tunnel_check_status_change__(dev);
566 netdev_change_seq_changed(dev_);
567 }
568 ovs_mutex_unlock(&dev->mutex);
569
570 err = 0;
571
572 out:
573 if (errors.length) {
574 ds_chomp(&errors, '\n');
575 VLOG_WARN("%s", ds_cstr(&errors));
576 if (err) {
577 *errp = ds_steal_cstr(&errors);
578 }
579 }
580
581 ds_destroy(&errors);
582
583 return err;
584 }
585
586 static int
587 get_tunnel_config(const struct netdev *dev, struct smap *args)
588 {
589 struct netdev_vport *netdev = netdev_vport_cast(dev);
590 struct netdev_tunnel_config tnl_cfg;
591
592 ovs_mutex_lock(&netdev->mutex);
593 tnl_cfg = netdev->tnl_cfg;
594 ovs_mutex_unlock(&netdev->mutex);
595
596 if (ipv6_addr_is_set(&tnl_cfg.ipv6_dst)) {
597 smap_add_ipv6(args, "remote_ip", &tnl_cfg.ipv6_dst);
598 } else if (tnl_cfg.ip_dst_flow) {
599 smap_add(args, "remote_ip", "flow");
600 }
601
602 if (ipv6_addr_is_set(&tnl_cfg.ipv6_src)) {
603 smap_add_ipv6(args, "local_ip", &tnl_cfg.ipv6_src);
604 } else if (tnl_cfg.ip_src_flow) {
605 smap_add(args, "local_ip", "flow");
606 }
607
608 if (tnl_cfg.in_key_flow && tnl_cfg.out_key_flow) {
609 smap_add(args, "key", "flow");
610 } else if (tnl_cfg.in_key_present && tnl_cfg.out_key_present
611 && tnl_cfg.in_key == tnl_cfg.out_key) {
612 smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg.in_key));
613 } else {
614 if (tnl_cfg.in_key_flow) {
615 smap_add(args, "in_key", "flow");
616 } else if (tnl_cfg.in_key_present) {
617 smap_add_format(args, "in_key", "%"PRIu64,
618 ntohll(tnl_cfg.in_key));
619 }
620
621 if (tnl_cfg.out_key_flow) {
622 smap_add(args, "out_key", "flow");
623 } else if (tnl_cfg.out_key_present) {
624 smap_add_format(args, "out_key", "%"PRIu64,
625 ntohll(tnl_cfg.out_key));
626 }
627 }
628
629 if (tnl_cfg.ttl_inherit) {
630 smap_add(args, "ttl", "inherit");
631 } else if (tnl_cfg.ttl != DEFAULT_TTL) {
632 smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg.ttl);
633 }
634
635 if (tnl_cfg.tos_inherit) {
636 smap_add(args, "tos", "inherit");
637 } else if (tnl_cfg.tos) {
638 smap_add_format(args, "tos", "0x%x", tnl_cfg.tos);
639 }
640
641 if (tnl_cfg.dst_port) {
642 uint16_t dst_port = ntohs(tnl_cfg.dst_port);
643 const char *type = netdev_get_type(dev);
644
645 if ((!strcmp("geneve", type) && dst_port != GENEVE_DST_PORT) ||
646 (!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
647 (!strcmp("lisp", type) && dst_port != LISP_DST_PORT) ||
648 (!strcmp("stt", type) && dst_port != STT_DST_PORT)) {
649 smap_add_format(args, "dst_port", "%d", dst_port);
650 }
651 }
652
653 if (tnl_cfg.csum) {
654 smap_add(args, "csum", "true");
655 }
656
657 if (!tnl_cfg.dont_fragment) {
658 smap_add(args, "df_default", "false");
659 }
660
661 if (tnl_cfg.set_egress_pkt_mark) {
662 smap_add_format(args, "egress_pkt_mark",
663 "%"PRIu32, tnl_cfg.egress_pkt_mark);
664 }
665 return 0;
666 }
667 \f
668 /* Code specific to patch ports. */
669
670 /* If 'netdev' is a patch port, returns the name of its peer as a malloc()'d
671 * string that the caller must free.
672 *
673 * If 'netdev' is not a patch port, returns NULL. */
674 char *
675 netdev_vport_patch_peer(const struct netdev *netdev_)
676 {
677 char *peer = NULL;
678
679 if (netdev_vport_is_patch(netdev_)) {
680 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
681
682 ovs_mutex_lock(&netdev->mutex);
683 if (netdev->peer) {
684 peer = xstrdup(netdev->peer);
685 }
686 ovs_mutex_unlock(&netdev->mutex);
687 }
688
689 return peer;
690 }
691
692 void
693 netdev_vport_inc_rx(const struct netdev *netdev,
694 const struct dpif_flow_stats *stats)
695 {
696 if (is_vport_class(netdev_get_class(netdev))) {
697 struct netdev_vport *dev = netdev_vport_cast(netdev);
698
699 ovs_mutex_lock(&dev->mutex);
700 dev->stats.rx_packets += stats->n_packets;
701 dev->stats.rx_bytes += stats->n_bytes;
702 ovs_mutex_unlock(&dev->mutex);
703 }
704 }
705
706 void
707 netdev_vport_inc_tx(const struct netdev *netdev,
708 const struct dpif_flow_stats *stats)
709 {
710 if (is_vport_class(netdev_get_class(netdev))) {
711 struct netdev_vport *dev = netdev_vport_cast(netdev);
712
713 ovs_mutex_lock(&dev->mutex);
714 dev->stats.tx_packets += stats->n_packets;
715 dev->stats.tx_bytes += stats->n_bytes;
716 ovs_mutex_unlock(&dev->mutex);
717 }
718 }
719
720 static int
721 get_patch_config(const struct netdev *dev_, struct smap *args)
722 {
723 struct netdev_vport *dev = netdev_vport_cast(dev_);
724
725 ovs_mutex_lock(&dev->mutex);
726 if (dev->peer) {
727 smap_add(args, "peer", dev->peer);
728 }
729 ovs_mutex_unlock(&dev->mutex);
730
731 return 0;
732 }
733
734 static int
735 set_patch_config(struct netdev *dev_, const struct smap *args, char **errp)
736 {
737 struct netdev_vport *dev = netdev_vport_cast(dev_);
738 const char *name = netdev_get_name(dev_);
739 const char *peer;
740
741 peer = smap_get(args, "peer");
742 if (!peer) {
743 VLOG_ERR_BUF(errp, "%s: patch type requires valid 'peer' argument",
744 name);
745 return EINVAL;
746 }
747
748 if (smap_count(args) > 1) {
749 VLOG_ERR_BUF(errp, "%s: patch type takes only a 'peer' argument",
750 name);
751 return EINVAL;
752 }
753
754 if (!strcmp(name, peer)) {
755 VLOG_ERR_BUF(errp, "%s: patch peer must not be self", name);
756 return EINVAL;
757 }
758
759 ovs_mutex_lock(&dev->mutex);
760 if (!dev->peer || strcmp(dev->peer, peer)) {
761 free(dev->peer);
762 dev->peer = xstrdup(peer);
763 netdev_change_seq_changed(dev_);
764 }
765 ovs_mutex_unlock(&dev->mutex);
766
767 return 0;
768 }
769
770 static int
771 get_stats(const struct netdev *netdev, struct netdev_stats *stats)
772 {
773 struct netdev_vport *dev = netdev_vport_cast(netdev);
774
775 ovs_mutex_lock(&dev->mutex);
776 /* Passing only collected counters */
777 stats->tx_packets = dev->stats.tx_packets;
778 stats->tx_bytes = dev->stats.tx_bytes;
779 stats->rx_packets = dev->stats.rx_packets;
780 stats->rx_bytes = dev->stats.rx_bytes;
781 ovs_mutex_unlock(&dev->mutex);
782
783 return 0;
784 }
785
786 \f
787 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \
788 GET_TUNNEL_CONFIG, GET_STATUS, \
789 BUILD_HEADER, \
790 PUSH_HEADER, POP_HEADER) \
791 NULL, \
792 netdev_vport_run, \
793 netdev_vport_wait, \
794 \
795 netdev_vport_alloc, \
796 netdev_vport_construct, \
797 netdev_vport_destruct, \
798 netdev_vport_dealloc, \
799 GET_CONFIG, \
800 SET_CONFIG, \
801 GET_TUNNEL_CONFIG, \
802 BUILD_HEADER, \
803 PUSH_HEADER, \
804 POP_HEADER, \
805 NULL, /* get_numa_id */ \
806 NULL, /* set_tx_multiq */ \
807 \
808 NULL, /* send */ \
809 NULL, /* send_wait */ \
810 \
811 netdev_vport_set_etheraddr, \
812 netdev_vport_get_etheraddr, \
813 NULL, /* get_mtu */ \
814 NULL, /* set_mtu */ \
815 NULL, /* get_ifindex */ \
816 NULL, /* get_carrier */ \
817 NULL, /* get_carrier_resets */ \
818 NULL, /* get_miimon */ \
819 get_stats, \
820 \
821 NULL, /* get_features */ \
822 NULL, /* set_advertisements */ \
823 \
824 NULL, /* set_policing */ \
825 NULL, /* get_qos_types */ \
826 NULL, /* get_qos_capabilities */ \
827 NULL, /* get_qos */ \
828 NULL, /* set_qos */ \
829 NULL, /* get_queue */ \
830 NULL, /* set_queue */ \
831 NULL, /* delete_queue */ \
832 NULL, /* get_queue_stats */ \
833 NULL, /* queue_dump_start */ \
834 NULL, /* queue_dump_next */ \
835 NULL, /* queue_dump_done */ \
836 NULL, /* dump_queue_stats */ \
837 \
838 NULL, /* set_in4 */ \
839 NULL, /* get_addr_list */ \
840 NULL, /* add_router */ \
841 NULL, /* get_next_hop */ \
842 GET_STATUS, \
843 NULL, /* arp_lookup */ \
844 \
845 netdev_vport_update_flags, \
846 NULL, /* reconfigure */ \
847 \
848 NULL, /* rx_alloc */ \
849 NULL, /* rx_construct */ \
850 NULL, /* rx_destruct */ \
851 NULL, /* rx_dealloc */ \
852 NULL, /* rx_recv */ \
853 NULL, /* rx_wait */ \
854 NULL, /* rx_drain */
855
856
857 #define TUNNEL_CLASS(NAME, DPIF_PORT, BUILD_HEADER, PUSH_HEADER, POP_HEADER) \
858 { DPIF_PORT, \
859 { NAME, false, \
860 VPORT_FUNCTIONS(get_tunnel_config, \
861 set_tunnel_config, \
862 get_netdev_tunnel_config, \
863 tunnel_get_status, \
864 BUILD_HEADER, PUSH_HEADER, POP_HEADER) }}
865
866 void
867 netdev_vport_tunnel_register(void)
868 {
869 /* The name of the dpif_port should be short enough to accomodate adding
870 * a port number to the end if one is necessary. */
871 static const struct vport_class vport_classes[] = {
872 TUNNEL_CLASS("geneve", "genev_sys", netdev_geneve_build_header,
873 netdev_tnl_push_udp_header,
874 netdev_geneve_pop_header),
875 TUNNEL_CLASS("gre", "gre_sys", netdev_gre_build_header,
876 netdev_gre_push_header,
877 netdev_gre_pop_header),
878 TUNNEL_CLASS("vxlan", "vxlan_sys", netdev_vxlan_build_header,
879 netdev_tnl_push_udp_header,
880 netdev_vxlan_pop_header),
881 TUNNEL_CLASS("lisp", "lisp_sys", NULL, NULL, NULL),
882 TUNNEL_CLASS("stt", "stt_sys", NULL, NULL, NULL),
883 };
884 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
885
886 if (ovsthread_once_start(&once)) {
887 int i;
888
889 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
890 netdev_register_provider(&vport_classes[i].netdev_class);
891 }
892
893 unixctl_command_register("tnl/egress_port_range", "min max", 0, 2,
894 netdev_tnl_egress_port_range, NULL);
895
896 ovsthread_once_done(&once);
897 }
898 }
899
900 void
901 netdev_vport_patch_register(void)
902 {
903 static const struct vport_class patch_class =
904 { NULL,
905 { "patch", false,
906 VPORT_FUNCTIONS(get_patch_config,
907 set_patch_config,
908 NULL,
909 NULL, NULL, NULL, NULL) }};
910 netdev_register_provider(&patch_class.netdev_class);
911 }