netdev: Pass class structure, instead of type, to "create" function.
[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 struct netdev_class *, const char *name,
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 struct netdev_class *class, const char *name,
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, class->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, class->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     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
194     *netdev_devp = &netdev_dev->netdev_dev;
195     return 0;
196 }
197
198 static int
199 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
200 {
201     const char *name = netdev_dev_get_name(netdev_dev_);
202     struct odp_vport_mod ovm;
203     struct tnl_port_config port_config;
204     int err;
205
206     ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
207     ovm.config = &port_config;
208
209     err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
210                        &port_config);
211     if (err) {
212         return err;
213     }
214
215     return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
216 }
217
218 static void
219 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
220 {
221     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
222
223     netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
224     free(netdev_dev);
225 }
226
227 static int
228 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
229                 struct netdev **netdevp)
230 {
231     struct netdev_tunnel *netdev;
232
233     netdev = xmalloc(sizeof *netdev);
234     netdev_init(&netdev->netdev, netdev_dev_);
235
236     *netdevp = &netdev->netdev;
237     return 0;
238 }
239
240 static void
241 netdev_tunnel_close(struct netdev *netdev_)
242 {
243     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
244     free(netdev);
245 }
246
247 const struct netdev_class netdev_gre_class = {
248     "gre",
249
250     NULL,                       /* init */
251     NULL,                       /* run */
252     NULL,                       /* wait */
253
254     netdev_tunnel_create,
255     netdev_tunnel_destroy,
256     netdev_tunnel_reconfigure,
257
258     netdev_tunnel_open,
259     netdev_tunnel_close,
260
261     NULL,                       /* enumerate */
262
263     NULL,                       /* recv */
264     NULL,                       /* recv_wait */
265     NULL,                       /* drain */
266
267     NULL,                       /* send */
268     NULL,                       /* send_wait */
269
270     netdev_vport_set_etheraddr,
271     netdev_vport_get_etheraddr,
272     netdev_vport_get_mtu,
273     NULL,                       /* get_ifindex */
274     netdev_vport_get_carrier,
275     netdev_vport_get_stats,
276     netdev_vport_set_stats,
277
278     NULL,                       /* get_features */
279     NULL,                       /* set_advertisements */
280     NULL,                       /* get_vlan_vid */
281
282     NULL,                       /* set_policing */
283     NULL,                       /* get_qos_types */
284     NULL,                       /* get_qos_capabilities */
285     NULL,                       /* get_qos */
286     NULL,                       /* set_qos */
287     NULL,                       /* get_queue */
288     NULL,                       /* set_queue */
289     NULL,                       /* delete_queue */
290     NULL,                       /* get_queue_stats */
291     NULL,                       /* dump_queues */
292     NULL,                       /* dump_queue_stats */
293
294     NULL,                       /* get_in4 */
295     NULL,                       /* set_in4 */
296     NULL,                       /* get_in6 */
297     NULL,                       /* add_router */
298     NULL,                       /* get_next_hop */
299     NULL,                       /* arp_lookup */
300
301     netdev_vport_update_flags,
302
303     netdev_vport_poll_add,
304     netdev_vport_poll_remove,
305 };
306
307 const struct netdev_class netdev_capwap_class = {
308     "capwap",
309
310     NULL,                       /* init */
311     NULL,                       /* run */
312     NULL,                       /* wait */
313
314     netdev_tunnel_create,
315     netdev_tunnel_destroy,
316     netdev_tunnel_reconfigure,
317
318     netdev_tunnel_open,
319     netdev_tunnel_close,
320
321     NULL,                       /* enumerate */
322
323     NULL,                       /* recv */
324     NULL,                       /* recv_wait */
325     NULL,                       /* drain */
326
327     NULL,                       /* send */
328     NULL,                       /* send_wait */
329
330     netdev_vport_set_etheraddr,
331     netdev_vport_get_etheraddr,
332     netdev_vport_get_mtu,
333     NULL,                       /* get_ifindex */
334     netdev_vport_get_carrier,
335     netdev_vport_get_stats,
336     netdev_vport_set_stats,
337
338     NULL,                       /* get_features */
339     NULL,                       /* set_advertisements */
340     NULL,                       /* get_vlan_vid */
341
342     NULL,                       /* set_policing */
343     NULL,                       /* get_qos_types */
344     NULL,                       /* get_qos_capabilities */
345     NULL,                       /* get_qos */
346     NULL,                       /* set_qos */
347     NULL,                       /* get_queue */
348     NULL,                       /* set_queue */
349     NULL,                       /* delete_queue */
350     NULL,                       /* get_queue_stats */
351     NULL,                       /* dump_queues */
352     NULL,                       /* dump_queue_stats */
353
354     NULL,                       /* get_in4 */
355     NULL,                       /* set_in4 */
356     NULL,                       /* get_in6 */
357     NULL,                       /* add_router */
358     NULL,                       /* get_next_hop */
359     NULL,                       /* arp_lookup */
360
361     netdev_vport_update_flags,
362
363     netdev_vport_poll_add,
364     netdev_vport_poll_remove,
365 };