Remove ezio-term and ovs-switchui utilities.
[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_CSUM;
115             }
116         } else if (!strcmp(node->name, "pmtud")) {
117             if (!strcmp(node->data, "false")) {
118                 config->flags &= ~GRE_F_PMTUD;
119             }
120         } else {
121             VLOG_WARN("%s: unknown gre argument '%s'", name, node->name);
122         }
123     }
124
125     if (!config->daddr) {
126         VLOG_WARN("%s: gre type requires valid 'remote_ip' argument", name);
127         return EINVAL;
128     }
129
130     return 0;
131 }
132
133 static int
134 netdev_gre_create(const char *name, const char *type OVS_UNUSED,
135                   const struct shash *args, struct netdev_dev **netdev_devp)
136 {
137     int err;
138     struct odp_vport_add ova;
139     struct gre_port_config port_config;
140     struct netdev_dev_gre *netdev_dev;
141
142     ovs_strlcpy(ova.port_type, "gre", sizeof ova.port_type);
143     ovs_strlcpy(ova.devname, name, sizeof ova.devname);
144     ova.config = &port_config;
145
146     err = parse_config(name, args, &port_config);
147     if (err) {
148         return err;
149     }
150
151     err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
152     if (err == EBUSY) {
153         VLOG_WARN("%s: destroying existing device", name);
154
155         err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
156         if (err) {
157             return err;
158         }
159
160         err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
161     }
162
163     if (err) {
164         return err;
165     }
166
167     netdev_dev = xmalloc(sizeof *netdev_dev);
168     netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
169
170     *netdev_devp = &netdev_dev->netdev_dev;
171     return 0;
172 }
173
174 static int
175 netdev_gre_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
176 {
177     const char *name = netdev_dev_get_name(netdev_dev_);
178     struct odp_vport_mod ovm;
179     struct gre_port_config port_config;
180     int err;
181
182     ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
183     ovm.config = &port_config;
184
185     err = parse_config(name, args, &port_config);
186     if (err) {
187         return err;
188     }
189
190     return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
191 }
192
193 static void
194 netdev_gre_destroy(struct netdev_dev *netdev_dev_)
195 {
196     struct netdev_dev_gre *netdev_dev = netdev_dev_gre_cast(netdev_dev_);
197
198     netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
199     free(netdev_dev);
200 }
201
202 static int
203 netdev_gre_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
204                 struct netdev **netdevp)
205 {
206     struct netdev_gre *netdev;
207
208     netdev = xmalloc(sizeof *netdev);
209     netdev_init(&netdev->netdev, netdev_dev_);
210
211     *netdevp = &netdev->netdev;
212     return 0;
213 }
214
215 static void
216 netdev_gre_close(struct netdev *netdev_)
217 {
218     struct netdev_gre *netdev = netdev_gre_cast(netdev_);
219     free(netdev);
220 }
221
222 const struct netdev_class netdev_gre_class = {
223     "gre",
224
225     NULL,                       /* init */
226     NULL,                       /* run */
227     NULL,                       /* wait */
228
229     netdev_gre_create,
230     netdev_gre_destroy,
231     netdev_gre_reconfigure,
232
233     netdev_gre_open,
234     netdev_gre_close,
235
236     NULL,                       /* enumerate */
237
238     NULL,                       /* recv */
239     NULL,                       /* recv_wait */
240     NULL,                       /* drain */
241
242     NULL,                       /* send */
243     NULL,                       /* send_wait */
244
245     netdev_vport_set_etheraddr,
246     netdev_vport_get_etheraddr,
247     netdev_vport_get_mtu,
248     NULL,                       /* get_ifindex */
249     netdev_vport_get_carrier,
250     netdev_vport_get_stats,
251     netdev_vport_set_stats,
252
253     NULL,                       /* get_features */
254     NULL,                       /* set_advertisements */
255     NULL,                       /* get_vlan_vid */
256
257     NULL,                       /* set_policing */
258     NULL,                       /* get_qos_types */
259     NULL,                       /* get_qos_capabilities */
260     NULL,                       /* get_qos */
261     NULL,                       /* set_qos */
262     NULL,                       /* get_queue */
263     NULL,                       /* set_queue */
264     NULL,                       /* delete_queue */
265     NULL,                       /* get_queue_stats */
266     NULL,                       /* dump_queues */
267     NULL,                       /* dump_queue_stats */
268
269     NULL,                       /* get_in4 */
270     NULL,                       /* set_in4 */
271     NULL,                       /* get_in6 */
272     NULL,                       /* add_router */
273     NULL,                       /* get_next_hop */
274     NULL,                       /* arp_lookup */
275
276     netdev_vport_update_flags,
277
278     netdev_vport_poll_add,
279     netdev_vport_poll_remove,
280 };