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