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")) {
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")) {
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")) {
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")) {
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);
172 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
174 *netdev_devp = &netdev_dev->netdev_dev;
179 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
181 const char *name = netdev_dev_get_name(netdev_dev_);
182 struct odp_vport_mod ovm;
183 struct tnl_port_config port_config;
186 ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
187 ovm.config = &port_config;
189 err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
195 return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
199 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
201 struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
203 netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
208 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
209 struct netdev **netdevp)
211 struct netdev_tunnel *netdev;
213 netdev = xmalloc(sizeof *netdev);
214 netdev_init(&netdev->netdev, netdev_dev_);
216 *netdevp = &netdev->netdev;
221 netdev_tunnel_close(struct netdev *netdev_)
223 struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
227 const struct netdev_class netdev_gre_class = {
234 netdev_tunnel_create,
235 netdev_tunnel_destroy,
236 netdev_tunnel_reconfigure,
241 NULL, /* enumerate */
244 NULL, /* recv_wait */
248 NULL, /* send_wait */
250 netdev_vport_set_etheraddr,
251 netdev_vport_get_etheraddr,
252 netdev_vport_get_mtu,
253 NULL, /* get_ifindex */
254 netdev_vport_get_carrier,
255 netdev_vport_get_stats,
256 netdev_vport_set_stats,
258 NULL, /* get_features */
259 NULL, /* set_advertisements */
260 NULL, /* get_vlan_vid */
262 NULL, /* set_policing */
263 NULL, /* get_qos_types */
264 NULL, /* get_qos_capabilities */
267 NULL, /* get_queue */
268 NULL, /* set_queue */
269 NULL, /* delete_queue */
270 NULL, /* get_queue_stats */
271 NULL, /* dump_queues */
272 NULL, /* dump_queue_stats */
277 NULL, /* add_router */
278 NULL, /* get_next_hop */
279 NULL, /* arp_lookup */
281 netdev_vport_update_flags,
283 netdev_vport_poll_add,
284 netdev_vport_poll_remove,