gre: Disable checksums by default.
[sliver-openvswitch.git] / lib / netdev-gre.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/gre.h"
28 #include "packets.h"
29 #include "socket-util.h"
30 #include "vlog.h"
31
32 VLOG_DEFINE_THIS_MODULE(netdev_gre)
33
34 struct netdev_dev_gre {
35     struct netdev_dev netdev_dev;
36 };
37
38 struct netdev_gre {
39     struct netdev netdev;
40 };
41
42 static struct netdev_dev_gre *
43 netdev_dev_gre_cast(const struct netdev_dev *netdev_dev)
44 {
45     netdev_dev_assert_class(netdev_dev, &netdev_gre_class);
46     return CONTAINER_OF(netdev_dev, struct netdev_dev_gre, netdev_dev);
47 }
48
49 static struct netdev_gre *
50 netdev_gre_cast(const struct netdev *netdev)
51 {
52     netdev_assert_class(netdev, &netdev_gre_class);
53     return CONTAINER_OF(netdev, struct netdev_gre, netdev);
54 }
55
56 static int
57 parse_config(const char *name, const struct shash *args,
58              struct gre_port_config *config)
59 {
60     struct shash_node *node;
61
62     memset(config, 0, sizeof *config);
63
64     config->flags |= GRE_F_PMTUD;
65
66     SHASH_FOR_EACH (node, args) {
67         if (!strcmp(node->name, "remote_ip")) {
68             struct in_addr in_addr;
69             if (lookup_ip(node->data, &in_addr)) {
70                 VLOG_WARN("%s: bad gre 'remote_ip'", name);
71             } else {
72                 config->daddr = in_addr.s_addr;
73             }
74         } else if (!strcmp(node->name, "local_ip")) {
75             struct in_addr in_addr;
76             if (lookup_ip(node->data, &in_addr)) {
77                 VLOG_WARN("%s: bad gre 'local_ip'", name);
78             } else {
79                 config->saddr = in_addr.s_addr;
80             }
81         } else if (!strcmp(node->name, "key")) {
82             if (!strcmp(node->data, "flow")) {
83                 config->flags |= GRE_F_IN_KEY_MATCH;
84                 config->flags |= GRE_F_OUT_KEY_ACTION;
85             } else {
86                 config->out_key = config->in_key = htonl(atoi(node->data));
87             }
88         } else if (!strcmp(node->name, "in_key")) {
89             if (!strcmp(node->data, "flow")) {
90                 config->flags |= GRE_F_IN_KEY_MATCH;
91             } else {
92                 config->in_key = htonl(atoi(node->data));
93             }
94         } else if (!strcmp(node->name, "out_key")) {
95             if (!strcmp(node->data, "flow")) {
96                 config->flags |= GRE_F_OUT_KEY_ACTION;
97             } else {
98                 config->out_key = htonl(atoi(node->data));
99             }
100         } else if (!strcmp(node->name, "tos")) {
101             if (!strcmp(node->data, "inherit")) {
102                 config->flags |= GRE_F_TOS_INHERIT;
103             } else {
104                 config->tos = atoi(node->data);
105             }
106         } else if (!strcmp(node->name, "ttl")) {
107             if (!strcmp(node->data, "inherit")) {
108                 config->flags |= GRE_F_TTL_INHERIT;
109             } else {
110                 config->ttl = atoi(node->data);
111             }
112         } else if (!strcmp(node->name, "csum")) {
113             if (!strcmp(node->data, "true")) {
114                 config->flags |= GRE_F_IN_CSUM;
115                 config->flags |= GRE_F_OUT_CSUM;
116             }
117         } else if (!strcmp(node->name, "pmtud")) {
118             if (!strcmp(node->data, "false")) {
119                 config->flags &= ~GRE_F_PMTUD;
120             }
121         } else {
122             VLOG_WARN("%s: unknown gre argument '%s'", name, node->name);
123         }
124     }
125
126     if (!config->daddr) {
127         VLOG_WARN("%s: gre type requires valid 'remote_ip' argument", name);
128         return EINVAL;
129     }
130
131     return 0;
132 }
133
134 static int
135 netdev_gre_create(const char *name, const char *type OVS_UNUSED,
136                   const struct shash *args, struct netdev_dev **netdev_devp)
137 {
138     int err;
139     struct odp_vport_add ova;
140     struct gre_port_config port_config;
141     struct netdev_dev_gre *netdev_dev;
142
143     ovs_strlcpy(ova.port_type, "gre", sizeof ova.port_type);
144     ovs_strlcpy(ova.devname, name, sizeof ova.devname);
145     ova.config = &port_config;
146
147     err = parse_config(name, args, &port_config);
148     if (err) {
149         return err;
150     }
151
152     err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
153     if (err == EBUSY) {
154         VLOG_WARN("%s: destroying existing device", name);
155
156         err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
157         if (err) {
158             return err;
159         }
160
161         err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
162     }
163
164     if (err) {
165         return err;
166     }
167
168     netdev_dev = xmalloc(sizeof *netdev_dev);
169     netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
170
171     *netdev_devp = &netdev_dev->netdev_dev;
172     return 0;
173 }
174
175 static int
176 netdev_gre_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
177 {
178     const char *name = netdev_dev_get_name(netdev_dev_);
179     struct odp_vport_mod ovm;
180     struct gre_port_config port_config;
181     int err;
182
183     ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
184     ovm.config = &port_config;
185
186     err = parse_config(name, args, &port_config);
187     if (err) {
188         return err;
189     }
190
191     return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
192 }
193
194 static void
195 netdev_gre_destroy(struct netdev_dev *netdev_dev_)
196 {
197     struct netdev_dev_gre *netdev_dev = netdev_dev_gre_cast(netdev_dev_);
198
199     netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
200     free(netdev_dev);
201 }
202
203 static int
204 netdev_gre_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
205                 struct netdev **netdevp)
206 {
207     struct netdev_gre *netdev;
208
209     netdev = xmalloc(sizeof *netdev);
210     netdev_init(&netdev->netdev, netdev_dev_);
211
212     *netdevp = &netdev->netdev;
213     return 0;
214 }
215
216 static void
217 netdev_gre_close(struct netdev *netdev_)
218 {
219     struct netdev_gre *netdev = netdev_gre_cast(netdev_);
220     free(netdev);
221 }
222
223 const struct netdev_class netdev_gre_class = {
224     "gre",
225
226     NULL,                       /* init */
227     NULL,                       /* run */
228     NULL,                       /* wait */
229
230     netdev_gre_create,
231     netdev_gre_destroy,
232     netdev_gre_reconfigure,
233
234     netdev_gre_open,
235     netdev_gre_close,
236
237     NULL,                       /* enumerate */
238
239     NULL,                       /* recv */
240     NULL,                       /* recv_wait */
241     NULL,                       /* drain */
242
243     NULL,                       /* send */
244     NULL,                       /* send_wait */
245
246     netdev_vport_set_etheraddr,
247     netdev_vport_get_etheraddr,
248     netdev_vport_get_mtu,
249     NULL,                       /* get_ifindex */
250     netdev_vport_get_carrier,
251     netdev_vport_get_stats,
252     netdev_vport_set_stats,
253
254     NULL,                       /* get_features */
255     NULL,                       /* set_advertisements */
256     NULL,                       /* get_vlan_vid */
257
258     NULL,                       /* set_policing */
259     NULL,                       /* get_qos_types */
260     NULL,                       /* get_qos_capabilities */
261     NULL,                       /* get_qos */
262     NULL,                       /* set_qos */
263     NULL,                       /* get_queue */
264     NULL,                       /* set_queue */
265     NULL,                       /* delete_queue */
266     NULL,                       /* get_queue_stats */
267     NULL,                       /* dump_queues */
268     NULL,                       /* dump_queue_stats */
269
270     NULL,                       /* get_in4 */
271     NULL,                       /* set_in4 */
272     NULL,                       /* get_in6 */
273     NULL,                       /* add_router */
274     NULL,                       /* get_next_hop */
275     NULL,                       /* arp_lookup */
276
277     netdev_vport_update_flags,
278
279     netdev_vport_poll_add,
280     netdev_vport_poll_remove,
281 };