2 * Copyright (c) 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
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.
21 #include <sys/ioctl.h>
23 #include "netdev-provider.h"
24 #include "netdev-vport.h"
25 #include "openflow/openflow.h"
26 #include "openvswitch/datapath-protocol.h"
27 #include "openvswitch/tunnel.h"
29 #include "socket-util.h"
32 VLOG_DEFINE_THIS_MODULE(netdev_tunnel)
34 struct netdev_dev_tunnel {
35 struct netdev_dev netdev_dev;
38 struct netdev_tunnel {
42 static int netdev_tunnel_create(const char *name, const char *type,
43 const struct shash *args, struct netdev_dev **);
45 static struct netdev_dev_tunnel *
46 netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
48 assert(netdev_dev_get_class(netdev_dev)->create == netdev_tunnel_create);
49 return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
52 static struct netdev_tunnel *
53 netdev_tunnel_cast(const struct netdev *netdev)
55 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
56 assert(netdev_dev_get_class(netdev_dev)->create == netdev_tunnel_create);
57 return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
61 parse_config(const char *name, const char *type, const struct shash *args,
62 struct tnl_port_config *config)
64 struct shash_node *node;
65 bool ipsec_ip_set = false;
66 bool ipsec_mech_set = false;
68 memset(config, 0, sizeof *config);
70 config->flags |= TNL_F_PMTUD;
71 config->flags |= TNL_F_HDR_CACHE;
73 SHASH_FOR_EACH (node, args) {
74 if (!strcmp(node->name, "remote_ip")) {
75 struct in_addr in_addr;
76 if (lookup_ip(node->data, &in_addr)) {
77 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
79 config->daddr = in_addr.s_addr;
81 } else if (!strcmp(node->name, "local_ip")) {
82 struct in_addr in_addr;
83 if (lookup_ip(node->data, &in_addr)) {
84 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
86 config->saddr = in_addr.s_addr;
88 } else if (!strcmp(node->name, "key") && !strcmp(type, "gre")) {
89 if (!strcmp(node->data, "flow")) {
90 config->flags |= TNL_F_IN_KEY_MATCH;
91 config->flags |= TNL_F_OUT_KEY_ACTION;
93 config->out_key = config->in_key = htonl(atoi(node->data));
95 } else if (!strcmp(node->name, "in_key") && !strcmp(type, "gre")) {
96 if (!strcmp(node->data, "flow")) {
97 config->flags |= TNL_F_IN_KEY_MATCH;
99 config->in_key = htonl(atoi(node->data));
101 } else if (!strcmp(node->name, "out_key") && !strcmp(type, "gre")) {
102 if (!strcmp(node->data, "flow")) {
103 config->flags |= TNL_F_OUT_KEY_ACTION;
105 config->out_key = htonl(atoi(node->data));
107 } else if (!strcmp(node->name, "tos")) {
108 if (!strcmp(node->data, "inherit")) {
109 config->flags |= TNL_F_TOS_INHERIT;
111 config->tos = atoi(node->data);
113 } else if (!strcmp(node->name, "ttl")) {
114 if (!strcmp(node->data, "inherit")) {
115 config->flags |= TNL_F_TTL_INHERIT;
117 config->ttl = atoi(node->data);
119 } else if (!strcmp(node->name, "csum") && !strcmp(type, "gre")) {
120 if (!strcmp(node->data, "true")) {
121 config->flags |= TNL_F_CSUM;
123 } else if (!strcmp(node->name, "pmtud")) {
124 if (!strcmp(node->data, "false")) {
125 config->flags &= ~TNL_F_PMTUD;
127 } else if (!strcmp(node->name, "header_cache")) {
128 if (!strcmp(node->data, "false")) {
129 config->flags &= ~TNL_F_HDR_CACHE;
131 } else if (!strcmp(node->name, "ipsec_local_ip")) {
133 } else if (!strcmp(node->name, "ipsec_cert")
134 || !strcmp(node->name, "ipsec_psk")) {
135 ipsec_mech_set = true;
137 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
141 /* IPsec doesn't work when header caching is enabled. Disable it if
142 * the IPsec local IP address and authentication mechanism have been
144 if (ipsec_ip_set && ipsec_mech_set) {
145 VLOG_INFO("%s: header caching disabled due to use of IPsec", name);
146 config->flags &= ~TNL_F_HDR_CACHE;
149 if (!config->daddr) {
150 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument", name, type);
158 netdev_tunnel_create(const char *name, const char *type,
159 const struct shash *args, struct netdev_dev **netdev_devp)
162 struct odp_vport_add ova;
163 struct tnl_port_config port_config;
164 struct netdev_dev_tunnel *netdev_dev;
166 ovs_strlcpy(ova.port_type, type, sizeof ova.port_type);
167 ovs_strlcpy(ova.devname, name, sizeof ova.devname);
168 ova.config = &port_config;
170 err = parse_config(name, type, args, &port_config);
175 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
177 VLOG_WARN("%s: destroying existing device", name);
179 err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
184 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
191 netdev_dev = xmalloc(sizeof *netdev_dev);
193 if (!strcmp(type, "gre")) {
194 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
196 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_capwap_class);
199 *netdev_devp = &netdev_dev->netdev_dev;
204 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
206 const char *name = netdev_dev_get_name(netdev_dev_);
207 struct odp_vport_mod ovm;
208 struct tnl_port_config port_config;
211 ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
212 ovm.config = &port_config;
214 err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
220 return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
224 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
226 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
228 netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
233 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
234 struct netdev **netdevp)
236 struct netdev_tunnel *netdev;
238 netdev = xmalloc(sizeof *netdev);
239 netdev_init(&netdev->netdev, netdev_dev_);
241 *netdevp = &netdev->netdev;
246 netdev_tunnel_close(struct netdev *netdev_)
248 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
252 const struct netdev_class netdev_gre_class = {
259 netdev_tunnel_create,
260 netdev_tunnel_destroy,
261 netdev_tunnel_reconfigure,
266 NULL, /* enumerate */
269 NULL, /* recv_wait */
273 NULL, /* send_wait */
275 netdev_vport_set_etheraddr,
276 netdev_vport_get_etheraddr,
277 netdev_vport_get_mtu,
278 NULL, /* get_ifindex */
279 netdev_vport_get_carrier,
280 netdev_vport_get_stats,
281 netdev_vport_set_stats,
283 NULL, /* get_features */
284 NULL, /* set_advertisements */
285 NULL, /* get_vlan_vid */
287 NULL, /* set_policing */
288 NULL, /* get_qos_types */
289 NULL, /* get_qos_capabilities */
292 NULL, /* get_queue */
293 NULL, /* set_queue */
294 NULL, /* delete_queue */
295 NULL, /* get_queue_stats */
296 NULL, /* dump_queues */
297 NULL, /* dump_queue_stats */
302 NULL, /* add_router */
303 NULL, /* get_next_hop */
304 NULL, /* arp_lookup */
306 netdev_vport_update_flags,
308 netdev_vport_poll_add,
309 netdev_vport_poll_remove,
312 const struct netdev_class netdev_capwap_class = {
319 netdev_tunnel_create,
320 netdev_tunnel_destroy,
321 netdev_tunnel_reconfigure,
326 NULL, /* enumerate */
329 NULL, /* recv_wait */
333 NULL, /* send_wait */
335 netdev_vport_set_etheraddr,
336 netdev_vport_get_etheraddr,
337 netdev_vport_get_mtu,
338 NULL, /* get_ifindex */
339 netdev_vport_get_carrier,
340 netdev_vport_get_stats,
341 netdev_vport_set_stats,
343 NULL, /* get_features */
344 NULL, /* set_advertisements */
345 NULL, /* get_vlan_vid */
347 NULL, /* set_policing */
348 NULL, /* get_qos_types */
349 NULL, /* get_qos_capabilities */
352 NULL, /* get_queue */
353 NULL, /* set_queue */
354 NULL, /* delete_queue */
355 NULL, /* get_queue_stats */
356 NULL, /* dump_queues */
357 NULL, /* dump_queue_stats */
362 NULL, /* add_router */
363 NULL, /* get_next_hop */
364 NULL, /* arp_lookup */
366 netdev_vport_update_flags,
368 netdev_vport_poll_add,
369 netdev_vport_poll_remove,