removed useless mtu and carrier
[sliver-openvswitch.git] / lib / netdev-tunnel.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 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
19 #include <unistd.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <errno.h>
24
25 #include "flow.h"
26 #include "list.h"
27 #include "netdev-provider.h"
28 #include "odp-util.h"
29 #include "ofp-print.h"
30 #include "ofpbuf.h"
31 #include "packets.h"
32 #include "poll-loop.h"
33 #include "shash.h"
34 #include "sset.h"
35 #include "unixctl.h"
36 #include "socket-util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(netdev_tunnel);
40
41 struct netdev_dev_tunnel {
42     struct netdev_dev netdev_dev;
43     uint8_t hwaddr[ETH_ADDR_LEN];
44     struct netdev_stats stats;
45     enum netdev_flags flags;
46     int sockfd;
47     struct sockaddr_in local_addr;
48     struct sockaddr_in remote_addr;
49     bool valid_remote_ip;
50     bool valid_remote_port;
51     bool connected;
52     unsigned int change_seq;
53 };
54
55 struct netdev_tunnel {
56     struct netdev netdev;
57 } ;
58
59 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
60
61 static struct shash tunnel_netdev_devs = SHASH_INITIALIZER(&tunnel_netdev_devs);
62
63 static int netdev_tunnel_create(const struct netdev_class *, const char *,
64                                struct netdev_dev **);
65 static void netdev_tunnel_poll_notify(const struct netdev *);
66
67 static bool
68 is_tunnel_class(const struct netdev_class *class)
69 {
70     return class->create == netdev_tunnel_create;
71 }
72
73 static struct netdev_dev_tunnel *
74 netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
75 {
76     assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
77     return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
78 }
79
80 static struct netdev_tunnel *
81 netdev_tunnel_cast(const struct netdev *netdev)
82 {
83     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
84     assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
85     return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
86 }
87
88 static int
89 netdev_tunnel_create(const struct netdev_class *class, const char *name,
90                     struct netdev_dev **netdev_devp)
91 {
92     static unsigned int n = 0xaa550000;
93     struct netdev_dev_tunnel *netdev_dev;
94     int error;
95
96     netdev_dev = xzalloc(sizeof *netdev_dev);
97     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
98     netdev_dev->hwaddr[0] = 0x55;
99     netdev_dev->hwaddr[1] = 0xaa;
100     netdev_dev->hwaddr[2] = n >> 24;
101     netdev_dev->hwaddr[3] = n >> 16;
102     netdev_dev->hwaddr[4] = n >> 8;
103     netdev_dev->hwaddr[5] = n;
104     netdev_dev->flags = 0;
105     netdev_dev->change_seq = 1;
106     memset(&netdev_dev->remote_addr, 0, sizeof(netdev_dev->remote_addr));
107     netdev_dev->valid_remote_ip = false;
108     netdev_dev->valid_remote_port = false;
109     netdev_dev->connected = false;
110
111
112     netdev_dev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev_dev->local_addr, 0);
113     if (netdev_dev->sockfd < 0) {
114         error = netdev_dev->sockfd;
115         goto error;
116     }
117
118
119     shash_add(&tunnel_netdev_devs, name, netdev_dev);
120
121     n++;
122
123     *netdev_devp = &netdev_dev->netdev_dev;
124
125     VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev_dev->sockfd, netdev_dev->local_addr.sin_port);
126
127     return 0;
128
129 error:
130     free(netdev_dev);
131     return error;
132 }
133
134 static void
135 netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
136 {
137     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
138
139     if (netdev_dev->sockfd != -1)
140         close(netdev_dev->sockfd);
141
142     shash_find_and_delete(&tunnel_netdev_devs,
143                           netdev_dev_get_name(netdev_dev_));
144     free(netdev_dev);
145 }
146
147 static int
148 netdev_tunnel_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
149 {
150     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
151     struct netdev_tunnel *netdev;
152
153     netdev = xmalloc(sizeof *netdev);
154     netdev_init(&netdev->netdev, netdev_dev_);
155
156     *netdevp = &netdev->netdev;
157     return 0;
158 }
159
160 static void
161 netdev_tunnel_close(struct netdev *netdev_)
162 {
163     struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
164     free(netdev);
165 }
166
167 static int
168 netdev_tunnel_get_config(struct netdev_dev *dev_, struct shash *args)
169 {
170     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
171
172     if (netdev_dev->valid_remote_ip)
173         shash_add(args, "remote_ip",
174             xasprintf(IP_FMT, IP_ARGS(&netdev_dev->remote_addr.sin_addr)));
175     if (netdev_dev->valid_remote_port)
176         shash_add(args, "remote_port",
177             xasprintf("%"PRIu16, ntohs(netdev_dev->remote_addr.sin_port)));
178     return 0;
179 }
180
181 static int
182 netdev_tunnel_connect(struct netdev_dev_tunnel *dev)
183 {
184     if (dev->sockfd < 0)
185         return EBADF;
186     if (!dev->valid_remote_ip || !dev->valid_remote_port)
187         return 0;
188     dev->remote_addr.sin_family = AF_INET;
189     if (connect(dev->sockfd, (struct sockaddr*) &dev->remote_addr, sizeof(dev->remote_addr)) < 0) {
190         return errno;
191     }
192     dev->connected = true;
193     VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
194         inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
195     return 0;
196 }
197
198 static int
199 netdev_tunnel_set_config(struct netdev_dev *dev_, const struct shash *args)
200 {
201     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
202     struct shash_node *node;
203
204     VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
205     SHASH_FOR_EACH(node, args) {
206         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
207         if (!strcmp(node->name, "remote_ip")) {
208             struct in_addr addr;
209             if (lookup_ip(node->data, &addr)) {
210                 VLOG_WARN("%s: bad 'remote_ip'", node->name);
211             } else {
212                 netdev_dev->remote_addr.sin_addr = addr;
213                 netdev_dev->valid_remote_ip = true;
214             }
215         } else if (!strcmp(node->name, "remote_port")) {
216             netdev_dev->remote_addr.sin_port = htons(atoi(node->data));
217             netdev_dev->valid_remote_port = true;
218         } else {
219             VLOG_WARN("%s: unknown argument '%s'", 
220                 netdev_dev_get_name(dev_), node->name);
221         }
222     }
223     return netdev_tunnel_connect(netdev_dev);        
224 }
225
226 static int
227 netdev_tunnel_listen(struct netdev *netdev_)
228 {
229     return 0;
230 }
231
232 static int
233 netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
234 {
235     struct netdev_dev_tunnel *dev = 
236         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
237     if (!dev->connected)
238         return -EAGAIN;
239     for (;;) {
240         ssize_t retval;
241         retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
242         VLOG_DBG("%s: recv(%x, %d, MSG_TRUNC) = %d",
243                         netdev_get_name(netdev_), buffer, size, retval);
244         if (retval >= 0) {
245             return retval <= size ? retval : -EMSGSIZE;
246         } else if (errno != EINTR) {
247             if (errno != EAGAIN) {
248                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
249                     netdev_get_name(netdev_), strerror(errno));
250             }
251             return -errno;
252         }
253     }
254 }
255
256 static void
257 netdev_tunnel_recv_wait(struct netdev *netdev_)
258 {
259     struct netdev_dev_tunnel *dev = 
260         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
261     if (dev->sockfd >= 0) {
262         poll_fd_wait(dev->sockfd, POLLIN);
263     }
264 }
265
266 static int
267 netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
268 {
269     struct netdev_dev_tunnel *dev = 
270         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
271     if (!dev->connected)
272         return EAGAIN;
273     for (;;) {
274         ssize_t retval;
275         retval = send(dev->sockfd, buffer, size, 0);
276         VLOG_DBG("%s: send(%x, %d) = %d", netdev_get_name(netdev_), buffer, size, retval);
277         if (retval >= 0) {
278             if (retval != size) {
279                 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
280                              "%zu) on %s", retval, size, netdev_get_name(netdev_));
281             }
282             return 0;
283         } else if (errno != EINTR) {
284             if (errno != EAGAIN) {
285                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
286                     netdev_get_name(netdev_), strerror(errno));
287             }
288             return errno;
289         }
290     }
291 }
292
293 static void
294 netdev_tunnel_send_wait(struct netdev *netdev_)
295 {
296     struct netdev_dev_tunnel *dev = 
297         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
298     if (dev->sockfd >= 0) {
299         poll_fd_wait(dev->sockfd, POLLOUT);
300     }
301 }
302
303 static int
304 netdev_tunnel_drain(struct netdev *netdev_)
305 {
306     struct netdev_dev_tunnel *dev = 
307         netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
308     char buffer[128];
309     int error;
310
311     if (!dev->connected)
312         return 0;
313     for (;;) {
314         error = recv(dev->sockfd, buffer, 128, MSG_TRUNC);
315         if (error) {
316             if (error == -EAGAIN)
317                 break;
318             else if (error != -EMSGSIZE)
319                 return error;
320         }
321     }
322     return 0;
323 }
324
325 static int
326 netdev_tunnel_set_etheraddr(struct netdev *netdev,
327                            const uint8_t mac[ETH_ADDR_LEN])
328 {
329     struct netdev_dev_tunnel *dev =
330         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
331
332     if (!eth_addr_equals(dev->hwaddr, mac)) {
333         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
334         netdev_tunnel_poll_notify(netdev);
335     }
336
337     return 0;
338 }
339
340 static int
341 netdev_tunnel_get_etheraddr(const struct netdev *netdev,
342                            uint8_t mac[ETH_ADDR_LEN])
343 {
344     const struct netdev_dev_tunnel *dev =
345         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
346
347     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
348     return 0;
349 }
350
351
352 static int
353 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
354 {
355     const struct netdev_dev_tunnel *dev =
356         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
357
358     *stats = dev->stats;
359     return 0;
360 }
361
362 static int
363 netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
364 {
365     struct netdev_dev_tunnel *dev =
366         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
367
368     dev->stats = *stats;
369     return 0;
370 }
371
372 static int
373 netdev_tunnel_update_flags(struct netdev *netdev,
374                           enum netdev_flags off, enum netdev_flags on,
375                           enum netdev_flags *old_flagsp)
376 {
377     struct netdev_dev_tunnel *dev =
378         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
379
380     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
381         return EINVAL;
382     }
383
384     *old_flagsp = dev->flags;
385     dev->flags |= on;
386     dev->flags &= ~off;
387     if (*old_flagsp != dev->flags) {
388         netdev_tunnel_poll_notify(netdev);
389     }
390     return 0;
391 }
392
393 static unsigned int
394 netdev_tunnel_change_seq(const struct netdev *netdev)
395 {
396     return netdev_dev_tunnel_cast(netdev_get_dev(netdev))->change_seq;
397 }
398 \f
399 /* Helper functions. */
400
401 static void
402 netdev_tunnel_poll_notify(const struct netdev *netdev)
403 {
404     struct netdev_dev_tunnel *dev =
405         netdev_dev_tunnel_cast(netdev_get_dev(netdev));
406
407     dev->change_seq++;
408     if (!dev->change_seq) {
409         dev->change_seq++;
410     }
411 }
412
413 static void
414 netdev_tunnel_get_port(struct unixctl_conn *conn,
415                      int argc, const char *argv[], void *aux OVS_UNUSED)
416 {
417     struct netdev_dev_tunnel *tunnel_dev;
418     uint16_t port;
419     char buf[6];
420
421     tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
422     if (!tunnel_dev) {
423         unixctl_command_reply_error(conn, "no such tunnel netdev");
424         return;
425     }
426
427     sprintf(buf, "%d", ntohs(tunnel_dev->local_addr.sin_port));
428     unixctl_command_reply(conn, buf);
429 }
430
431
432 static int
433 netdev_tunnel_init(void)
434 {
435     unixctl_command_register("netdev-tunnel/get-port", "NAME",
436                              1, 1, netdev_tunnel_get_port, NULL);
437     return 0;
438 }
439
440 const struct netdev_class netdev_tunnel_class = {
441     "tunnel",
442     netdev_tunnel_init,         /* init */
443     NULL,                       /* run */
444     NULL,                       /* wait */
445
446     netdev_tunnel_create,
447     netdev_tunnel_destroy,
448     netdev_tunnel_get_config,
449     netdev_tunnel_set_config, 
450
451     netdev_tunnel_open,
452     netdev_tunnel_close,
453
454     netdev_tunnel_listen,
455     netdev_tunnel_recv,
456     netdev_tunnel_recv_wait,
457     netdev_tunnel_drain,
458
459     netdev_tunnel_send, 
460     netdev_tunnel_send_wait,  
461
462     netdev_tunnel_set_etheraddr,
463     netdev_tunnel_get_etheraddr,
464     NULL,                       /* get_mtu */
465     NULL,                       /* set_mtu */
466     NULL,                       /* get_ifindex */
467     NULL,                       /* get_carrier */
468     NULL,                       /* get_carrier_resets */
469     NULL,                       /* get_miimon */
470     netdev_tunnel_get_stats,
471     netdev_tunnel_set_stats,
472
473     NULL,                       /* get_features */
474     NULL,                       /* set_advertisements */
475
476     NULL,                       /* set_policing */
477     NULL,                       /* get_qos_types */
478     NULL,                       /* get_qos_capabilities */
479     NULL,                       /* get_qos */
480     NULL,                       /* set_qos */
481     NULL,                       /* get_queue */
482     NULL,                       /* set_queue */
483     NULL,                       /* delete_queue */
484     NULL,                       /* get_queue_stats */
485     NULL,                       /* dump_queues */
486     NULL,                       /* dump_queue_stats */
487
488     NULL,                       /* get_in4 */
489     NULL,                       /* set_in4 */
490     NULL,                       /* get_in6 */
491     NULL,                       /* add_router */
492     NULL,                       /* get_next_hop */
493     NULL,                       /* get_drv_info */
494     NULL,                       /* arp_lookup */
495
496     netdev_tunnel_update_flags,
497
498     netdev_tunnel_change_seq
499 };