]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-vport.c
dpif-linux: Recycle leaked ports.
[mirror_ovs.git] / lib / netdev-vport.c
CommitLineData
777ece09 1/*
a132aa96 2 * Copyright (c) 2010, 2011 Nicira Networks.
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
EJ
23#include <sys/socket.h>
24#include <linux/rtnetlink.h>
2b9d6589 25#include <net/if.h>
777ece09
JG
26#include <sys/ioctl.h>
27
b9298d3f 28#include "byte-order.h"
5059eff3
JP
29#include "daemon.h"
30#include "dirs.h"
c19e6535 31#include "dpif-linux.h"
ea83a2fc
EJ
32#include "hash.h"
33#include "hmap.h"
777ece09 34#include "list.h"
2b9d6589 35#include "netdev-provider.h"
ea83a2fc
EJ
36#include "netlink.h"
37#include "netlink-socket.h"
38#include "ofpbuf.h"
777ece09 39#include "openvswitch/datapath-protocol.h"
2b9d6589
BP
40#include "openvswitch/tunnel.h"
41#include "packets.h"
a132aa96 42#include "route-table.h"
c19e6535 43#include "rtnetlink.h"
777ece09
JG
44#include "shash.h"
45#include "socket-util.h"
777ece09
JG
46#include "vlog.h"
47
d98e6007 48VLOG_DEFINE_THIS_MODULE(netdev_vport);
5136ce49 49
777ece09
JG
50struct netdev_vport_notifier {
51 struct netdev_notifier notifier;
52 struct list list_node;
d295e8e9 53 struct shash_node *shash_node;
777ece09
JG
54};
55
2b9d6589
BP
56struct netdev_dev_vport {
57 struct netdev_dev netdev_dev;
c19e6535 58 struct ofpbuf *options;
7feba1ac
BP
59 int dp_ifindex; /* -1 if unknown. */
60 uint32_t port_no; /* UINT32_MAX if unknown. */
2b9d6589
BP
61};
62
63struct netdev_vport {
64 struct netdev netdev;
65};
66
2b9d6589 67struct vport_class {
c283069c 68 enum odp_vport_type type;
c3827f61 69 struct netdev_class netdev_class;
6d9e6eb4 70 int (*parse_config)(const char *name, const char *type,
c19e6535 71 const struct shash *args, struct ofpbuf *options);
6d9e6eb4 72 int (*unparse_config)(const char *name, const char *type,
c19e6535
BP
73 const struct nlattr *options, size_t options_len,
74 struct shash *args);
2b9d6589
BP
75};
76
777ece09
JG
77static struct shash netdev_vport_notifiers =
78 SHASH_INITIALIZER(&netdev_vport_notifiers);
79
80static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
81
2b9d6589
BP
82static int netdev_vport_create(const struct netdev_class *, const char *,
83 const struct shash *, struct netdev_dev **);
84static void netdev_vport_poll_notify(const struct netdev *);
c19e6535
BP
85static int tnl_port_config_from_nlattr(const struct nlattr *options,
86 size_t options_len,
87 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1]);
2b9d6589 88
ea763e0e 89static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
ea83a2fc 90
2b9d6589
BP
91static bool
92is_vport_class(const struct netdev_class *class)
777ece09 93{
2b9d6589
BP
94 return class->create == netdev_vport_create;
95}
777ece09 96
2b9d6589
BP
97static const struct vport_class *
98vport_class_cast(const struct netdev_class *class)
99{
100 assert(is_vport_class(class));
101 return CONTAINER_OF(class, struct vport_class, netdev_class);
102}
103
104static struct netdev_dev_vport *
105netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
106{
107 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
108 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
109}
110
111static struct netdev_vport *
112netdev_vport_cast(const struct netdev *netdev)
113{
114 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
115 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
116 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
117}
118
c19e6535
BP
119/* If 'netdev' is a vport netdev, returns an ofpbuf that contains Netlink
120 * options to include in ODP_VPORT_ATTR_OPTIONS for configuring that vport.
121 * Otherwise returns NULL. */
122const struct ofpbuf *
123netdev_vport_get_options(const struct netdev *netdev)
124{
125 const struct netdev_dev *dev = netdev_get_dev(netdev);
126
127 return (is_vport_class(netdev_dev_get_class(dev))
128 ? netdev_dev_vport_cast(dev)->options
129 : NULL);
130}
131
132enum odp_vport_type
133netdev_vport_get_vport_type(const struct netdev *netdev)
2b9d6589 134{
c3827f61 135 const struct netdev_dev *dev = netdev_get_dev(netdev);
c19e6535
BP
136 const struct netdev_class *class = netdev_dev_get_class(dev);
137
138 return (is_vport_class(class) ? vport_class_cast(class)->type
139 : class == &netdev_internal_class ? ODP_VPORT_TYPE_INTERNAL
140 : class == &netdev_linux_class ? ODP_VPORT_TYPE_NETDEV
141 : ODP_VPORT_TYPE_UNSPEC);
142}
143
144const char *
145netdev_vport_get_netdev_type(const struct dpif_linux_vport *vport)
146{
147 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
148
149 switch (vport->type) {
150 case ODP_VPORT_TYPE_UNSPEC:
151 break;
152
153 case ODP_VPORT_TYPE_NETDEV:
154 return "system";
155
156 case ODP_VPORT_TYPE_INTERNAL:
157 return "internal";
c3827f61 158
c19e6535
BP
159 case ODP_VPORT_TYPE_PATCH:
160 return "patch";
161
162 case ODP_VPORT_TYPE_GRE:
163 if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
164 a)) {
165 break;
166 }
167 return (nl_attr_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
168 ? "ipsec_gre" : "gre");
169
170 case ODP_VPORT_TYPE_CAPWAP:
171 return "capwap";
172
173 case __ODP_VPORT_TYPE_MAX:
174 break;
777ece09 175 }
c19e6535
BP
176
177 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
254f2dc8 178 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
c19e6535 179 return "unknown";
2b9d6589 180}
777ece09 181
2b9d6589 182static int
c3827f61
BP
183netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
184 const struct shash *args,
185 struct netdev_dev **netdev_devp)
2b9d6589 186{
c3827f61 187 const struct vport_class *vport_class = vport_class_cast(netdev_class);
c19e6535 188 struct ofpbuf *options = NULL;
6d9e6eb4 189 struct shash fetched_args;
7feba1ac
BP
190 int dp_ifindex;
191 uint32_t port_no;
c3827f61 192 int error;
2b9d6589 193
6d9e6eb4
BP
194 shash_init(&fetched_args);
195
7feba1ac
BP
196 dp_ifindex = -1;
197 port_no = UINT32_MAX;
6d9e6eb4
BP
198 if (!shash_is_empty(args)) {
199 /* Parse the provided configuration. */
c19e6535 200 options = ofpbuf_new(64);
6d9e6eb4 201 error = vport_class->parse_config(name, netdev_class->type,
c19e6535 202 args, options);
6d9e6eb4
BP
203 } else {
204 /* Fetch an existing configuration from the kernel.
205 *
206 * This case could be ambiguous with initializing a new vport with an
207 * empty configuration, but none of the existing vport classes accept
208 * an empty configuration. */
c19e6535
BP
209 struct dpif_linux_vport reply;
210 struct ofpbuf *buf;
6d9e6eb4 211
c19e6535 212 error = dpif_linux_vport_get(name, &reply, &buf);
6d9e6eb4
BP
213 if (!error) {
214 /* XXX verify correct type */
6d9e6eb4 215 error = vport_class->unparse_config(name, netdev_class->type,
c19e6535
BP
216 reply.options,
217 reply.options_len,
6d9e6eb4
BP
218 &fetched_args);
219 if (error) {
220 VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
221 name, strerror(error));
c19e6535
BP
222 } else {
223 options = ofpbuf_clone_data(reply.options, reply.options_len);
7feba1ac
BP
224 dp_ifindex = reply.dp_ifindex;
225 port_no = reply.port_no;
6d9e6eb4 226 }
c19e6535 227 ofpbuf_delete(buf);
6d9e6eb4
BP
228 } else {
229 VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
230 name, strerror(error));
231 }
232 }
233
234 if (!error) {
235 struct netdev_dev_vport *dev;
2b9d6589 236
6d9e6eb4
BP
237 dev = xmalloc(sizeof *dev);
238 netdev_dev_init(&dev->netdev_dev, name,
239 shash_is_empty(&fetched_args) ? args : &fetched_args,
240 netdev_class);
c19e6535 241 dev->options = options;
7feba1ac
BP
242 dev->dp_ifindex = dp_ifindex;
243 dev->port_no = port_no;
2b9d6589 244
6d9e6eb4 245 *netdev_devp = &dev->netdev_dev;
ba615c2b 246 route_table_register();
c19e6535
BP
247 } else {
248 ofpbuf_delete(options);
2b9d6589 249 }
6d9e6eb4
BP
250
251 shash_destroy(&fetched_args);
252
c3827f61 253 return error;
777ece09
JG
254}
255
2b9d6589
BP
256static void
257netdev_vport_destroy(struct netdev_dev *netdev_dev_)
258{
259 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
260
a132aa96 261 route_table_unregister();
2b9d6589
BP
262 free(netdev_dev);
263}
264
265static int
266netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
267 struct netdev **netdevp)
268{
269 struct netdev_vport *netdev;
270
271 netdev = xmalloc(sizeof *netdev);
272 netdev_init(&netdev->netdev, netdev_dev_);
273
274 *netdevp = &netdev->netdev;
275 return 0;
276}
277
278static void
279netdev_vport_close(struct netdev *netdev_)
280{
281 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
282 free(netdev);
283}
284
285static int
6d9e6eb4 286netdev_vport_set_config(struct netdev_dev *dev_, const struct shash *args)
2b9d6589 287{
c3827f61
BP
288 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
289 const struct vport_class *vport_class = vport_class_cast(netdev_class);
290 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
c19e6535
BP
291 const char *name = netdev_dev_get_name(dev_);
292 struct ofpbuf *options;
c3827f61
BP
293 int error;
294
c19e6535
BP
295 options = ofpbuf_new(64);
296 error = vport_class->parse_config(name, netdev_dev_get_type(dev_),
297 args, options);
298 if (!error
299 && (options->size != dev->options->size
300 || memcmp(options->data, dev->options->data, options->size))) {
301 struct dpif_linux_vport vport;
302
303 dpif_linux_vport_init(&vport);
f0fef760 304 vport.cmd = ODP_VPORT_CMD_SET;
c19e6535
BP
305 vport.name = name;
306 vport.options = options->data;
307 vport.options_len = options->size;
308 error = dpif_linux_vport_transact(&vport, NULL, NULL);
c3827f61
BP
309 if (!error || error == ENODEV) {
310 /* Either reconfiguration succeeded or this vport is not installed
311 * in the kernel (e.g. it hasn't been added to a dpif yet with
312 * dpif_port_add()). */
c19e6535
BP
313 ofpbuf_delete(dev->options);
314 dev->options = options;
315 options = NULL;
316 error = 0;
c3827f61 317 }
2b9d6589 318 }
c19e6535
BP
319 ofpbuf_delete(options);
320
c3827f61 321 return error;
2b9d6589
BP
322}
323
7feba1ac
BP
324static int
325netdev_vport_send(struct netdev *netdev, const void *data, size_t size)
326{
327 struct netdev_dev *dev_ = netdev_get_dev(netdev);
328 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
329
330 if (dev->dp_ifindex == -1) {
331 const char *name = netdev_get_name(netdev);
332 struct dpif_linux_vport reply;
333 struct ofpbuf *buf;
334 int error;
335
336 error = dpif_linux_vport_get(name, &reply, &buf);
337 if (error) {
338 VLOG_ERR_RL(&rl, "%s: failed to query vport for send (%s)",
339 name, strerror(error));
340 return error;
341 }
342 dev->dp_ifindex = reply.dp_ifindex;
343 dev->port_no = reply.port_no;
344 ofpbuf_delete(buf);
345 }
346
347 return dpif_linux_vport_send(dev->dp_ifindex, dev->port_no, data, size);
348}
349
2b9d6589 350static int
777ece09
JG
351netdev_vport_set_etheraddr(struct netdev *netdev,
352 const uint8_t mac[ETH_ADDR_LEN])
353{
c19e6535
BP
354 struct dpif_linux_vport vport;
355 int error;
777ece09 356
c19e6535 357 dpif_linux_vport_init(&vport);
f0fef760 358 vport.cmd = ODP_VPORT_CMD_SET;
c19e6535
BP
359 vport.name = netdev_get_name(netdev);
360 vport.address = mac;
777ece09 361
c19e6535
BP
362 error = dpif_linux_vport_transact(&vport, NULL, NULL);
363 if (!error) {
364 netdev_vport_poll_notify(netdev);
777ece09 365 }
c19e6535 366 return error;
777ece09
JG
367}
368
2b9d6589 369static int
777ece09
JG
370netdev_vport_get_etheraddr(const struct netdev *netdev,
371 uint8_t mac[ETH_ADDR_LEN])
372{
c19e6535
BP
373 struct dpif_linux_vport reply;
374 struct ofpbuf *buf;
375 int error;
777ece09 376
c19e6535
BP
377 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
378 if (!error) {
379 if (reply.address) {
380 memcpy(mac, reply.address, ETH_ADDR_LEN);
381 } else {
382 error = EOPNOTSUPP;
383 }
384 ofpbuf_delete(buf);
777ece09 385 }
c19e6535 386 return error;
777ece09
JG
387}
388
2b9d6589 389static int
777ece09
JG
390netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
391{
c19e6535
BP
392 struct dpif_linux_vport reply;
393 struct ofpbuf *buf;
394 int error;
777ece09 395
c19e6535
BP
396 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
397 if (!error) {
398 *mtup = reply.mtu;
399 ofpbuf_delete(buf);
777ece09 400 }
c19e6535 401 return error;
777ece09
JG
402}
403
777ece09
JG
404int
405netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
406{
c19e6535
BP
407 struct dpif_linux_vport reply;
408 struct ofpbuf *buf;
409 int error;
777ece09 410
c19e6535
BP
411 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
412 if (error) {
413 return error;
414 } else if (!reply.stats) {
415 ofpbuf_delete(buf);
416 return EOPNOTSUPP;
417 }
418
419 stats->rx_packets = reply.stats->rx_packets;
420 stats->tx_packets = reply.stats->tx_packets;
421 stats->rx_bytes = reply.stats->rx_bytes;
422 stats->tx_bytes = reply.stats->tx_bytes;
423 stats->rx_errors = reply.stats->rx_errors;
424 stats->tx_errors = reply.stats->tx_errors;
425 stats->rx_dropped = reply.stats->rx_dropped;
426 stats->tx_dropped = reply.stats->tx_dropped;
427 stats->multicast = reply.stats->multicast;
428 stats->collisions = reply.stats->collisions;
429 stats->rx_length_errors = reply.stats->rx_length_errors;
430 stats->rx_over_errors = reply.stats->rx_over_errors;
431 stats->rx_crc_errors = reply.stats->rx_crc_errors;
432 stats->rx_frame_errors = reply.stats->rx_frame_errors;
433 stats->rx_fifo_errors = reply.stats->rx_fifo_errors;
434 stats->rx_missed_errors = reply.stats->rx_missed_errors;
435 stats->tx_aborted_errors = reply.stats->tx_aborted_errors;
436 stats->tx_carrier_errors = reply.stats->tx_carrier_errors;
437 stats->tx_fifo_errors = reply.stats->tx_fifo_errors;
438 stats->tx_heartbeat_errors = reply.stats->tx_heartbeat_errors;
439 stats->tx_window_errors = reply.stats->tx_window_errors;
440
441 ofpbuf_delete(buf);
777ece09
JG
442
443 return 0;
444}
445
f4b6076a
JG
446int
447netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
448{
c19e6535
BP
449 struct rtnl_link_stats64 rtnl_stats;
450 struct dpif_linux_vport vport;
f4b6076a
JG
451 int err;
452
c19e6535
BP
453 rtnl_stats.rx_packets = stats->rx_packets;
454 rtnl_stats.tx_packets = stats->tx_packets;
455 rtnl_stats.rx_bytes = stats->rx_bytes;
456 rtnl_stats.tx_bytes = stats->tx_bytes;
457 rtnl_stats.rx_errors = stats->rx_errors;
458 rtnl_stats.tx_errors = stats->tx_errors;
459 rtnl_stats.rx_dropped = stats->rx_dropped;
460 rtnl_stats.tx_dropped = stats->tx_dropped;
461 rtnl_stats.multicast = stats->multicast;
462 rtnl_stats.collisions = stats->collisions;
463 rtnl_stats.rx_length_errors = stats->rx_length_errors;
464 rtnl_stats.rx_over_errors = stats->rx_over_errors;
465 rtnl_stats.rx_crc_errors = stats->rx_crc_errors;
466 rtnl_stats.rx_frame_errors = stats->rx_frame_errors;
467 rtnl_stats.rx_fifo_errors = stats->rx_fifo_errors;
468 rtnl_stats.rx_missed_errors = stats->rx_missed_errors;
469 rtnl_stats.tx_aborted_errors = stats->tx_aborted_errors;
470 rtnl_stats.tx_carrier_errors = stats->tx_carrier_errors;
471 rtnl_stats.tx_fifo_errors = stats->tx_fifo_errors;
472 rtnl_stats.tx_heartbeat_errors = stats->tx_heartbeat_errors;
473 rtnl_stats.tx_window_errors = stats->tx_window_errors;
474
475 dpif_linux_vport_init(&vport);
f0fef760 476 vport.cmd = ODP_VPORT_CMD_SET;
c19e6535
BP
477 vport.name = netdev_get_name(netdev);
478 vport.stats = &rtnl_stats;
479
480 err = dpif_linux_vport_transact(&vport, NULL, NULL);
f4b6076a
JG
481
482 /* If the vport layer doesn't know about the device, that doesn't mean it
483 * doesn't exist (after all were able to open it when netdev_open() was
484 * called), it just means that it isn't attached and we'll be getting
485 * stats a different way. */
486 if (err == ENODEV) {
487 err = EOPNOTSUPP;
488 }
489
490 return err;
491}
492
ea763e0e
EJ
493static int
494netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
495{
496 const char *iface = netdev_vport_get_tnl_iface(netdev);
497
498 if (iface) {
a404826e
AE
499 struct netdev *egress_netdev;
500
ea763e0e 501 shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
a404826e
AE
502
503 if (!netdev_open_default(iface, &egress_netdev)) {
504 shash_add(sh, "tunnel_egress_iface_carrier",
505 xstrdup(netdev_get_carrier(egress_netdev)
506 ? "up" : "down"));
507 netdev_close(egress_netdev);
508 }
ea763e0e
EJ
509 }
510
511 return 0;
512}
513
2b9d6589 514static int
777ece09
JG
515netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
516 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
517 enum netdev_flags *old_flagsp)
518{
519 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
520 return EOPNOTSUPP;
521 }
522
523 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
524 return 0;
525}
526
527static char *
528make_poll_name(const struct netdev *netdev)
529{
530 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
531}
532
2b9d6589 533static int
777ece09
JG
534netdev_vport_poll_add(struct netdev *netdev,
535 void (*cb)(struct netdev_notifier *), void *aux,
536 struct netdev_notifier **notifierp)
537{
538 char *poll_name = make_poll_name(netdev);
539 struct netdev_vport_notifier *notifier;
540 struct list *list;
541 struct shash_node *shash_node;
542
0574f71b 543 shash_node = shash_find(&netdev_vport_notifiers, poll_name);
777ece09
JG
544 if (!shash_node) {
545 list = xmalloc(sizeof *list);
546 list_init(list);
eb5f3e93 547 shash_node = shash_add(&netdev_vport_notifiers, poll_name, list);
777ece09
JG
548 } else {
549 list = shash_node->data;
550 }
551
552 notifier = xmalloc(sizeof *notifier);
553 netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
554 list_push_back(list, &notifier->list_node);
555 notifier->shash_node = shash_node;
556
557 *notifierp = &notifier->notifier;
558 free(poll_name);
559
560 return 0;
561}
562
2b9d6589 563static void
777ece09
JG
564netdev_vport_poll_remove(struct netdev_notifier *notifier_)
565{
566 struct netdev_vport_notifier *notifier =
567 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
568
569 struct list *list;
570
571 list = list_remove(&notifier->list_node);
572 if (list_is_empty(list)) {
573 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
574 free(list);
575 }
576
577 free(notifier);
578}
ea83a2fc
EJ
579
580static void
581netdev_vport_run(void)
582{
a132aa96 583 route_table_run();
ea83a2fc
EJ
584}
585
586static void
587netdev_vport_wait(void)
588{
a132aa96 589 route_table_wait();
ea83a2fc
EJ
590}
591\f
592/* get_tnl_iface() implementation. */
ea83a2fc
EJ
593static const char *
594netdev_vport_get_tnl_iface(const struct netdev *netdev)
595{
c19e6535 596 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
ea83a2fc
EJ
597 uint32_t route;
598 struct netdev_dev_vport *ndv;
b46ccdf5 599 static char name[IFNAMSIZ];
ea83a2fc
EJ
600
601 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
c19e6535
BP
602 if (tnl_port_config_from_nlattr(ndv->options->data, ndv->options->size,
603 a)) {
604 return NULL;
605 }
606 route = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
ea83a2fc 607
b46ccdf5
EJ
608 if (route_table_get_name(route, name)) {
609 return name;
ea83a2fc
EJ
610 }
611
612 return NULL;
613}
2b9d6589
BP
614\f
615/* Helper functions. */
777ece09 616
2b9d6589 617static void
777ece09
JG
618netdev_vport_poll_notify(const struct netdev *netdev)
619{
620 char *poll_name = make_poll_name(netdev);
621 struct list *list = shash_find_data(&netdev_vport_notifiers,
622 poll_name);
623
624 if (list) {
625 struct netdev_vport_notifier *notifier;
626
4e8e4213 627 LIST_FOR_EACH (notifier, list_node, list) {
777ece09
JG
628 struct netdev_notifier *n = &notifier->notifier;
629 n->cb(n);
630 }
631 }
632
633 free(poll_name);
634}
2b9d6589
BP
635\f
636/* Code specific to individual vport types. */
637
c19e6535
BP
638static void
639set_key(const struct shash *args, const char *name, uint16_t type,
640 struct ofpbuf *options)
641{
642 const char *s;
643
644 s = shash_find_data(args, name);
645 if (!s) {
646 s = shash_find_data(args, "key");
647 if (!s) {
648 s = "0";
649 }
650 }
651
652 if (!strcmp(s, "flow")) {
653 /* This is the default if no attribute is present. */
654 } else {
655 nl_msg_put_be64(options, type, htonll(strtoull(s, NULL, 0)));
656 }
657}
658
2b9d6589 659static int
6d9e6eb4 660parse_tunnel_config(const char *name, const char *type,
c19e6535 661 const struct shash *args, struct ofpbuf *options)
2b9d6589 662{
e16a28b5
JP
663 bool is_gre = false;
664 bool is_ipsec = false;
2b9d6589 665 struct shash_node *node;
2b9d6589 666 bool ipsec_mech_set = false;
c19e6535
BP
667 ovs_be32 daddr = htonl(0);
668 uint32_t flags;
2b9d6589 669
c19e6535 670 flags = TNL_F_PMTUD | TNL_F_HDR_CACHE;
e16a28b5
JP
671 if (!strcmp(type, "gre")) {
672 is_gre = true;
673 } else if (!strcmp(type, "ipsec_gre")) {
674 is_gre = true;
675 is_ipsec = true;
c19e6535
BP
676 flags |= TNL_F_IPSEC;
677 flags &= ~TNL_F_HDR_CACHE;
e16a28b5
JP
678 }
679
2b9d6589
BP
680 SHASH_FOR_EACH (node, args) {
681 if (!strcmp(node->name, "remote_ip")) {
682 struct in_addr in_addr;
683 if (lookup_ip(node->data, &in_addr)) {
c3827f61 684 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
2b9d6589 685 } else {
c19e6535 686 daddr = in_addr.s_addr;
2b9d6589
BP
687 }
688 } else if (!strcmp(node->name, "local_ip")) {
689 struct in_addr in_addr;
690 if (lookup_ip(node->data, &in_addr)) {
c3827f61 691 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
2b9d6589 692 } else {
c19e6535
BP
693 nl_msg_put_be32(options, ODP_TUNNEL_ATTR_SRC_IPV4,
694 in_addr.s_addr);
2b9d6589
BP
695 }
696 } else if (!strcmp(node->name, "tos")) {
697 if (!strcmp(node->data, "inherit")) {
c19e6535 698 flags |= TNL_F_TOS_INHERIT;
2b9d6589 699 } else {
c19e6535 700 nl_msg_put_u8(options, ODP_TUNNEL_ATTR_TOS, atoi(node->data));
2b9d6589
BP
701 }
702 } else if (!strcmp(node->name, "ttl")) {
703 if (!strcmp(node->data, "inherit")) {
c19e6535 704 flags |= TNL_F_TTL_INHERIT;
2b9d6589 705 } else {
c19e6535 706 nl_msg_put_u8(options, ODP_TUNNEL_ATTR_TTL, atoi(node->data));
2b9d6589
BP
707 }
708 } else if (!strcmp(node->name, "csum") && is_gre) {
709 if (!strcmp(node->data, "true")) {
c19e6535 710 flags |= TNL_F_CSUM;
2b9d6589
BP
711 }
712 } else if (!strcmp(node->name, "pmtud")) {
713 if (!strcmp(node->data, "false")) {
c19e6535 714 flags &= ~TNL_F_PMTUD;
2b9d6589
BP
715 }
716 } else if (!strcmp(node->name, "header_cache")) {
717 if (!strcmp(node->data, "false")) {
c19e6535 718 flags &= ~TNL_F_HDR_CACHE;
2b9d6589 719 }
3c52fa7b
JP
720 } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
721 if (shash_find(args, "certificate")) {
722 ipsec_mech_set = true;
723 } else {
ef7ee76a
JP
724 const char *use_ssl_cert;
725
726 /* If the "use_ssl_cert" is true, then "certificate" and
727 * "private_key" will be pulled from the SSL table. The
728 * use of this option is strongly discouraged, since it
729 * will like be removed when multiple SSL configurations
730 * are supported by OVS.
731 */
732 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
733 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
8283e514
JP
734 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
735 name);
ef7ee76a
JP
736 return EINVAL;
737 }
738 ipsec_mech_set = true;
3c52fa7b
JP
739 }
740 } else if (!strcmp(node->name, "psk") && is_ipsec) {
2b9d6589 741 ipsec_mech_set = true;
ea83a2fc 742 } else if (is_ipsec
3c52fa7b 743 && (!strcmp(node->name, "certificate")
ef7ee76a
JP
744 || !strcmp(node->name, "private_key")
745 || !strcmp(node->name, "use_ssl_cert"))) {
3c52fa7b 746 /* Ignore options not used by the netdev. */
8a86254e
JP
747 } else if (is_gre && (!strcmp(node->name, "key") ||
748 !strcmp(node->name, "in_key") ||
c19e6535
BP
749 !strcmp(node->name, "out_key"))) {
750 /* Handled separately below. */
2b9d6589 751 } else {
c19e6535 752 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
2b9d6589
BP
753 }
754 }
755
3c52fa7b 756 if (is_ipsec) {
5059eff3
JP
757 char *file_name = xasprintf("%s/%s", ovs_rundir(),
758 "ovs-monitor-ipsec.pid");
e7009c36 759 pid_t pid = read_pidfile(file_name);
5059eff3 760 free(file_name);
e7009c36 761 if (pid < 0) {
8283e514
JP
762 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
763 name);
e7009c36
JP
764 return EINVAL;
765 }
5059eff3 766
3c52fa7b 767 if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
8283e514 768 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
3c52fa7b
JP
769 return EINVAL;
770 }
771
772 if (!ipsec_mech_set) {
8283e514
JP
773 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
774 name);
3c52fa7b
JP
775 return EINVAL;
776 }
2b9d6589
BP
777 }
778
c19e6535
BP
779 if (is_gre) {
780 set_key(args, "in_key", ODP_TUNNEL_ATTR_IN_KEY, options);
781 set_key(args, "out_key", ODP_TUNNEL_ATTR_OUT_KEY, options);
782 }
783
784 if (!daddr) {
8283e514
JP
785 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
786 name, type);
2b9d6589
BP
787 return EINVAL;
788 }
c19e6535
BP
789 nl_msg_put_be32(options, ODP_TUNNEL_ATTR_DST_IPV4, daddr);
790
791 nl_msg_put_u32(options, ODP_TUNNEL_ATTR_FLAGS, flags);
2b9d6589
BP
792
793 return 0;
794}
795
c19e6535
BP
796static int
797tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
798 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1])
799{
800 static const struct nl_policy odp_tunnel_policy[] = {
801 [ODP_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32 },
802 [ODP_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32 },
803 [ODP_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
804 [ODP_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
805 [ODP_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
806 [ODP_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
807 [ODP_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
808 };
809 struct ofpbuf buf;
810
811 ofpbuf_use_const(&buf, options, options_len);
812 if (!nl_policy_parse(&buf, 0, odp_tunnel_policy,
813 a, ARRAY_SIZE(odp_tunnel_policy))) {
814 return EINVAL;
815 }
816 return 0;
817}
818
819static uint64_t
820get_be64_or_zero(const struct nlattr *a)
821{
822 return a ? ntohll(nl_attr_get_be64(a)) : 0;
823}
824
2b9d6589 825static int
6d9e6eb4 826unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
c19e6535
BP
827 const struct nlattr *options, size_t options_len,
828 struct shash *args)
6d9e6eb4 829{
c19e6535
BP
830 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
831 ovs_be32 daddr;
832 uint32_t flags;
833 int error;
6d9e6eb4 834
c19e6535
BP
835 error = tnl_port_config_from_nlattr(options, options_len, a);
836 if (error) {
837 return error;
838 }
839
840 flags = nl_attr_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]);
841 if (!(flags & TNL_F_HDR_CACHE) == !(flags & TNL_F_IPSEC)) {
6d9e6eb4 842 smap_add(args, "header_cache",
c19e6535 843 flags & TNL_F_HDR_CACHE ? "true" : "false");
6d9e6eb4 844 }
c19e6535
BP
845
846 daddr = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
847 shash_add(args, "remote_ip", xasprintf(IP_FMT, IP_ARGS(&daddr)));
848
849 if (a[ODP_TUNNEL_ATTR_SRC_IPV4]) {
850 ovs_be32 saddr = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_SRC_IPV4]);
851 shash_add(args, "local_ip", xasprintf(IP_FMT, IP_ARGS(&saddr)));
6d9e6eb4
BP
852 }
853
c19e6535 854 if (!a[ODP_TUNNEL_ATTR_IN_KEY] && !a[ODP_TUNNEL_ATTR_OUT_KEY]) {
6d9e6eb4 855 smap_add(args, "key", "flow");
6d9e6eb4 856 } else {
c19e6535
BP
857 uint64_t in_key = get_be64_or_zero(a[ODP_TUNNEL_ATTR_IN_KEY]);
858 uint64_t out_key = get_be64_or_zero(a[ODP_TUNNEL_ATTR_OUT_KEY]);
859
860 if (in_key && in_key == out_key) {
861 shash_add(args, "key", xasprintf("%"PRIu64, in_key));
862 } else {
863 if (!a[ODP_TUNNEL_ATTR_IN_KEY]) {
864 smap_add(args, "in_key", "flow");
865 } else if (in_key) {
866 shash_add(args, "in_key", xasprintf("%"PRIu64, in_key));
867 }
6d9e6eb4 868
c19e6535
BP
869 if (!a[ODP_TUNNEL_ATTR_OUT_KEY]) {
870 smap_add(args, "out_key", "flow");
871 } else if (out_key) {
872 shash_add(args, "out_key", xasprintf("%"PRIu64, out_key));
873 }
6d9e6eb4
BP
874 }
875 }
876
c19e6535
BP
877 if (flags & TNL_F_TTL_INHERIT) {
878 smap_add(args, "tos", "inherit");
879 } else if (a[ODP_TUNNEL_ATTR_TTL]) {
880 int ttl = nl_attr_get_u8(a[ODP_TUNNEL_ATTR_TTL]);
881 shash_add(args, "tos", xasprintf("%d", ttl));
882 }
883
884 if (flags & TNL_F_TOS_INHERIT) {
6d9e6eb4 885 smap_add(args, "tos", "inherit");
c19e6535
BP
886 } else if (a[ODP_TUNNEL_ATTR_TOS]) {
887 int tos = nl_attr_get_u8(a[ODP_TUNNEL_ATTR_TOS]);
888 shash_add(args, "tos", xasprintf("%d", tos));
6d9e6eb4
BP
889 }
890
c19e6535 891 if (flags & TNL_F_CSUM) {
6d9e6eb4
BP
892 smap_add(args, "csum", "true");
893 }
c19e6535 894 if (!(flags & TNL_F_PMTUD)) {
6d9e6eb4
BP
895 smap_add(args, "pmtud", "false");
896 }
897
898 return 0;
899}
900
901static int
902parse_patch_config(const char *name, const char *type OVS_UNUSED,
c19e6535 903 const struct shash *args, struct ofpbuf *options)
2b9d6589 904{
2b9d6589
BP
905 const char *peer;
906
907 peer = shash_find_data(args, "peer");
908 if (!peer) {
8283e514 909 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
2b9d6589
BP
910 return EINVAL;
911 }
912
913 if (shash_count(args) > 1) {
8283e514 914 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
2b9d6589
BP
915 return EINVAL;
916 }
917
c19e6535 918 if (strlen(peer) >= IFNAMSIZ) {
8283e514 919 VLOG_ERR("%s: patch 'peer' arg too long", name);
2b9d6589
BP
920 return EINVAL;
921 }
922
923 if (!strcmp(name, peer)) {
8283e514 924 VLOG_ERR("%s: patch peer must not be self", name);
2b9d6589
BP
925 return EINVAL;
926 }
927
c19e6535 928 nl_msg_put_string(options, ODP_PATCH_ATTR_PEER, peer);
2b9d6589
BP
929
930 return 0;
931}
6d9e6eb4
BP
932
933static int
934unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
c19e6535
BP
935 const struct nlattr *options, size_t options_len,
936 struct shash *args)
6d9e6eb4 937{
c19e6535
BP
938 static const struct nl_policy odp_patch_policy[] = {
939 [ODP_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
940 .max_len = IFNAMSIZ,
941 .optional = false }
942 };
943
944 struct nlattr *a[ARRAY_SIZE(odp_patch_policy)];
945 struct ofpbuf buf;
946
947 ofpbuf_use_const(&buf, options, options_len);
948 if (!nl_policy_parse(&buf, 0, odp_patch_policy,
949 a, ARRAY_SIZE(odp_patch_policy))) {
950 return EINVAL;
6d9e6eb4
BP
951 }
952
c19e6535 953 smap_add(args, "peer", nl_attr_get_string(a[ODP_PATCH_ATTR_PEER]));
6d9e6eb4
BP
954 return 0;
955}
2b9d6589 956\f
ea763e0e 957#define VPORT_FUNCTIONS(GET_STATUS) \
b46ccdf5 958 NULL, \
ea83a2fc
EJ
959 netdev_vport_run, \
960 netdev_vport_wait, \
2b9d6589
BP
961 \
962 netdev_vport_create, \
963 netdev_vport_destroy, \
6d9e6eb4 964 netdev_vport_set_config, \
2b9d6589
BP
965 \
966 netdev_vport_open, \
967 netdev_vport_close, \
968 \
969 NULL, /* enumerate */ \
970 \
971 NULL, /* recv */ \
972 NULL, /* recv_wait */ \
973 NULL, /* drain */ \
974 \
7feba1ac 975 netdev_vport_send, /* send */ \
2b9d6589
BP
976 NULL, /* send_wait */ \
977 \
978 netdev_vport_set_etheraddr, \
979 netdev_vport_get_etheraddr, \
980 netdev_vport_get_mtu, \
981 NULL, /* get_ifindex */ \
85da620e 982 NULL, /* get_carrier */ \
63331829 983 NULL, /* get_miimon */ \
2b9d6589
BP
984 netdev_vport_get_stats, \
985 netdev_vport_set_stats, \
986 \
987 NULL, /* get_features */ \
988 NULL, /* set_advertisements */ \
989 NULL, /* get_vlan_vid */ \
990 \
991 NULL, /* set_policing */ \
992 NULL, /* get_qos_types */ \
993 NULL, /* get_qos_capabilities */ \
994 NULL, /* get_qos */ \
995 NULL, /* set_qos */ \
996 NULL, /* get_queue */ \
997 NULL, /* set_queue */ \
998 NULL, /* delete_queue */ \
999 NULL, /* get_queue_stats */ \
1000 NULL, /* dump_queues */ \
1001 NULL, /* dump_queue_stats */ \
1002 \
1003 NULL, /* get_in4 */ \
1004 NULL, /* set_in4 */ \
1005 NULL, /* get_in6 */ \
1006 NULL, /* add_router */ \
1007 NULL, /* get_next_hop */ \
ea763e0e 1008 GET_STATUS, \
2b9d6589
BP
1009 NULL, /* arp_lookup */ \
1010 \
1011 netdev_vport_update_flags, \
1012 \
1013 netdev_vport_poll_add, \
1014 netdev_vport_poll_remove,
1015
2b9d6589
BP
1016void
1017netdev_vport_register(void)
1018{
c3827f61 1019 static const struct vport_class vport_classes[] = {
c283069c
BP
1020 { ODP_VPORT_TYPE_GRE,
1021 { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
6d9e6eb4 1022 parse_tunnel_config, unparse_tunnel_config },
c283069c
BP
1023
1024 { ODP_VPORT_TYPE_GRE,
1025 { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
6d9e6eb4 1026 parse_tunnel_config, unparse_tunnel_config },
c283069c
BP
1027
1028 { ODP_VPORT_TYPE_CAPWAP,
1029 { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
6d9e6eb4 1030 parse_tunnel_config, unparse_tunnel_config },
c283069c
BP
1031
1032 { ODP_VPORT_TYPE_PATCH,
1033 { "patch", VPORT_FUNCTIONS(NULL) },
6d9e6eb4 1034 parse_patch_config, unparse_patch_config }
c3827f61
BP
1035 };
1036
1037 int i;
1038
1039 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
1040 netdev_register_provider(&vport_classes[i].netdev_class);
1041 }
2b9d6589 1042}