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;
70 SHASH_FOR_EACH (node, args) {
71 if (!strcmp(node->name, "remote_ip")) {
72 struct in_addr in_addr;
73 if (lookup_ip(node->data, &in_addr)) {
74 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
76 config->daddr = in_addr.s_addr;
78 } else if (!strcmp(node->name, "local_ip")) {
79 struct in_addr in_addr;
80 if (lookup_ip(node->data, &in_addr)) {
81 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
83 config->saddr = in_addr.s_addr;
85 } else if (!strcmp(node->name, "key") && !strcmp(type, "gre")) {
86 if (!strcmp(node->data, "flow")) {
87 config->flags |= TNL_F_IN_KEY_MATCH;
88 config->flags |= TNL_F_OUT_KEY_ACTION;
90 config->out_key = config->in_key = htonl(atoi(node->data));
92 } else if (!strcmp(node->name, "in_key") && !strcmp(type, "gre")) {
93 if (!strcmp(node->data, "flow")) {
94 config->flags |= TNL_F_IN_KEY_MATCH;
96 config->in_key = htonl(atoi(node->data));
98 } else if (!strcmp(node->name, "out_key") && !strcmp(type, "gre")) {
99 if (!strcmp(node->data, "flow")) {
100 config->flags |= TNL_F_OUT_KEY_ACTION;
102 config->out_key = htonl(atoi(node->data));
104 } else if (!strcmp(node->name, "tos")) {
105 if (!strcmp(node->data, "inherit")) {
106 config->flags |= TNL_F_TOS_INHERIT;
108 config->tos = atoi(node->data);
110 } else if (!strcmp(node->name, "ttl")) {
111 if (!strcmp(node->data, "inherit")) {
112 config->flags |= TNL_F_TTL_INHERIT;
114 config->ttl = atoi(node->data);
116 } else if (!strcmp(node->name, "csum") && !strcmp(type, "gre")) {
117 if (!strcmp(node->data, "true")) {
118 config->flags |= TNL_F_CSUM;
120 } else if (!strcmp(node->name, "pmtud")) {
121 if (!strcmp(node->data, "false")) {
122 config->flags &= ~TNL_F_PMTUD;
125 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
129 if (!config->daddr) {
130 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument", name, type);
138 netdev_tunnel_create(const char *name, const char *type,
139 const struct shash *args, struct netdev_dev **netdev_devp)
142 struct odp_vport_add ova;
143 struct tnl_port_config port_config;
144 struct netdev_dev_tunnel *netdev_dev;
146 ovs_strlcpy(ova.port_type, type, sizeof ova.port_type);
147 ovs_strlcpy(ova.devname, name, sizeof ova.devname);
148 ova.config = &port_config;
150 err = parse_config(name, type, args, &port_config);
155 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
157 VLOG_WARN("%s: destroying existing device", name);
159 err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
164 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
171 netdev_dev = xmalloc(sizeof *netdev_dev);
173 if (!strcmp(type, "gre")) {
174 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
176 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_capwap_class);
179 *netdev_devp = &netdev_dev->netdev_dev;
184 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
186 const char *name = netdev_dev_get_name(netdev_dev_);
187 struct odp_vport_mod ovm;
188 struct tnl_port_config port_config;
191 ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
192 ovm.config = &port_config;
194 err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
200 return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
204 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
206 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
208 netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
213 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
214 struct netdev **netdevp)
216 struct netdev_tunnel *netdev;
218 netdev = xmalloc(sizeof *netdev);
219 netdev_init(&netdev->netdev, netdev_dev_);
221 *netdevp = &netdev->netdev;
226 netdev_tunnel_close(struct netdev *netdev_)
228 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
232 const struct netdev_class netdev_gre_class = {
239 netdev_tunnel_create,
240 netdev_tunnel_destroy,
241 netdev_tunnel_reconfigure,
246 NULL, /* enumerate */
249 NULL, /* recv_wait */
253 NULL, /* send_wait */
255 netdev_vport_set_etheraddr,
256 netdev_vport_get_etheraddr,
257 netdev_vport_get_mtu,
258 NULL, /* get_ifindex */
259 netdev_vport_get_carrier,
260 netdev_vport_get_stats,
261 netdev_vport_set_stats,
263 NULL, /* get_features */
264 NULL, /* set_advertisements */
265 NULL, /* get_vlan_vid */
267 NULL, /* set_policing */
268 NULL, /* get_qos_types */
269 NULL, /* get_qos_capabilities */
272 NULL, /* get_queue */
273 NULL, /* set_queue */
274 NULL, /* delete_queue */
275 NULL, /* get_queue_stats */
276 NULL, /* dump_queues */
277 NULL, /* dump_queue_stats */
282 NULL, /* add_router */
283 NULL, /* get_next_hop */
284 NULL, /* arp_lookup */
286 netdev_vport_update_flags,
288 netdev_vport_poll_add,
289 netdev_vport_poll_remove,
292 const struct netdev_class netdev_capwap_class = {
299 netdev_tunnel_create,
300 netdev_tunnel_destroy,
301 netdev_tunnel_reconfigure,
306 NULL, /* enumerate */
309 NULL, /* recv_wait */
313 NULL, /* send_wait */
315 netdev_vport_set_etheraddr,
316 netdev_vport_get_etheraddr,
317 netdev_vport_get_mtu,
318 NULL, /* get_ifindex */
319 netdev_vport_get_carrier,
320 netdev_vport_get_stats,
321 netdev_vport_set_stats,
323 NULL, /* get_features */
324 NULL, /* set_advertisements */
325 NULL, /* get_vlan_vid */
327 NULL, /* set_policing */
328 NULL, /* get_qos_types */
329 NULL, /* get_qos_capabilities */
332 NULL, /* get_queue */
333 NULL, /* set_queue */
334 NULL, /* delete_queue */
335 NULL, /* get_queue_stats */
336 NULL, /* dump_queues */
337 NULL, /* dump_queue_stats */
342 NULL, /* add_router */
343 NULL, /* get_next_hop */
344 NULL, /* arp_lookup */
346 netdev_vport_update_flags,
348 netdev_vport_poll_add,
349 netdev_vport_poll_remove,