d0ecd98e204e7e603f6f392f570a282aebcf0292
[sliver-openvswitch.git] / lib / netdev-tunnel.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <net/if.h>
21 #include <sys/ioctl.h>
22
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"
28 #include "packets.h"
29 #include "socket-util.h"
30 #include "vlog.h"
31
32 VLOG_DEFINE_THIS_MODULE(netdev_tunnel)
33
34 struct netdev_dev_tunnel {
35     struct netdev_dev netdev_dev;
36 };
37
38 struct netdev_tunnel {
39     struct netdev netdev;
40 };
41
42 static int netdev_tunnel_create(const char *name, const char *type,
43                                 const struct shash *args, struct netdev_dev **);
44
45 static struct netdev_dev_tunnel *
46 netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
47 {
48     assert(netdev_dev_get_class(netdev_dev)->create == netdev_tunnel_create);
49     return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
50 }
51
52 static struct netdev_tunnel *
53 netdev_tunnel_cast(const struct netdev *netdev)
54 {
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);
58 }
59
60 static int
61 parse_config(const char *name, const char *type, const struct shash *args,
62              struct tnl_port_config *config)
63 {
64     struct shash_node *node;
65
66     memset(config, 0, sizeof *config);
67
68     config->flags |= TNL_F_PMTUD;
69
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);
75             } else {
76                 config->daddr = in_addr.s_addr;
77             }
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);
82             } else {
83                 config->saddr = in_addr.s_addr;
84             }
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;
89             } else {
90                 config->out_key = config->in_key = htonl(atoi(node->data));
91             }
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;
95             } else {
96                 config->in_key = htonl(atoi(node->data));
97             }
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;
101             } else {
102                 config->out_key = htonl(atoi(node->data));
103             }
104         } else if (!strcmp(node->name, "tos")) {
105             if (!strcmp(node->data, "inherit")) {
106                 config->flags |= TNL_F_TOS_INHERIT;
107             } else {
108                 config->tos = atoi(node->data);
109             }
110         } else if (!strcmp(node->name, "ttl")) {
111             if (!strcmp(node->data, "inherit")) {
112                 config->flags |= TNL_F_TTL_INHERIT;
113             } else {
114                 config->ttl = atoi(node->data);
115             }
116         } else if (!strcmp(node->name, "csum") && !strcmp(type, "gre")) {
117             if (!strcmp(node->data, "true")) {
118                 config->flags |= TNL_F_CSUM;
119             }
120         } else if (!strcmp(node->name, "pmtud")) {
121             if (!strcmp(node->data, "false")) {
122                 config->flags &= ~TNL_F_PMTUD;
123             }
124         } else {
125             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
126         }
127     }
128
129     if (!config->daddr) {
130         VLOG_WARN("%s: %s type requires valid 'remote_ip' argument", name, type);
131         return EINVAL;
132     }
133
134     return 0;
135 }
136
137 static int
138 netdev_tunnel_create(const char *name, const char *type,
139                      const struct shash *args, struct netdev_dev **netdev_devp)
140 {
141     int err;
142     struct odp_vport_add ova;
143     struct tnl_port_config port_config;
144     struct netdev_dev_tunnel *netdev_dev;
145
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;
149
150     err = parse_config(name, type, args, &port_config);
151     if (err) {
152         return err;
153     }
154
155     err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
156     if (err == EBUSY) {
157         VLOG_WARN("%s: destroying existing device", name);
158
159         err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
160         if (err) {
161             return err;
162         }
163
164         err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
165     }
166
167     if (err) {
168         return err;
169     }
170
171     netdev_dev = xmalloc(sizeof *netdev_dev);
172
173     if (!strcmp(type, "gre")) {
174         netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
175     } else {
176         netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_capwap_class);
177     }
178
179     *netdev_devp = &netdev_dev->netdev_dev;
180     return 0;
181 }
182
183 static int
184 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
185 {
186     const char *name = netdev_dev_get_name(netdev_dev_);
187     struct odp_vport_mod ovm;
188     struct tnl_port_config port_config;
189     int err;
190
191     ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
192     ovm.config = &port_config;
193
194     err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
195                        &port_config);
196     if (err) {
197         return err;
198     }
199
200     return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
201 }
202
203 static void
204 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
205 {
206     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
207
208     netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
209     free(netdev_dev);
210 }
211
212 static int
213 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
214                 struct netdev **netdevp)
215 {
216     struct netdev_tunnel *netdev;
217
218     netdev = xmalloc(sizeof *netdev);
219     netdev_init(&netdev->netdev, netdev_dev_);
220
221     *netdevp = &netdev->netdev;
222     return 0;
223 }
224
225 static void
226 netdev_tunnel_close(struct netdev *netdev_)
227 {
228     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
229     free(netdev);
230 }
231
232 const struct netdev_class netdev_gre_class = {
233     "gre",
234
235     NULL,                       /* init */
236     NULL,                       /* run */
237     NULL,                       /* wait */
238
239     netdev_tunnel_create,
240     netdev_tunnel_destroy,
241     netdev_tunnel_reconfigure,
242
243     netdev_tunnel_open,
244     netdev_tunnel_close,
245
246     NULL,                       /* enumerate */
247
248     NULL,                       /* recv */
249     NULL,                       /* recv_wait */
250     NULL,                       /* drain */
251
252     NULL,                       /* send */
253     NULL,                       /* send_wait */
254
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,
262
263     NULL,                       /* get_features */
264     NULL,                       /* set_advertisements */
265     NULL,                       /* get_vlan_vid */
266
267     NULL,                       /* set_policing */
268     NULL,                       /* get_qos_types */
269     NULL,                       /* get_qos_capabilities */
270     NULL,                       /* get_qos */
271     NULL,                       /* set_qos */
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 */
278
279     NULL,                       /* get_in4 */
280     NULL,                       /* set_in4 */
281     NULL,                       /* get_in6 */
282     NULL,                       /* add_router */
283     NULL,                       /* get_next_hop */
284     NULL,                       /* arp_lookup */
285
286     netdev_vport_update_flags,
287
288     netdev_vport_poll_add,
289     netdev_vport_poll_remove,
290 };
291
292 const struct netdev_class netdev_capwap_class = {
293     "capwap",
294
295     NULL,                       /* init */
296     NULL,                       /* run */
297     NULL,                       /* wait */
298
299     netdev_tunnel_create,
300     netdev_tunnel_destroy,
301     netdev_tunnel_reconfigure,
302
303     netdev_tunnel_open,
304     netdev_tunnel_close,
305
306     NULL,                       /* enumerate */
307
308     NULL,                       /* recv */
309     NULL,                       /* recv_wait */
310     NULL,                       /* drain */
311
312     NULL,                       /* send */
313     NULL,                       /* send_wait */
314
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,
322
323     NULL,                       /* get_features */
324     NULL,                       /* set_advertisements */
325     NULL,                       /* get_vlan_vid */
326
327     NULL,                       /* set_policing */
328     NULL,                       /* get_qos_types */
329     NULL,                       /* get_qos_capabilities */
330     NULL,                       /* get_qos */
331     NULL,                       /* set_qos */
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 */
338
339     NULL,                       /* get_in4 */
340     NULL,                       /* set_in4 */
341     NULL,                       /* get_in6 */
342     NULL,                       /* add_router */
343     NULL,                       /* get_next_hop */
344     NULL,                       /* arp_lookup */
345
346     netdev_vport_update_flags,
347
348     netdev_vport_poll_add,
349     netdev_vport_poll_remove,
350 };