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;
66 memset(config, 0, sizeof *config);
68 config->flags |= TNL_F_PMTUD;
69 config->flags |= TNL_F_HDR_CACHE;
71 SHASH_FOR_EACH (node, args) {
72 if (!strcmp(node->name, "remote_ip")) {
73 struct in_addr in_addr;
74 if (lookup_ip(node->data, &in_addr)) {
75 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
77 config->daddr = in_addr.s_addr;
79 } else if (!strcmp(node->name, "local_ip")) {
80 struct in_addr in_addr;
81 if (lookup_ip(node->data, &in_addr)) {
82 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
84 config->saddr = in_addr.s_addr;
86 } else if (!strcmp(node->name, "key") && !strcmp(type, "gre")) {
87 if (!strcmp(node->data, "flow")) {
88 config->flags |= TNL_F_IN_KEY_MATCH;
89 config->flags |= TNL_F_OUT_KEY_ACTION;
91 config->out_key = config->in_key = htonl(atoi(node->data));
93 } else if (!strcmp(node->name, "in_key") && !strcmp(type, "gre")) {
94 if (!strcmp(node->data, "flow")) {
95 config->flags |= TNL_F_IN_KEY_MATCH;
97 config->in_key = htonl(atoi(node->data));
99 } else if (!strcmp(node->name, "out_key") && !strcmp(type, "gre")) {
100 if (!strcmp(node->data, "flow")) {
101 config->flags |= TNL_F_OUT_KEY_ACTION;
103 config->out_key = htonl(atoi(node->data));
105 } else if (!strcmp(node->name, "tos")) {
106 if (!strcmp(node->data, "inherit")) {
107 config->flags |= TNL_F_TOS_INHERIT;
109 config->tos = atoi(node->data);
111 } else if (!strcmp(node->name, "ttl")) {
112 if (!strcmp(node->data, "inherit")) {
113 config->flags |= TNL_F_TTL_INHERIT;
115 config->ttl = atoi(node->data);
117 } else if (!strcmp(node->name, "csum") && !strcmp(type, "gre")) {
118 if (!strcmp(node->data, "true")) {
119 config->flags |= TNL_F_CSUM;
121 } else if (!strcmp(node->name, "pmtud")) {
122 if (!strcmp(node->data, "false")) {
123 config->flags &= ~TNL_F_PMTUD;
125 } else if (!strcmp(node->name, "header_cache")) {
126 if (!strcmp(node->data, "false")) {
127 config->flags &= ~TNL_F_HDR_CACHE;
130 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
134 if (!config->daddr) {
135 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument", name, type);
143 netdev_tunnel_create(const char *name, const char *type,
144 const struct shash *args, struct netdev_dev **netdev_devp)
147 struct odp_vport_add ova;
148 struct tnl_port_config port_config;
149 struct netdev_dev_tunnel *netdev_dev;
151 ovs_strlcpy(ova.port_type, type, sizeof ova.port_type);
152 ovs_strlcpy(ova.devname, name, sizeof ova.devname);
153 ova.config = &port_config;
155 err = parse_config(name, type, args, &port_config);
160 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
162 VLOG_WARN("%s: destroying existing device", name);
164 err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
169 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
176 netdev_dev = xmalloc(sizeof *netdev_dev);
178 if (!strcmp(type, "gre")) {
179 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
181 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_capwap_class);
184 *netdev_devp = &netdev_dev->netdev_dev;
189 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
191 const char *name = netdev_dev_get_name(netdev_dev_);
192 struct odp_vport_mod ovm;
193 struct tnl_port_config port_config;
196 ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
197 ovm.config = &port_config;
199 err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
205 return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
209 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
211 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
213 netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
218 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
219 struct netdev **netdevp)
221 struct netdev_tunnel *netdev;
223 netdev = xmalloc(sizeof *netdev);
224 netdev_init(&netdev->netdev, netdev_dev_);
226 *netdevp = &netdev->netdev;
231 netdev_tunnel_close(struct netdev *netdev_)
233 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
237 const struct netdev_class netdev_gre_class = {
244 netdev_tunnel_create,
245 netdev_tunnel_destroy,
246 netdev_tunnel_reconfigure,
251 NULL, /* enumerate */
254 NULL, /* recv_wait */
258 NULL, /* send_wait */
260 netdev_vport_set_etheraddr,
261 netdev_vport_get_etheraddr,
262 netdev_vport_get_mtu,
263 NULL, /* get_ifindex */
264 netdev_vport_get_carrier,
265 netdev_vport_get_stats,
266 netdev_vport_set_stats,
268 NULL, /* get_features */
269 NULL, /* set_advertisements */
270 NULL, /* get_vlan_vid */
272 NULL, /* set_policing */
273 NULL, /* get_qos_types */
274 NULL, /* get_qos_capabilities */
277 NULL, /* get_queue */
278 NULL, /* set_queue */
279 NULL, /* delete_queue */
280 NULL, /* get_queue_stats */
281 NULL, /* dump_queues */
282 NULL, /* dump_queue_stats */
287 NULL, /* add_router */
288 NULL, /* get_next_hop */
289 NULL, /* arp_lookup */
291 netdev_vport_update_flags,
293 netdev_vport_poll_add,
294 netdev_vport_poll_remove,
297 const struct netdev_class netdev_capwap_class = {
304 netdev_tunnel_create,
305 netdev_tunnel_destroy,
306 netdev_tunnel_reconfigure,
311 NULL, /* enumerate */
314 NULL, /* recv_wait */
318 NULL, /* send_wait */
320 netdev_vport_set_etheraddr,
321 netdev_vport_get_etheraddr,
322 netdev_vport_get_mtu,
323 NULL, /* get_ifindex */
324 netdev_vport_get_carrier,
325 netdev_vport_get_stats,
326 netdev_vport_set_stats,
328 NULL, /* get_features */
329 NULL, /* set_advertisements */
330 NULL, /* get_vlan_vid */
332 NULL, /* set_policing */
333 NULL, /* get_qos_types */
334 NULL, /* get_qos_capabilities */
337 NULL, /* get_queue */
338 NULL, /* set_queue */
339 NULL, /* delete_queue */
340 NULL, /* get_queue_stats */
341 NULL, /* dump_queues */
342 NULL, /* dump_queue_stats */
347 NULL, /* add_router */
348 NULL, /* get_next_hop */
349 NULL, /* arp_lookup */
351 netdev_vport_update_flags,
353 netdev_vport_poll_add,
354 netdev_vport_poll_remove,