2 * Copyright (c) 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/ioctl.h>
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"
29 #include "socket-util.h"
31 #define THIS_MODULE VLM_netdev_gre
34 struct netdev_dev_gre {
35 struct netdev_dev netdev_dev;
42 static struct netdev_dev_gre *
43 netdev_dev_gre_cast(const struct netdev_dev *netdev_dev)
45 netdev_dev_assert_class(netdev_dev, &netdev_gre_class);
46 return CONTAINER_OF(netdev_dev, struct netdev_dev_gre, netdev_dev);
49 static struct netdev_gre *
50 netdev_gre_cast(const struct netdev *netdev)
52 netdev_assert_class(netdev, &netdev_gre_class);
53 return CONTAINER_OF(netdev, struct netdev_gre, netdev);
57 parse_config(const char *name, const struct shash *args,
58 struct gre_port_config *config)
60 struct shash_node *node;
62 memset(config, 0, sizeof *config);
64 config->flags |= GRE_F_IN_CSUM;
65 config->flags |= GRE_F_OUT_CSUM;
66 config->flags |= GRE_F_PMTUD;
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);
74 config->daddr = in_addr.s_addr;
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);
81 config->saddr = in_addr.s_addr;
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;
88 config->out_key = config->in_key = htonl(atoi(node->data));
90 } else if (!strcmp(node->name, "in_key")) {
91 if (!strcmp(node->data, "flow")) {
92 config->flags |= GRE_F_IN_KEY_MATCH;
94 config->in_key = htonl(atoi(node->data));
96 } else if (!strcmp(node->name, "out_key")) {
97 if (!strcmp(node->data, "flow")) {
98 config->flags |= GRE_F_OUT_KEY_ACTION;
100 config->out_key = htonl(atoi(node->data));
102 } else if (!strcmp(node->name, "tos")) {
103 if (!strcmp(node->data, "inherit")) {
104 config->flags |= GRE_F_TOS_INHERIT;
106 config->tos = atoi(node->data);
108 } else if (!strcmp(node->name, "ttl")) {
109 if (!strcmp(node->data, "inherit")) {
110 config->flags |= GRE_F_TTL_INHERIT;
112 config->ttl = atoi(node->data);
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;
119 } else if (!strcmp(node->name, "pmtud")) {
120 if (!strcmp(node->data, "false")) {
121 config->flags &= ~GRE_F_PMTUD;
124 VLOG_WARN("%s: unknown gre argument '%s'", name, node->name);
128 if (!config->daddr) {
129 VLOG_WARN("%s: gre type requires valid 'remote_ip' argument", name);
137 netdev_gre_create(const char *name, const char *type OVS_UNUSED,
138 const struct shash *args, struct netdev_dev **netdev_devp)
141 struct odp_vport_add ova;
142 struct gre_port_config port_config;
143 struct netdev_dev_gre *netdev_dev;
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;
149 err = parse_config(name, args, &port_config);
154 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
156 VLOG_WARN("%s: destroying existing device", name);
158 err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
163 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
170 netdev_dev = xmalloc(sizeof *netdev_dev);
171 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
173 *netdev_devp = &netdev_dev->netdev_dev;
178 netdev_gre_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
180 const char *name = netdev_dev_get_name(netdev_dev_);
181 struct odp_vport_mod ovm;
182 struct gre_port_config port_config;
185 ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
186 ovm.config = &port_config;
188 err = parse_config(name, args, &port_config);
193 return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
197 netdev_gre_destroy(struct netdev_dev *netdev_dev_)
199 struct netdev_dev_gre *netdev_dev = netdev_dev_gre_cast(netdev_dev_);
201 netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
206 netdev_gre_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
207 struct netdev **netdevp)
209 struct netdev_gre *netdev;
211 netdev = xmalloc(sizeof *netdev);
212 netdev_init(&netdev->netdev, netdev_dev_);
214 *netdevp = &netdev->netdev;
219 netdev_gre_close(struct netdev *netdev_)
221 struct netdev_gre *netdev = netdev_gre_cast(netdev_);
225 const struct netdev_class netdev_gre_class = {
234 netdev_gre_reconfigure,
239 NULL, /* enumerate */
242 NULL, /* recv_wait */
246 NULL, /* send_wait */
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,
256 NULL, /* get_features */
257 NULL, /* set_advertisements */
258 NULL, /* get_vlan_vid */
260 NULL, /* set_policing */
261 NULL, /* get_qos_types */
262 NULL, /* get_qos_capabilities */
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 */
275 NULL, /* add_router */
276 NULL, /* get_next_hop */
277 NULL, /* arp_lookup */
279 netdev_vport_update_flags,
281 netdev_vport_poll_add,
282 netdev_vport_poll_remove,