079830e63372ee45f47bdbf13da4c603f08b252d
[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     bool ipsec_ip_set = false;
66     bool ipsec_mech_set = false;
67
68     memset(config, 0, sizeof *config);
69
70     config->flags |= TNL_F_PMTUD;
71     config->flags |= TNL_F_HDR_CACHE;
72
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);
78             } else {
79                 config->daddr = in_addr.s_addr;
80             }
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);
85             } else {
86                 config->saddr = in_addr.s_addr;
87             }
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;
92             } else {
93                 config->out_key = config->in_key = htonl(atoi(node->data));
94             }
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;
98             } else {
99                 config->in_key = htonl(atoi(node->data));
100             }
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;
104             } else {
105                 config->out_key = htonl(atoi(node->data));
106             }
107         } else if (!strcmp(node->name, "tos")) {
108             if (!strcmp(node->data, "inherit")) {
109                 config->flags |= TNL_F_TOS_INHERIT;
110             } else {
111                 config->tos = atoi(node->data);
112             }
113         } else if (!strcmp(node->name, "ttl")) {
114             if (!strcmp(node->data, "inherit")) {
115                 config->flags |= TNL_F_TTL_INHERIT;
116             } else {
117                 config->ttl = atoi(node->data);
118             }
119         } else if (!strcmp(node->name, "csum") && !strcmp(type, "gre")) {
120             if (!strcmp(node->data, "true")) {
121                 config->flags |= TNL_F_CSUM;
122             }
123         } else if (!strcmp(node->name, "pmtud")) {
124             if (!strcmp(node->data, "false")) {
125                 config->flags &= ~TNL_F_PMTUD;
126             }
127         } else if (!strcmp(node->name, "header_cache")) {
128             if (!strcmp(node->data, "false")) {
129                 config->flags &= ~TNL_F_HDR_CACHE;
130             }
131         } else if (!strcmp(node->name, "ipsec_local_ip")) {
132             ipsec_ip_set = true;
133         } else if (!strcmp(node->name, "ipsec_cert")
134                    || !strcmp(node->name, "ipsec_psk")) {
135             ipsec_mech_set = true;
136         } else {
137             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
138         }
139     }
140
141     /* IPsec doesn't work when header caching is enabled.  Disable it if
142      * the IPsec local IP address and authentication mechanism have been
143      * defined. */
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;
147     }
148
149     if (!config->daddr) {
150         VLOG_WARN("%s: %s type requires valid 'remote_ip' argument", name, type);
151         return EINVAL;
152     }
153
154     return 0;
155 }
156
157 static int
158 netdev_tunnel_create(const char *name, const char *type,
159                      const struct shash *args, struct netdev_dev **netdev_devp)
160 {
161     int err;
162     struct odp_vport_add ova;
163     struct tnl_port_config port_config;
164     struct netdev_dev_tunnel *netdev_dev;
165
166     ovs_strlcpy(ova.port_type, type, sizeof ova.port_type);
167     ovs_strlcpy(ova.devname, name, sizeof ova.devname);
168     ova.config = &port_config;
169
170     err = parse_config(name, type, args, &port_config);
171     if (err) {
172         return err;
173     }
174
175     err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
176     if (err == EBUSY) {
177         VLOG_WARN("%s: destroying existing device", name);
178
179         err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
180         if (err) {
181             return err;
182         }
183
184         err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
185     }
186
187     if (err) {
188         return err;
189     }
190
191     netdev_dev = xmalloc(sizeof *netdev_dev);
192
193     if (!strcmp(type, "gre")) {
194         netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
195     } else {
196         netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_capwap_class);
197     }
198
199     *netdev_devp = &netdev_dev->netdev_dev;
200     return 0;
201 }
202
203 static int
204 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
205 {
206     const char *name = netdev_dev_get_name(netdev_dev_);
207     struct odp_vport_mod ovm;
208     struct tnl_port_config port_config;
209     int err;
210
211     ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
212     ovm.config = &port_config;
213
214     err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
215                        &port_config);
216     if (err) {
217         return err;
218     }
219
220     return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
221 }
222
223 static void
224 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
225 {
226     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
227
228     netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
229     free(netdev_dev);
230 }
231
232 static int
233 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
234                 struct netdev **netdevp)
235 {
236     struct netdev_tunnel *netdev;
237
238     netdev = xmalloc(sizeof *netdev);
239     netdev_init(&netdev->netdev, netdev_dev_);
240
241     *netdevp = &netdev->netdev;
242     return 0;
243 }
244
245 static void
246 netdev_tunnel_close(struct netdev *netdev_)
247 {
248     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
249     free(netdev);
250 }
251
252 const struct netdev_class netdev_gre_class = {
253     "gre",
254
255     NULL,                       /* init */
256     NULL,                       /* run */
257     NULL,                       /* wait */
258
259     netdev_tunnel_create,
260     netdev_tunnel_destroy,
261     netdev_tunnel_reconfigure,
262
263     netdev_tunnel_open,
264     netdev_tunnel_close,
265
266     NULL,                       /* enumerate */
267
268     NULL,                       /* recv */
269     NULL,                       /* recv_wait */
270     NULL,                       /* drain */
271
272     NULL,                       /* send */
273     NULL,                       /* send_wait */
274
275     netdev_vport_set_etheraddr,
276     netdev_vport_get_etheraddr,
277     netdev_vport_get_mtu,
278     NULL,                       /* get_ifindex */
279     netdev_vport_get_carrier,
280     netdev_vport_get_stats,
281     netdev_vport_set_stats,
282
283     NULL,                       /* get_features */
284     NULL,                       /* set_advertisements */
285     NULL,                       /* get_vlan_vid */
286
287     NULL,                       /* set_policing */
288     NULL,                       /* get_qos_types */
289     NULL,                       /* get_qos_capabilities */
290     NULL,                       /* get_qos */
291     NULL,                       /* set_qos */
292     NULL,                       /* get_queue */
293     NULL,                       /* set_queue */
294     NULL,                       /* delete_queue */
295     NULL,                       /* get_queue_stats */
296     NULL,                       /* dump_queues */
297     NULL,                       /* dump_queue_stats */
298
299     NULL,                       /* get_in4 */
300     NULL,                       /* set_in4 */
301     NULL,                       /* get_in6 */
302     NULL,                       /* add_router */
303     NULL,                       /* get_next_hop */
304     NULL,                       /* arp_lookup */
305
306     netdev_vport_update_flags,
307
308     netdev_vport_poll_add,
309     netdev_vport_poll_remove,
310 };
311
312 const struct netdev_class netdev_capwap_class = {
313     "capwap",
314
315     NULL,                       /* init */
316     NULL,                       /* run */
317     NULL,                       /* wait */
318
319     netdev_tunnel_create,
320     netdev_tunnel_destroy,
321     netdev_tunnel_reconfigure,
322
323     netdev_tunnel_open,
324     netdev_tunnel_close,
325
326     NULL,                       /* enumerate */
327
328     NULL,                       /* recv */
329     NULL,                       /* recv_wait */
330     NULL,                       /* drain */
331
332     NULL,                       /* send */
333     NULL,                       /* send_wait */
334
335     netdev_vport_set_etheraddr,
336     netdev_vport_get_etheraddr,
337     netdev_vport_get_mtu,
338     NULL,                       /* get_ifindex */
339     netdev_vport_get_carrier,
340     netdev_vport_get_stats,
341     netdev_vport_set_stats,
342
343     NULL,                       /* get_features */
344     NULL,                       /* set_advertisements */
345     NULL,                       /* get_vlan_vid */
346
347     NULL,                       /* set_policing */
348     NULL,                       /* get_qos_types */
349     NULL,                       /* get_qos_capabilities */
350     NULL,                       /* get_qos */
351     NULL,                       /* set_qos */
352     NULL,                       /* get_queue */
353     NULL,                       /* set_queue */
354     NULL,                       /* delete_queue */
355     NULL,                       /* get_queue_stats */
356     NULL,                       /* dump_queues */
357     NULL,                       /* dump_queue_stats */
358
359     NULL,                       /* get_in4 */
360     NULL,                       /* set_in4 */
361     NULL,                       /* get_in6 */
362     NULL,                       /* add_router */
363     NULL,                       /* get_next_hop */
364     NULL,                       /* arp_lookup */
365
366     netdev_vport_update_flags,
367
368     netdev_vport_poll_add,
369     netdev_vport_poll_remove,
370 };