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