]>
Commit | Line | Data |
---|---|---|
777ece09 | 1 | /* |
9284baa2 | 2 | * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc. |
777ece09 JG |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
6fcfff1b | 10 | * Unless required by applicable law or agreed to in writing, software |
777ece09 JG |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
2b9d6589 BP |
18 | |
19 | #include "netdev-vport.h" | |
20 | ||
777ece09 JG |
21 | #include <errno.h> |
22 | #include <fcntl.h> | |
ea83a2fc | 23 | #include <sys/socket.h> |
2b9d6589 | 24 | #include <net/if.h> |
777ece09 JG |
25 | #include <sys/ioctl.h> |
26 | ||
b9298d3f | 27 | #include "byte-order.h" |
5059eff3 JP |
28 | #include "daemon.h" |
29 | #include "dirs.h" | |
0a740f48 | 30 | #include "dpif.h" |
ea83a2fc EJ |
31 | #include "hash.h" |
32 | #include "hmap.h" | |
777ece09 | 33 | #include "list.h" |
2b9d6589 | 34 | #include "netdev-provider.h" |
ea83a2fc | 35 | #include "ofpbuf.h" |
2b9d6589 | 36 | #include "packets.h" |
a132aa96 | 37 | #include "route-table.h" |
777ece09 JG |
38 | #include "shash.h" |
39 | #include "socket-util.h" | |
777ece09 JG |
40 | #include "vlog.h" |
41 | ||
d98e6007 | 42 | VLOG_DEFINE_THIS_MODULE(netdev_vport); |
5136ce49 | 43 | |
79f827fa KM |
44 | /* Default to the OTV port, per the VXLAN IETF draft. */ |
45 | #define VXLAN_DST_PORT 8472 | |
46 | ||
a6ae068b LJ |
47 | #define LISP_DST_PORT 4341 |
48 | ||
f431bf7d EJ |
49 | #define DEFAULT_TTL 64 |
50 | ||
2b9d6589 BP |
51 | struct netdev_dev_vport { |
52 | struct netdev_dev netdev_dev; | |
ac4d3bcb | 53 | unsigned int change_seq; |
35b769cb | 54 | uint8_t etheraddr[ETH_ADDR_LEN]; |
b9ad7294 | 55 | struct netdev_stats stats; |
0a740f48 EJ |
56 | |
57 | /* Tunnels. */ | |
f431bf7d | 58 | struct netdev_tunnel_config tnl_cfg; |
0a740f48 EJ |
59 | |
60 | /* Patch Ports. */ | |
0a740f48 | 61 | char *peer; |
2b9d6589 BP |
62 | }; |
63 | ||
2b9d6589 | 64 | struct vport_class { |
b9ad7294 | 65 | const char *dpif_port; |
c3827f61 | 66 | struct netdev_class netdev_class; |
2b9d6589 BP |
67 | }; |
68 | ||
2b9d6589 | 69 | static int netdev_vport_create(const struct netdev_class *, const char *, |
de5cdb90 | 70 | struct netdev_dev **); |
f18a39b7 | 71 | static int get_patch_config(struct netdev_dev *, struct smap *args); |
56b11f0b | 72 | static int get_tunnel_config(struct netdev_dev *, struct smap *args); |
b9ad7294 | 73 | static void netdev_vport_poll_notify(struct netdev_dev_vport *); |
2b9d6589 BP |
74 | |
75 | static bool | |
76 | is_vport_class(const struct netdev_class *class) | |
777ece09 | 77 | { |
2b9d6589 BP |
78 | return class->create == netdev_vport_create; |
79 | } | |
777ece09 | 80 | |
2b9d6589 BP |
81 | static const struct vport_class * |
82 | vport_class_cast(const struct netdev_class *class) | |
83 | { | |
cb22974d | 84 | ovs_assert(is_vport_class(class)); |
2b9d6589 BP |
85 | return CONTAINER_OF(class, struct vport_class, netdev_class); |
86 | } | |
87 | ||
88 | static struct netdev_dev_vport * | |
89 | netdev_dev_vport_cast(const struct netdev_dev *netdev_dev) | |
90 | { | |
cb22974d | 91 | ovs_assert(is_vport_class(netdev_dev_get_class(netdev_dev))); |
2b9d6589 BP |
92 | return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev); |
93 | } | |
94 | ||
df67d7ae EJ |
95 | static struct netdev_dev_vport * |
96 | netdev_vport_get_dev(const struct netdev *netdev) | |
97 | { | |
98 | return netdev_dev_vport_cast(netdev_get_dev(netdev)); | |
99 | } | |
100 | ||
f431bf7d EJ |
101 | static const struct netdev_tunnel_config * |
102 | get_netdev_tunnel_config(const struct netdev_dev *netdev_dev) | |
103 | { | |
104 | return &netdev_dev_vport_cast(netdev_dev)->tnl_cfg; | |
105 | } | |
106 | ||
0a740f48 EJ |
107 | bool |
108 | netdev_vport_is_patch(const struct netdev *netdev) | |
109 | { | |
f18a39b7 JG |
110 | const struct netdev_dev *dev = netdev_get_dev(netdev); |
111 | const struct netdev_class *class = netdev_dev_get_class(dev); | |
112 | ||
c060c4cf | 113 | return class->get_config == get_patch_config; |
0a740f48 EJ |
114 | } |
115 | ||
56b11f0b | 116 | static bool |
a6ae068b | 117 | netdev_vport_needs_dst_port(const struct netdev_dev *dev) |
56b11f0b | 118 | { |
56b11f0b KM |
119 | const struct netdev_class *class = netdev_dev_get_class(dev); |
120 | const char *type = netdev_dev_get_type(dev); | |
121 | ||
a6ae068b LJ |
122 | return (class->get_config == get_tunnel_config && |
123 | (!strcmp("vxlan", type) || !strcmp("lisp", type))); | |
56b11f0b KM |
124 | } |
125 | ||
de281153 EJ |
126 | const char * |
127 | netdev_vport_get_dpif_port(const struct netdev *netdev) | |
128 | { | |
b9ad7294 EJ |
129 | const struct netdev_dev *dev = netdev_get_dev(netdev); |
130 | const struct netdev_class *class = netdev_dev_get_class(dev); | |
131 | const char *dpif_port; | |
c19e6535 | 132 | |
a6ae068b | 133 | if (netdev_vport_needs_dst_port(dev)) { |
56b11f0b KM |
134 | const struct netdev_dev_vport *vport = netdev_vport_get_dev(netdev); |
135 | const char *type = netdev_dev_get_type(dev); | |
a6ae068b | 136 | static char dpif_port_combined[IFNAMSIZ]; |
56b11f0b KM |
137 | |
138 | /* | |
139 | * Note: IFNAMSIZ is 16 bytes long. The maximum length of a VXLAN | |
a6ae068b LJ |
140 | * or LISP port name below is 15 or 14 bytes respectively. Still, |
141 | * assert here on the size of strlen(type) in case that changes | |
142 | * in the future. | |
56b11f0b KM |
143 | */ |
144 | ovs_assert(strlen(type) + 10 < IFNAMSIZ); | |
a6ae068b | 145 | snprintf(dpif_port_combined, IFNAMSIZ, "%s_sys_%d", type, |
56b11f0b | 146 | ntohs(vport->tnl_cfg.dst_port)); |
a6ae068b | 147 | return dpif_port_combined; |
56b11f0b KM |
148 | } else { |
149 | dpif_port = (is_vport_class(class) | |
150 | ? vport_class_cast(class)->dpif_port | |
151 | : NULL); | |
152 | } | |
153 | ||
b9ad7294 | 154 | return dpif_port ? dpif_port : netdev_get_name(netdev); |
2b9d6589 | 155 | } |
777ece09 | 156 | |
2b9d6589 | 157 | static int |
c3827f61 | 158 | netdev_vport_create(const struct netdev_class *netdev_class, const char *name, |
c3827f61 | 159 | struct netdev_dev **netdev_devp) |
2b9d6589 | 160 | { |
de5cdb90 | 161 | struct netdev_dev_vport *dev; |
6d9e6eb4 | 162 | |
f431bf7d | 163 | dev = xzalloc(sizeof *dev); |
de5cdb90 | 164 | netdev_dev_init(&dev->netdev_dev, name, netdev_class); |
de5cdb90 | 165 | dev->change_seq = 1; |
35b769cb | 166 | eth_addr_random(dev->etheraddr); |
6d9e6eb4 | 167 | |
de5cdb90 BP |
168 | *netdev_devp = &dev->netdev_dev; |
169 | route_table_register(); | |
6d9e6eb4 | 170 | |
de5cdb90 | 171 | return 0; |
777ece09 JG |
172 | } |
173 | ||
2b9d6589 BP |
174 | static void |
175 | netdev_vport_destroy(struct netdev_dev *netdev_dev_) | |
176 | { | |
177 | struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_); | |
178 | ||
a132aa96 | 179 | route_table_unregister(); |
0a740f48 | 180 | free(netdev_dev->peer); |
2b9d6589 BP |
181 | free(netdev_dev); |
182 | } | |
183 | ||
184 | static int | |
2f8999f5 | 185 | netdev_vport_open(struct netdev_dev *netdev_dev, struct netdev **netdevp) |
2b9d6589 | 186 | { |
2f8999f5 EJ |
187 | *netdevp = xmalloc(sizeof **netdevp); |
188 | netdev_init(*netdevp, netdev_dev); | |
2b9d6589 BP |
189 | return 0; |
190 | } | |
191 | ||
192 | static void | |
2f8999f5 | 193 | netdev_vport_close(struct netdev *netdev) |
2b9d6589 | 194 | { |
2b9d6589 BP |
195 | free(netdev); |
196 | } | |
197 | ||
2b9d6589 | 198 | static int |
777ece09 JG |
199 | netdev_vport_set_etheraddr(struct netdev *netdev, |
200 | const uint8_t mac[ETH_ADDR_LEN]) | |
201 | { | |
b9ad7294 EJ |
202 | struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev); |
203 | memcpy(dev->etheraddr, mac, ETH_ADDR_LEN); | |
204 | netdev_vport_poll_notify(dev); | |
35b769cb | 205 | return 0; |
777ece09 JG |
206 | } |
207 | ||
2b9d6589 | 208 | static int |
777ece09 JG |
209 | netdev_vport_get_etheraddr(const struct netdev *netdev, |
210 | uint8_t mac[ETH_ADDR_LEN]) | |
211 | { | |
35b769cb EJ |
212 | memcpy(mac, netdev_vport_get_dev(netdev)->etheraddr, ETH_ADDR_LEN); |
213 | return 0; | |
777ece09 JG |
214 | } |
215 | ||
ea763e0e | 216 | static int |
275707c3 | 217 | tunnel_get_status(const struct netdev *netdev, struct smap *smap) |
ea763e0e | 218 | { |
275707c3 EJ |
219 | static char iface[IFNAMSIZ]; |
220 | ovs_be32 route; | |
ea763e0e | 221 | |
f431bf7d | 222 | route = netdev_vport_get_dev(netdev)->tnl_cfg.ip_dst; |
275707c3 | 223 | if (route_table_get_name(route, iface)) { |
a404826e AE |
224 | struct netdev *egress_netdev; |
225 | ||
79f1cbe9 | 226 | smap_add(smap, "tunnel_egress_iface", iface); |
a404826e | 227 | |
18812dff | 228 | if (!netdev_open(iface, "system", &egress_netdev)) { |
79f1cbe9 EJ |
229 | smap_add(smap, "tunnel_egress_iface_carrier", |
230 | netdev_get_carrier(egress_netdev) ? "up" : "down"); | |
a404826e AE |
231 | netdev_close(egress_netdev); |
232 | } | |
ea763e0e EJ |
233 | } |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
2b9d6589 | 238 | static int |
777ece09 JG |
239 | netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED, |
240 | enum netdev_flags off, enum netdev_flags on OVS_UNUSED, | |
241 | enum netdev_flags *old_flagsp) | |
242 | { | |
243 | if (off & (NETDEV_UP | NETDEV_PROMISC)) { | |
244 | return EOPNOTSUPP; | |
245 | } | |
246 | ||
247 | *old_flagsp = NETDEV_UP | NETDEV_PROMISC; | |
248 | return 0; | |
249 | } | |
250 | ||
ac4d3bcb EJ |
251 | static unsigned int |
252 | netdev_vport_change_seq(const struct netdev *netdev) | |
253 | { | |
df67d7ae | 254 | return netdev_vport_get_dev(netdev)->change_seq; |
ac4d3bcb EJ |
255 | } |
256 | ||
ea83a2fc EJ |
257 | static void |
258 | netdev_vport_run(void) | |
259 | { | |
a132aa96 | 260 | route_table_run(); |
ea83a2fc EJ |
261 | } |
262 | ||
263 | static void | |
264 | netdev_vport_wait(void) | |
265 | { | |
a132aa96 | 266 | route_table_wait(); |
ea83a2fc EJ |
267 | } |
268 | \f | |
2b9d6589 | 269 | /* Helper functions. */ |
777ece09 | 270 | |
2b9d6589 | 271 | static void |
b9ad7294 | 272 | netdev_vport_poll_notify(struct netdev_dev_vport *ndv) |
777ece09 | 273 | { |
ac4d3bcb EJ |
274 | ndv->change_seq++; |
275 | if (!ndv->change_seq) { | |
276 | ndv->change_seq++; | |
277 | } | |
777ece09 | 278 | } |
2b9d6589 | 279 | \f |
0a740f48 | 280 | /* Code specific to tunnel types. */ |
2b9d6589 | 281 | |
f431bf7d EJ |
282 | static ovs_be64 |
283 | parse_key(const struct smap *args, const char *name, | |
284 | bool *present, bool *flow) | |
c19e6535 BP |
285 | { |
286 | const char *s; | |
287 | ||
f431bf7d EJ |
288 | *present = false; |
289 | *flow = false; | |
290 | ||
79f1cbe9 | 291 | s = smap_get(args, name); |
c19e6535 | 292 | if (!s) { |
79f1cbe9 | 293 | s = smap_get(args, "key"); |
c19e6535 | 294 | if (!s) { |
f431bf7d | 295 | return 0; |
c19e6535 BP |
296 | } |
297 | } | |
298 | ||
f431bf7d EJ |
299 | *present = true; |
300 | ||
c19e6535 | 301 | if (!strcmp(s, "flow")) { |
f431bf7d EJ |
302 | *flow = true; |
303 | return 0; | |
c19e6535 | 304 | } else { |
f431bf7d | 305 | return htonll(strtoull(s, NULL, 0)); |
c19e6535 BP |
306 | } |
307 | } | |
308 | ||
2b9d6589 | 309 | static int |
0a740f48 | 310 | set_tunnel_config(struct netdev_dev *dev_, const struct smap *args) |
2b9d6589 | 311 | { |
0a740f48 EJ |
312 | struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_); |
313 | const char *name = netdev_dev_get_name(dev_); | |
314 | const char *type = netdev_dev_get_type(dev_); | |
f431bf7d EJ |
315 | bool ipsec_mech_set, needs_dst_port, has_csum; |
316 | struct netdev_tunnel_config tnl_cfg; | |
79f1cbe9 | 317 | struct smap_node *node; |
f431bf7d | 318 | |
f431bf7d EJ |
319 | has_csum = strstr(type, "gre"); |
320 | ipsec_mech_set = false; | |
321 | memset(&tnl_cfg, 0, sizeof tnl_cfg); | |
2b9d6589 | 322 | |
a6ae068b | 323 | needs_dst_port = netdev_vport_needs_dst_port(dev_); |
f431bf7d | 324 | tnl_cfg.ipsec = strstr(type, "ipsec"); |
f431bf7d | 325 | tnl_cfg.dont_fragment = true; |
e16a28b5 | 326 | |
79f1cbe9 EJ |
327 | SMAP_FOR_EACH (node, args) { |
328 | if (!strcmp(node->key, "remote_ip")) { | |
2b9d6589 | 329 | struct in_addr in_addr; |
79f1cbe9 | 330 | if (lookup_ip(node->value, &in_addr)) { |
c3827f61 | 331 | VLOG_WARN("%s: bad %s 'remote_ip'", name, type); |
2b9d6589 | 332 | } else { |
f431bf7d | 333 | tnl_cfg.ip_dst = in_addr.s_addr; |
2b9d6589 | 334 | } |
79f1cbe9 | 335 | } else if (!strcmp(node->key, "local_ip")) { |
2b9d6589 | 336 | struct in_addr in_addr; |
79f1cbe9 | 337 | if (lookup_ip(node->value, &in_addr)) { |
c3827f61 | 338 | VLOG_WARN("%s: bad %s 'local_ip'", name, type); |
2b9d6589 | 339 | } else { |
f431bf7d | 340 | tnl_cfg.ip_src = in_addr.s_addr; |
2b9d6589 | 341 | } |
79f1cbe9 EJ |
342 | } else if (!strcmp(node->key, "tos")) { |
343 | if (!strcmp(node->value, "inherit")) { | |
f431bf7d | 344 | tnl_cfg.tos_inherit = true; |
2b9d6589 | 345 | } else { |
3fca7064 PS |
346 | char *endptr; |
347 | int tos; | |
79f1cbe9 | 348 | tos = strtol(node->value, &endptr, 0); |
91aff446 | 349 | if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) { |
f431bf7d | 350 | tnl_cfg.tos = tos; |
91aff446 BP |
351 | } else { |
352 | VLOG_WARN("%s: invalid TOS %s", name, node->value); | |
3fca7064 | 353 | } |
2b9d6589 | 354 | } |
79f1cbe9 EJ |
355 | } else if (!strcmp(node->key, "ttl")) { |
356 | if (!strcmp(node->value, "inherit")) { | |
f431bf7d | 357 | tnl_cfg.ttl_inherit = true; |
2b9d6589 | 358 | } else { |
f431bf7d | 359 | tnl_cfg.ttl = atoi(node->value); |
2b9d6589 | 360 | } |
79f827fa | 361 | } else if (!strcmp(node->key, "dst_port") && needs_dst_port) { |
f431bf7d | 362 | tnl_cfg.dst_port = htons(atoi(node->value)); |
f431bf7d | 363 | } else if (!strcmp(node->key, "csum") && has_csum) { |
79f1cbe9 | 364 | if (!strcmp(node->value, "true")) { |
f431bf7d | 365 | tnl_cfg.csum = true; |
2b9d6589 | 366 | } |
79f1cbe9 EJ |
367 | } else if (!strcmp(node->key, "df_default")) { |
368 | if (!strcmp(node->value, "false")) { | |
f431bf7d | 369 | tnl_cfg.dont_fragment = false; |
66409d1b | 370 | } |
f431bf7d | 371 | } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) { |
79f1cbe9 | 372 | if (smap_get(args, "certificate")) { |
3c52fa7b JP |
373 | ipsec_mech_set = true; |
374 | } else { | |
ef7ee76a JP |
375 | const char *use_ssl_cert; |
376 | ||
377 | /* If the "use_ssl_cert" is true, then "certificate" and | |
378 | * "private_key" will be pulled from the SSL table. The | |
379 | * use of this option is strongly discouraged, since it | |
380 | * will like be removed when multiple SSL configurations | |
381 | * are supported by OVS. | |
382 | */ | |
79f1cbe9 | 383 | use_ssl_cert = smap_get(args, "use_ssl_cert"); |
ef7ee76a | 384 | if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) { |
8283e514 JP |
385 | VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument", |
386 | name); | |
b9ad7294 | 387 | return EINVAL; |
ef7ee76a JP |
388 | } |
389 | ipsec_mech_set = true; | |
3c52fa7b | 390 | } |
f431bf7d | 391 | } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) { |
2b9d6589 | 392 | ipsec_mech_set = true; |
f431bf7d | 393 | } else if (tnl_cfg.ipsec |
79f1cbe9 EJ |
394 | && (!strcmp(node->key, "certificate") |
395 | || !strcmp(node->key, "private_key") | |
396 | || !strcmp(node->key, "use_ssl_cert"))) { | |
3c52fa7b | 397 | /* Ignore options not used by the netdev. */ |
79f1cbe9 EJ |
398 | } else if (!strcmp(node->key, "key") || |
399 | !strcmp(node->key, "in_key") || | |
400 | !strcmp(node->key, "out_key")) { | |
c19e6535 | 401 | /* Handled separately below. */ |
2b9d6589 | 402 | } else { |
79f1cbe9 | 403 | VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key); |
2b9d6589 BP |
404 | } |
405 | } | |
406 | ||
79f827fa | 407 | /* Add a default destination port for VXLAN if none specified. */ |
a6ae068b | 408 | if (!strcmp(type, "vxlan") && !tnl_cfg.dst_port) { |
f431bf7d | 409 | tnl_cfg.dst_port = htons(VXLAN_DST_PORT); |
79f827fa KM |
410 | } |
411 | ||
a6ae068b LJ |
412 | /* Add a default destination port for LISP if none specified. */ |
413 | if (!strcmp(type, "lisp") && !tnl_cfg.dst_port) { | |
414 | tnl_cfg.dst_port = htons(LISP_DST_PORT); | |
415 | } | |
416 | ||
f431bf7d | 417 | if (tnl_cfg.ipsec) { |
2a586a5c | 418 | static pid_t pid = 0; |
900f7601 | 419 | if (pid <= 0) { |
2a586a5c AS |
420 | char *file_name = xasprintf("%s/%s", ovs_rundir(), |
421 | "ovs-monitor-ipsec.pid"); | |
422 | pid = read_pidfile(file_name); | |
423 | free(file_name); | |
424 | } | |
425 | ||
e7009c36 | 426 | if (pid < 0) { |
8283e514 JP |
427 | VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon", |
428 | name); | |
b9ad7294 | 429 | return EINVAL; |
e7009c36 | 430 | } |
5059eff3 | 431 | |
79f1cbe9 | 432 | if (smap_get(args, "peer_cert") && smap_get(args, "psk")) { |
8283e514 | 433 | VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name); |
b9ad7294 | 434 | return EINVAL; |
3c52fa7b JP |
435 | } |
436 | ||
437 | if (!ipsec_mech_set) { | |
8283e514 JP |
438 | VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument", |
439 | name); | |
b9ad7294 | 440 | return EINVAL; |
3c52fa7b | 441 | } |
2b9d6589 BP |
442 | } |
443 | ||
f431bf7d | 444 | if (!tnl_cfg.ip_dst) { |
8283e514 JP |
445 | VLOG_ERR("%s: %s type requires valid 'remote_ip' argument", |
446 | name, type); | |
b9ad7294 | 447 | return EINVAL; |
2b9d6589 | 448 | } |
c19e6535 | 449 | |
f431bf7d EJ |
450 | if (tnl_cfg.ip_src) { |
451 | if (ip_is_multicast(tnl_cfg.ip_dst)) { | |
b37e6334 | 452 | VLOG_WARN("%s: remote_ip is multicast, ignoring local_ip", name); |
f431bf7d | 453 | tnl_cfg.ip_src = 0; |
b37e6334 BP |
454 | } |
455 | } | |
456 | ||
f431bf7d EJ |
457 | if (!tnl_cfg.ttl) { |
458 | tnl_cfg.ttl = DEFAULT_TTL; | |
459 | } | |
460 | ||
461 | tnl_cfg.in_key = parse_key(args, "in_key", | |
462 | &tnl_cfg.in_key_present, | |
463 | &tnl_cfg.in_key_flow); | |
f431bf7d EJ |
464 | |
465 | tnl_cfg.out_key = parse_key(args, "out_key", | |
466 | &tnl_cfg.out_key_present, | |
467 | &tnl_cfg.out_key_flow); | |
2b9d6589 | 468 | |
0a740f48 | 469 | dev->tnl_cfg = tnl_cfg; |
b9ad7294 | 470 | netdev_vport_poll_notify(dev); |
f431bf7d | 471 | |
c19e6535 BP |
472 | return 0; |
473 | } | |
474 | ||
2b9d6589 | 475 | static int |
b9ad7294 | 476 | get_tunnel_config(struct netdev_dev *dev, struct smap *args) |
6d9e6eb4 | 477 | { |
b9ad7294 EJ |
478 | const struct netdev_tunnel_config *tnl_cfg = |
479 | &netdev_dev_vport_cast(dev)->tnl_cfg; | |
6d9e6eb4 | 480 | |
b9ad7294 EJ |
481 | if (tnl_cfg->ip_dst) { |
482 | smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(tnl_cfg->ip_dst)); | |
0a740f48 EJ |
483 | } |
484 | ||
b9ad7294 EJ |
485 | if (tnl_cfg->ip_src) { |
486 | smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(tnl_cfg->ip_src)); | |
7f804ea5 | 487 | } |
c19e6535 | 488 | |
b9ad7294 | 489 | if (tnl_cfg->in_key_flow && tnl_cfg->out_key_flow) { |
6d9e6eb4 | 490 | smap_add(args, "key", "flow"); |
b9ad7294 EJ |
491 | } else if (tnl_cfg->in_key_present && tnl_cfg->out_key_present |
492 | && tnl_cfg->in_key == tnl_cfg->out_key) { | |
493 | smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg->in_key)); | |
6d9e6eb4 | 494 | } else { |
b9ad7294 EJ |
495 | if (tnl_cfg->in_key_flow) { |
496 | smap_add(args, "in_key", "flow"); | |
497 | } else if (tnl_cfg->in_key_present) { | |
498 | smap_add_format(args, "in_key", "%"PRIu64, | |
499 | ntohll(tnl_cfg->in_key)); | |
500 | } | |
6d9e6eb4 | 501 | |
b9ad7294 EJ |
502 | if (tnl_cfg->out_key_flow) { |
503 | smap_add(args, "out_key", "flow"); | |
504 | } else if (tnl_cfg->out_key_present) { | |
505 | smap_add_format(args, "out_key", "%"PRIu64, | |
506 | ntohll(tnl_cfg->out_key)); | |
6d9e6eb4 BP |
507 | } |
508 | } | |
509 | ||
b9ad7294 | 510 | if (tnl_cfg->ttl_inherit) { |
62827e6a | 511 | smap_add(args, "ttl", "inherit"); |
b9ad7294 EJ |
512 | } else if (tnl_cfg->ttl != DEFAULT_TTL) { |
513 | smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg->ttl); | |
c19e6535 BP |
514 | } |
515 | ||
b9ad7294 | 516 | if (tnl_cfg->tos_inherit) { |
6d9e6eb4 | 517 | smap_add(args, "tos", "inherit"); |
b9ad7294 EJ |
518 | } else if (tnl_cfg->tos) { |
519 | smap_add_format(args, "tos", "0x%x", tnl_cfg->tos); | |
6d9e6eb4 BP |
520 | } |
521 | ||
b9ad7294 EJ |
522 | if (tnl_cfg->dst_port) { |
523 | uint16_t dst_port = ntohs(tnl_cfg->dst_port); | |
79f827fa KM |
524 | if (dst_port != VXLAN_DST_PORT) { |
525 | smap_add_format(args, "dst_port", "%d", dst_port); | |
526 | } | |
527 | } | |
528 | ||
b9ad7294 | 529 | if (tnl_cfg->csum) { |
6d9e6eb4 BP |
530 | smap_add(args, "csum", "true"); |
531 | } | |
8a9ff93a | 532 | |
b9ad7294 | 533 | if (!tnl_cfg->dont_fragment) { |
66409d1b AE |
534 | smap_add(args, "df_default", "false"); |
535 | } | |
6d9e6eb4 BP |
536 | |
537 | return 0; | |
538 | } | |
0a740f48 EJ |
539 | \f |
540 | /* Code specific to patch ports. */ | |
541 | ||
542 | const char * | |
543 | netdev_vport_patch_peer(const struct netdev *netdev) | |
544 | { | |
545 | return netdev_vport_is_patch(netdev) | |
546 | ? netdev_vport_get_dev(netdev)->peer | |
547 | : NULL; | |
548 | } | |
549 | ||
550 | void | |
b9ad7294 | 551 | netdev_vport_inc_rx(const struct netdev *netdev, |
0a740f48 EJ |
552 | const struct dpif_flow_stats *stats) |
553 | { | |
b9ad7294 | 554 | if (is_vport_class(netdev_dev_get_class(netdev_get_dev(netdev)))) { |
0a740f48 EJ |
555 | struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev); |
556 | dev->stats.rx_packets += stats->n_packets; | |
557 | dev->stats.rx_bytes += stats->n_bytes; | |
558 | } | |
559 | } | |
560 | ||
561 | void | |
b9ad7294 EJ |
562 | netdev_vport_inc_tx(const struct netdev *netdev, |
563 | const struct dpif_flow_stats *stats) | |
0a740f48 | 564 | { |
b9ad7294 | 565 | if (is_vport_class(netdev_dev_get_class(netdev_get_dev(netdev)))) { |
0a740f48 EJ |
566 | struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev); |
567 | dev->stats.tx_packets += stats->n_packets; | |
568 | dev->stats.tx_bytes += stats->n_bytes; | |
569 | } | |
570 | } | |
571 | ||
572 | static int | |
573 | get_patch_config(struct netdev_dev *dev_, struct smap *args) | |
574 | { | |
575 | struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_); | |
576 | ||
577 | if (dev->peer) { | |
578 | smap_add(args, "peer", dev->peer); | |
579 | } | |
580 | return 0; | |
581 | } | |
6d9e6eb4 BP |
582 | |
583 | static int | |
0a740f48 | 584 | set_patch_config(struct netdev_dev *dev_, const struct smap *args) |
2b9d6589 | 585 | { |
0a740f48 EJ |
586 | struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_); |
587 | const char *name = netdev_dev_get_name(dev_); | |
2b9d6589 BP |
588 | const char *peer; |
589 | ||
79f1cbe9 | 590 | peer = smap_get(args, "peer"); |
2b9d6589 | 591 | if (!peer) { |
8283e514 | 592 | VLOG_ERR("%s: patch type requires valid 'peer' argument", name); |
2b9d6589 BP |
593 | return EINVAL; |
594 | } | |
595 | ||
79f1cbe9 | 596 | if (smap_count(args) > 1) { |
8283e514 | 597 | VLOG_ERR("%s: patch type takes only a 'peer' argument", name); |
2b9d6589 BP |
598 | return EINVAL; |
599 | } | |
600 | ||
2b9d6589 | 601 | if (!strcmp(name, peer)) { |
8283e514 | 602 | VLOG_ERR("%s: patch peer must not be self", name); |
2b9d6589 BP |
603 | return EINVAL; |
604 | } | |
605 | ||
0a740f48 EJ |
606 | free(dev->peer); |
607 | dev->peer = xstrdup(peer); | |
2b9d6589 BP |
608 | |
609 | return 0; | |
610 | } | |
6d9e6eb4 BP |
611 | |
612 | static int | |
b9ad7294 | 613 | get_stats(const struct netdev *netdev, struct netdev_stats *stats) |
0a740f48 EJ |
614 | { |
615 | struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev); | |
616 | memcpy(stats, &dev->stats, sizeof *stats); | |
6d9e6eb4 BP |
617 | return 0; |
618 | } | |
2b9d6589 | 619 | \f |
0a740f48 | 620 | #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \ |
b9ad7294 | 621 | GET_TUNNEL_CONFIG, GET_STATUS) \ |
b46ccdf5 | 622 | NULL, \ |
ea83a2fc EJ |
623 | netdev_vport_run, \ |
624 | netdev_vport_wait, \ | |
2b9d6589 BP |
625 | \ |
626 | netdev_vport_create, \ | |
627 | netdev_vport_destroy, \ | |
0a740f48 EJ |
628 | GET_CONFIG, \ |
629 | SET_CONFIG, \ | |
f431bf7d | 630 | GET_TUNNEL_CONFIG, \ |
2b9d6589 BP |
631 | \ |
632 | netdev_vport_open, \ | |
633 | netdev_vport_close, \ | |
634 | \ | |
7b6b0ef4 | 635 | NULL, /* listen */ \ |
2b9d6589 BP |
636 | NULL, /* recv */ \ |
637 | NULL, /* recv_wait */ \ | |
638 | NULL, /* drain */ \ | |
639 | \ | |
552e20d0 | 640 | NULL, /* send */ \ |
2b9d6589 BP |
641 | NULL, /* send_wait */ \ |
642 | \ | |
643 | netdev_vport_set_etheraddr, \ | |
644 | netdev_vport_get_etheraddr, \ | |
14622f22 BP |
645 | NULL, /* get_mtu */ \ |
646 | NULL, /* set_mtu */ \ | |
2b9d6589 | 647 | NULL, /* get_ifindex */ \ |
85da620e | 648 | NULL, /* get_carrier */ \ |
65c3058c | 649 | NULL, /* get_carrier_resets */ \ |
63331829 | 650 | NULL, /* get_miimon */ \ |
b9ad7294 | 651 | get_stats, \ |
2f31a822 | 652 | NULL, /* set_stats */ \ |
2b9d6589 BP |
653 | \ |
654 | NULL, /* get_features */ \ | |
655 | NULL, /* set_advertisements */ \ | |
2b9d6589 BP |
656 | \ |
657 | NULL, /* set_policing */ \ | |
658 | NULL, /* get_qos_types */ \ | |
659 | NULL, /* get_qos_capabilities */ \ | |
660 | NULL, /* get_qos */ \ | |
661 | NULL, /* set_qos */ \ | |
662 | NULL, /* get_queue */ \ | |
663 | NULL, /* set_queue */ \ | |
664 | NULL, /* delete_queue */ \ | |
665 | NULL, /* get_queue_stats */ \ | |
666 | NULL, /* dump_queues */ \ | |
667 | NULL, /* dump_queue_stats */ \ | |
668 | \ | |
669 | NULL, /* get_in4 */ \ | |
670 | NULL, /* set_in4 */ \ | |
671 | NULL, /* get_in6 */ \ | |
672 | NULL, /* add_router */ \ | |
673 | NULL, /* get_next_hop */ \ | |
ea763e0e | 674 | GET_STATUS, \ |
2b9d6589 BP |
675 | NULL, /* arp_lookup */ \ |
676 | \ | |
677 | netdev_vport_update_flags, \ | |
678 | \ | |
ac4d3bcb | 679 | netdev_vport_change_seq |
2b9d6589 | 680 | |
c060c4cf EJ |
681 | #define TUNNEL_CLASS(NAME, DPIF_PORT) \ |
682 | { DPIF_PORT, \ | |
0a740f48 EJ |
683 | { NAME, VPORT_FUNCTIONS(get_tunnel_config, \ |
684 | set_tunnel_config, \ | |
685 | get_netdev_tunnel_config, \ | |
0a740f48 | 686 | tunnel_get_status) }} |
db078f85 | 687 | |
2b9d6589 | 688 | void |
c060c4cf | 689 | netdev_vport_tunnel_register(void) |
2b9d6589 | 690 | { |
c3827f61 | 691 | static const struct vport_class vport_classes[] = { |
c060c4cf EJ |
692 | TUNNEL_CLASS("gre", "gre_system"), |
693 | TUNNEL_CLASS("ipsec_gre", "gre_system"), | |
694 | TUNNEL_CLASS("gre64", "gre64_system"), | |
695 | TUNNEL_CLASS("ipsec_gre64", "gre64_system"), | |
a6ae068b LJ |
696 | TUNNEL_CLASS("vxlan", "vxlan_system"), |
697 | TUNNEL_CLASS("lisp", "lisp_system") | |
c3827f61 BP |
698 | }; |
699 | ||
700 | int i; | |
701 | ||
702 | for (i = 0; i < ARRAY_SIZE(vport_classes); i++) { | |
703 | netdev_register_provider(&vport_classes[i].netdev_class); | |
704 | } | |
2b9d6589 | 705 | } |
c060c4cf EJ |
706 | |
707 | void | |
708 | netdev_vport_patch_register(void) | |
709 | { | |
710 | static const struct vport_class patch_class = | |
711 | { NULL, | |
712 | { "patch", VPORT_FUNCTIONS(get_patch_config, | |
713 | set_patch_config, | |
714 | NULL, | |
715 | NULL) }}; | |
716 | netdev_register_provider(&patch_class.netdev_class); | |
717 | } |