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 struct netdev_class *, const char *name,
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 struct netdev_class *class, const char *name,
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, class->type, sizeof ova.port_type);
167 ovs_strlcpy(ova.devname, name, sizeof ova.devname);
168 ova.config = &port_config;
170 err = parse_config(name, class->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 netdev_dev_init(&netdev_dev->netdev_dev, name, class);
194 *netdev_devp = &netdev_dev->netdev_dev;
199 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
201 const char *name = netdev_dev_get_name(netdev_dev_);
202 struct odp_vport_mod ovm;
203 struct tnl_port_config port_config;
206 ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
207 ovm.config = &port_config;
209 err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
215 return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
219 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
221 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
223 netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
228 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
229 struct netdev **netdevp)
231 struct netdev_tunnel *netdev;
233 netdev = xmalloc(sizeof *netdev);
234 netdev_init(&netdev->netdev, netdev_dev_);
236 *netdevp = &netdev->netdev;
241 netdev_tunnel_close(struct netdev *netdev_)
243 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
247 const struct netdev_class netdev_gre_class = {
254 netdev_tunnel_create,
255 netdev_tunnel_destroy,
256 netdev_tunnel_reconfigure,
261 NULL, /* enumerate */
264 NULL, /* recv_wait */
268 NULL, /* send_wait */
270 netdev_vport_set_etheraddr,
271 netdev_vport_get_etheraddr,
272 netdev_vport_get_mtu,
273 NULL, /* get_ifindex */
274 netdev_vport_get_carrier,
275 netdev_vport_get_stats,
276 netdev_vport_set_stats,
278 NULL, /* get_features */
279 NULL, /* set_advertisements */
280 NULL, /* get_vlan_vid */
282 NULL, /* set_policing */
283 NULL, /* get_qos_types */
284 NULL, /* get_qos_capabilities */
287 NULL, /* get_queue */
288 NULL, /* set_queue */
289 NULL, /* delete_queue */
290 NULL, /* get_queue_stats */
291 NULL, /* dump_queues */
292 NULL, /* dump_queue_stats */
297 NULL, /* add_router */
298 NULL, /* get_next_hop */
299 NULL, /* arp_lookup */
301 netdev_vport_update_flags,
303 netdev_vport_poll_add,
304 netdev_vport_poll_remove,
307 const struct netdev_class netdev_capwap_class = {
314 netdev_tunnel_create,
315 netdev_tunnel_destroy,
316 netdev_tunnel_reconfigure,
321 NULL, /* enumerate */
324 NULL, /* recv_wait */
328 NULL, /* send_wait */
330 netdev_vport_set_etheraddr,
331 netdev_vport_get_etheraddr,
332 netdev_vport_get_mtu,
333 NULL, /* get_ifindex */
334 netdev_vport_get_carrier,
335 netdev_vport_get_stats,
336 netdev_vport_set_stats,
338 NULL, /* get_features */
339 NULL, /* set_advertisements */
340 NULL, /* get_vlan_vid */
342 NULL, /* set_policing */
343 NULL, /* get_qos_types */
344 NULL, /* get_qos_capabilities */
347 NULL, /* get_queue */
348 NULL, /* set_queue */
349 NULL, /* delete_queue */
350 NULL, /* get_queue_stats */
351 NULL, /* dump_queues */
352 NULL, /* dump_queue_stats */
357 NULL, /* add_router */
358 NULL, /* get_next_hop */
359 NULL, /* arp_lookup */
361 netdev_vport_update_flags,
363 netdev_vport_poll_add,
364 netdev_vport_poll_remove,