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/tunnel.h"
29 #include "socket-util.h"
32 VLOG_DEFINE_THIS_MODULE(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 tnl_port_config *config)
60 struct shash_node *node;
62 memset(config, 0, sizeof *config);
64 config->flags |= TNL_F_PMTUD;
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);
72 config->daddr = in_addr.s_addr;
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);
79 config->saddr = in_addr.s_addr;
81 } else if (!strcmp(node->name, "key")) {
82 if (!strcmp(node->data, "flow")) {
83 config->flags |= TNL_F_IN_KEY_MATCH;
84 config->flags |= TNL_F_OUT_KEY_ACTION;
86 config->out_key = config->in_key = htonl(atoi(node->data));
88 } else if (!strcmp(node->name, "in_key")) {
89 if (!strcmp(node->data, "flow")) {
90 config->flags |= TNL_F_IN_KEY_MATCH;
92 config->in_key = htonl(atoi(node->data));
94 } else if (!strcmp(node->name, "out_key")) {
95 if (!strcmp(node->data, "flow")) {
96 config->flags |= TNL_F_OUT_KEY_ACTION;
98 config->out_key = htonl(atoi(node->data));
100 } else if (!strcmp(node->name, "tos")) {
101 if (!strcmp(node->data, "inherit")) {
102 config->flags |= TNL_F_TOS_INHERIT;
104 config->tos = atoi(node->data);
106 } else if (!strcmp(node->name, "ttl")) {
107 if (!strcmp(node->data, "inherit")) {
108 config->flags |= TNL_F_TTL_INHERIT;
110 config->ttl = atoi(node->data);
112 } else if (!strcmp(node->name, "csum")) {
113 if (!strcmp(node->data, "true")) {
114 config->flags |= TNL_F_CSUM;
116 } else if (!strcmp(node->name, "pmtud")) {
117 if (!strcmp(node->data, "false")) {
118 config->flags &= ~TNL_F_PMTUD;
121 VLOG_WARN("%s: unknown gre argument '%s'", name, node->name);
125 if (!config->daddr) {
126 VLOG_WARN("%s: gre type requires valid 'remote_ip' argument", name);
134 netdev_gre_create(const char *name, const char *type OVS_UNUSED,
135 const struct shash *args, struct netdev_dev **netdev_devp)
138 struct odp_vport_add ova;
139 struct tnl_port_config port_config;
140 struct netdev_dev_gre *netdev_dev;
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;
146 err = parse_config(name, args, &port_config);
151 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
153 VLOG_WARN("%s: destroying existing device", name);
155 err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
160 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
167 netdev_dev = xmalloc(sizeof *netdev_dev);
168 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
170 *netdev_devp = &netdev_dev->netdev_dev;
175 netdev_gre_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
177 const char *name = netdev_dev_get_name(netdev_dev_);
178 struct odp_vport_mod ovm;
179 struct tnl_port_config port_config;
182 ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
183 ovm.config = &port_config;
185 err = parse_config(name, args, &port_config);
190 return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
194 netdev_gre_destroy(struct netdev_dev *netdev_dev_)
196 struct netdev_dev_gre *netdev_dev = netdev_dev_gre_cast(netdev_dev_);
198 netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
203 netdev_gre_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
204 struct netdev **netdevp)
206 struct netdev_gre *netdev;
208 netdev = xmalloc(sizeof *netdev);
209 netdev_init(&netdev->netdev, netdev_dev_);
211 *netdevp = &netdev->netdev;
216 netdev_gre_close(struct netdev *netdev_)
218 struct netdev_gre *netdev = netdev_gre_cast(netdev_);
222 const struct netdev_class netdev_gre_class = {
231 netdev_gre_reconfigure,
236 NULL, /* enumerate */
239 NULL, /* recv_wait */
243 NULL, /* send_wait */
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,
253 NULL, /* get_features */
254 NULL, /* set_advertisements */
255 NULL, /* get_vlan_vid */
257 NULL, /* set_policing */
258 NULL, /* get_qos_types */
259 NULL, /* get_qos_capabilities */
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 */
272 NULL, /* add_router */
273 NULL, /* get_next_hop */
274 NULL, /* arp_lookup */
276 netdev_vport_update_flags,
278 netdev_vport_poll_add,
279 netdev_vport_poll_remove,