#include "netdev-provider.h"
#include "ofpbuf.h"
#include "packets.h"
+#include "poll-loop.h"
#include "route-table.h"
#include "shash.h"
#include "socket-util.h"
/* Tunnels. */
struct netdev_tunnel_config tnl_cfg;
+ char egress_iface[IFNAMSIZ];
+ bool carrier_status;
/* Patch Ports. */
char *peer;
struct netdev_class netdev_class;
};
+/* Last read of the route-table's change number. */
+static uint64_t rt_change_seqno;
+
static int netdev_vport_construct(struct netdev *);
static int get_patch_config(const struct netdev *netdev, struct smap *args);
static int get_tunnel_config(const struct netdev *, struct smap *args);
+static bool tunnel_check_status_change__(struct netdev_vport *);
static bool
is_vport_class(const struct netdev_class *class)
return class->construct == netdev_vport_construct;
}
+bool
+netdev_vport_is_vport_class(const struct netdev_class *class)
+{
+ return is_vport_class(class);
+}
+
static const struct vport_class *
vport_class_cast(const struct netdev_class *class)
{
sizeof namebuf));
}
+/* Whenever the route-table change number is incremented,
+ * netdev_vport_route_changed() should be called to update
+ * the corresponding tunnel interface status. */
+static void
+netdev_vport_route_changed(void)
+{
+ struct netdev **vports;
+ size_t i, n_vports;
+
+ vports = netdev_get_vports(&n_vports);
+ for (i = 0; i < n_vports; i++) {
+ struct netdev *netdev_ = vports[i];
+ struct netdev_vport *netdev = netdev_vport_cast(netdev_);
+
+ ovs_mutex_lock(&netdev->mutex);
+ /* Finds all tunnel vports. */
+ if (netdev->tnl_cfg.ip_dst) {
+ if (tunnel_check_status_change__(netdev)) {
+ netdev_change_seq_changed(netdev_);
+ }
+ }
+ netdev_close(netdev_);
+ ovs_mutex_unlock(&netdev->mutex);
+ }
+
+ free(vports);
+}
+
static struct netdev *
netdev_vport_alloc(void)
{
return 0;
}
-static int
-tunnel_get_status(const struct netdev *netdev_, struct smap *smap)
+/* Checks if the tunnel status has changed and returns a boolean.
+ * Updates the tunnel status if it has changed. */
+static bool
+tunnel_check_status_change__(struct netdev_vport *netdev)
+ OVS_REQUIRES(netdev->mutex)
{
- struct netdev_vport *netdev = netdev_vport_cast(netdev_);
char iface[IFNAMSIZ];
+ bool status = false;
ovs_be32 route;
- ovs_mutex_lock(&netdev->mutex);
+ iface[0] = '\0';
route = netdev->tnl_cfg.ip_dst;
- ovs_mutex_unlock(&netdev->mutex);
-
if (route_table_get_name(route, iface)) {
struct netdev *egress_netdev;
- smap_add(smap, "tunnel_egress_iface", iface);
-
if (!netdev_open(iface, "system", &egress_netdev)) {
- smap_add(smap, "tunnel_egress_iface_carrier",
- netdev_get_carrier(egress_netdev) ? "up" : "down");
+ status = netdev_get_carrier(egress_netdev);
netdev_close(egress_netdev);
}
}
+ if (strcmp(netdev->egress_iface, iface)
+ || netdev->carrier_status != status) {
+ ovs_strlcpy(netdev->egress_iface, iface, IFNAMSIZ);
+ netdev->carrier_status = status;
+
+ return true;
+ }
+
+ return false;
+}
+
+static int
+tunnel_get_status(const struct netdev *netdev_, struct smap *smap)
+{
+ struct netdev_vport *netdev = netdev_vport_cast(netdev_);
+
+ if (netdev->egress_iface[0]) {
+ smap_add(smap, "tunnel_egress_iface", netdev->egress_iface);
+
+ smap_add(smap, "tunnel_egress_iface_carrier",
+ netdev->carrier_status ? "up" : "down");
+ }
+
return 0;
}
static void
netdev_vport_run(void)
{
+ uint64_t seq;
+
route_table_run();
+ seq = route_table_get_change_seq();
+ if (rt_change_seqno != seq) {
+ rt_change_seqno = seq;
+ netdev_vport_route_changed();
+ }
}
static void
netdev_vport_wait(void)
{
+ uint64_t seq;
+
route_table_wait();
+ seq = route_table_get_change_seq();
+ if (rt_change_seqno != seq) {
+ poll_immediate_wake();
+ }
}
\f
/* Code specific to tunnel types. */
ovs_mutex_lock(&dev->mutex);
dev->tnl_cfg = tnl_cfg;
+ tunnel_check_status_change__(dev);
netdev_change_seq_changed(dev_);
ovs_mutex_unlock(&dev->mutex);
ovs_mutex_unlock(&netdev_mutex);
}
+/* Extracts pointers to all 'netdev-vports' into an array 'vports'
+ * and returns it. Stores the size of the array into '*size'.
+ *
+ * The caller is responsible for freeing 'vports' and must close
+ * each 'netdev-vport' in the list. */
+struct netdev **
+netdev_get_vports(size_t *size)
+ OVS_EXCLUDED(netdev_mutex)
+{
+ struct netdev **vports;
+ struct shash_node *node;
+ size_t n = 0;
+
+ if (!size) {
+ return NULL;
+ }
+
+ /* Explicitly allocates big enough chunk of memory. */
+ vports = xmalloc(shash_count(&netdev_shash) * sizeof *vports);
+ ovs_mutex_lock(&netdev_mutex);
+ SHASH_FOR_EACH (node, &netdev_shash) {
+ struct netdev *dev = node->data;
+
+ if (netdev_vport_is_vport_class(dev->netdev_class)) {
+ dev->ref_cnt++;
+ vports[n] = dev;
+ n++;
+ }
+ }
+ ovs_mutex_unlock(&netdev_mutex);
+ *size = n;
+
+ return vports;
+}
+
const char *
netdev_get_type_from_name(const char *name)
{
/*
- * Copyright (c) 2011, 2012, 2013 Nicira, Inc.
+ * Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
+/* Global change number for route-table, which should be incremented
+ * every time route_table_reset() is called. */
+static uint64_t rt_change_seq;
+
static unsigned int register_count = 0;
static struct nln *nln = NULL;
static struct route_table_msg rtmsg;
return false;
}
+uint64_t
+route_table_get_change_seq(void)
+{
+ return rt_change_seq;
+}
+
/* Users of the route_table module should register themselves with this
* function before making any other route_table function calls. */
void
if (nln) {
rtnetlink_link_run();
nln_run(nln);
+
+ if (!route_table_valid) {
+ route_table_reset();
+ }
}
}
route_map_clear();
route_table_valid = true;
+ rt_change_seq++;
ofpbuf_init(&request, 0);