0497cbbc266e3ad396f713f04bce897135d50033
[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")) {
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")) {
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")) {
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")) {
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     netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
173
174     *netdev_devp = &netdev_dev->netdev_dev;
175     return 0;
176 }
177
178 static int
179 netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
180 {
181     const char *name = netdev_dev_get_name(netdev_dev_);
182     struct odp_vport_mod ovm;
183     struct tnl_port_config port_config;
184     int err;
185
186     ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
187     ovm.config = &port_config;
188
189     err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
190                        &port_config);
191     if (err) {
192         return err;
193     }
194
195     return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
196 }
197
198 static void
199 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
200 {
201     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
202
203     netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
204     free(netdev_dev);
205 }
206
207 static int
208 netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
209                 struct netdev **netdevp)
210 {
211     struct netdev_tunnel *netdev;
212
213     netdev = xmalloc(sizeof *netdev);
214     netdev_init(&netdev->netdev, netdev_dev_);
215
216     *netdevp = &netdev->netdev;
217     return 0;
218 }
219
220 static void
221 netdev_tunnel_close(struct netdev *netdev_)
222 {
223     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
224     free(netdev);
225 }
226
227 const struct netdev_class netdev_gre_class = {
228     "gre",
229
230     NULL,                       /* init */
231     NULL,                       /* run */
232     NULL,                       /* wait */
233
234     netdev_tunnel_create,
235     netdev_tunnel_destroy,
236     netdev_tunnel_reconfigure,
237
238     netdev_tunnel_open,
239     netdev_tunnel_close,
240
241     NULL,                       /* enumerate */
242
243     NULL,                       /* recv */
244     NULL,                       /* recv_wait */
245     NULL,                       /* drain */
246
247     NULL,                       /* send */
248     NULL,                       /* send_wait */
249
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,
257
258     NULL,                       /* get_features */
259     NULL,                       /* set_advertisements */
260     NULL,                       /* get_vlan_vid */
261
262     NULL,                       /* set_policing */
263     NULL,                       /* get_qos_types */
264     NULL,                       /* get_qos_capabilities */
265     NULL,                       /* get_qos */
266     NULL,                       /* set_qos */
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 */
273
274     NULL,                       /* get_in4 */
275     NULL,                       /* set_in4 */
276     NULL,                       /* get_in6 */
277     NULL,                       /* add_router */
278     NULL,                       /* get_next_hop */
279     NULL,                       /* arp_lookup */
280
281     netdev_vport_update_flags,
282
283     netdev_vport_poll_add,
284     netdev_vport_poll_remove,
285 };