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