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