2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-31 14:25:26 +00:00

route: support IPv6 and use IPv4-mapped addresses

This adds support for IPv6 in ovs-router and route-table. IPv4 is stored in
ovs-router using IPv4-mapped addresses.

Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@redhat.com>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
This commit is contained in:
Thadeu Lima de Souza Cascardo
2015-09-29 19:10:56 -03:00
committed by Pravin B Shelar
parent 12c96b8af6
commit 0b8da9ae1f
6 changed files with 196 additions and 88 deletions

View File

@@ -321,7 +321,7 @@ tunnel_check_status_change__(struct netdev_vport *netdev)
iface[0] = '\0';
route = netdev->tnl_cfg.ip_dst;
if (ovs_router_lookup(route, iface, &gw)) {
if (ovs_router_lookup4(route, iface, &gw)) {
struct netdev *egress_netdev;
if (!netdev_open(iface, "system", &egress_netdev)) {

View File

@@ -49,8 +49,8 @@ static struct classifier cls;
struct ovs_router_entry {
struct cls_rule cr;
char output_bridge[IFNAMSIZ];
ovs_be32 gw;
ovs_be32 nw_addr;
struct in6_addr gw;
struct in6_addr nw_addr;
uint8_t plen;
uint8_t priority;
};
@@ -66,10 +66,11 @@ ovs_router_entry_cast(const struct cls_rule *cr)
}
bool
ovs_router_lookup(ovs_be32 ip_dst, char output_bridge[], ovs_be32 *gw)
ovs_router_lookup(const struct in6_addr *ip6_dst, char output_bridge[],
struct in6_addr *gw)
{
const struct cls_rule *cr;
struct flow flow = {.nw_dst = ip_dst};
struct flow flow = {.ipv6_dst = *ip6_dst};
cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
if (cr) {
@@ -79,6 +80,20 @@ ovs_router_lookup(ovs_be32 ip_dst, char output_bridge[], ovs_be32 *gw)
*gw = p->gw;
return true;
}
return false;
}
bool
ovs_router_lookup4(ovs_be32 ip_dst, char output_bridge[], ovs_be32 *gw)
{
struct in6_addr ip6_dst;
struct in6_addr gw6;
in6_addr_set_mapped_ipv4(&ip6_dst, ip_dst);
if (ovs_router_lookup(&ip6_dst, output_bridge, &gw6)) {
*gw = in6_addr_get_mapped_ipv4(&gw6);
return true;
}
return route_table_fallback_lookup(ip_dst, output_bridge, gw);
}
@@ -89,33 +104,37 @@ rt_entry_free(struct ovs_router_entry *p)
free(p);
}
static void rt_init_match(struct match *match, ovs_be32 ip_dst, uint8_t plen)
static void rt_init_match(struct match *match, const struct in6_addr *ip6_dst,
uint8_t plen)
{
ovs_be32 mask;
struct in6_addr dst;
struct in6_addr mask;
mask = be32_prefix_mask(plen);
mask = ipv6_create_mask(plen);
ip_dst &= mask; /* Clear out insignificant bits. */
dst = ipv6_addr_bitand(ip6_dst, &mask);
memset(match, 0, sizeof *match);
match->flow.nw_dst = ip_dst;
match->wc.masks.nw_dst = mask;
match->flow.ipv6_dst = dst;
match->wc.masks.ipv6_dst = mask;
}
static void
ovs_router_insert__(uint8_t priority, ovs_be32 ip_dst, uint8_t plen,
const char output_bridge[],
ovs_be32 gw)
ovs_router_insert__(uint8_t priority, const struct in6_addr *ip6_dst,
uint8_t plen, const char output_bridge[],
const struct in6_addr *gw)
{
const struct cls_rule *cr;
struct ovs_router_entry *p;
struct match match;
rt_init_match(&match, ip_dst, plen);
rt_init_match(&match, ip6_dst, plen);
p = xzalloc(sizeof *p);
ovs_strlcpy(p->output_bridge, output_bridge, sizeof p->output_bridge);
p->gw = gw;
p->nw_addr = match.flow.nw_dst;
if (ipv6_addr_is_set(gw)) {
p->gw = *gw;
}
p->nw_addr = match.flow.ipv6_dst;
p->plen = plen;
p->priority = priority;
/* Longest prefix matches first. */
@@ -134,8 +153,8 @@ ovs_router_insert__(uint8_t priority, ovs_be32 ip_dst, uint8_t plen,
}
void
ovs_router_insert(ovs_be32 ip_dst, uint8_t plen, const char output_bridge[],
ovs_be32 gw)
ovs_router_insert(const struct in6_addr *ip_dst, uint8_t plen,
const char output_bridge[], const struct in6_addr *gw)
{
ovs_router_insert__(plen, ip_dst, plen, output_bridge, gw);
}
@@ -157,14 +176,14 @@ __rt_entry_delete(const struct cls_rule *cr)
}
static bool
rt_entry_delete(uint8_t priority, ovs_be32 ip_dst, uint8_t plen)
rt_entry_delete(uint8_t priority, const struct in6_addr *ip6_dst, uint8_t plen)
{
const struct cls_rule *cr;
struct cls_rule rule;
struct match match;
bool res = false;
rt_init_match(&match, ip_dst, plen);
rt_init_match(&match, ip6_dst, plen);
cls_rule_init(&rule, &match, priority);
@@ -178,6 +197,27 @@ rt_entry_delete(uint8_t priority, ovs_be32 ip_dst, uint8_t plen)
return res;
}
static bool
scan_ipv6_route(const char *s, struct in6_addr *addr, unsigned int *plen)
{
int len, n;
int slen = strlen(s);
char ipv6_s[IPV6_SCAN_LEN + 1];
if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &len)
&& inet_pton(AF_INET6, ipv6_s, addr) == 1) {
if (len == slen) {
*plen = 128;
return true;
}
if (ovs_scan(s + len, "/%u%n", plen, &n)
&& len + n == slen && *plen <= 128) {
return true;
}
}
return false;
}
static bool
scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
{
@@ -216,6 +256,8 @@ ovs_router_add(struct unixctl_conn *conn, int argc,
{
ovs_be32 ip, gw;
unsigned int plen;
struct in6_addr ip6;
struct in6_addr gw6;
if (scan_ipv4_route(argv[1], &ip, &plen)) {
if (argc > 3) {
@@ -223,11 +265,20 @@ ovs_router_add(struct unixctl_conn *conn, int argc,
} else {
gw = 0;
}
ovs_router_insert__(plen + 32, ip, plen, argv[2], gw);
unixctl_command_reply(conn, "OK");
in6_addr_set_mapped_ipv4(&ip6, ip);
in6_addr_set_mapped_ipv4(&gw6, gw);
plen += 96;
} else if (scan_ipv6_route(argv[1], &ip6, &plen)) {
if (argc > 3) {
inet_pton(AF_INET6, argv[3], &gw6);
} else {
gw6 = in6addr_any;
}
} else {
unixctl_command_reply(conn, "Invalid parameters");
}
ovs_router_insert__(plen + 32, &ip6, plen, argv[2], &gw6);
unixctl_command_reply(conn, "OK");
}
static void
@@ -236,18 +287,20 @@ ovs_router_del(struct unixctl_conn *conn, int argc OVS_UNUSED,
{
ovs_be32 ip;
unsigned int plen;
struct in6_addr ip6;
if (scan_ipv4_route(argv[1], &ip, &plen)) {
if (rt_entry_delete(plen + 32, ip, plen)) {
unixctl_command_reply(conn, "OK");
seq_change(tnl_conf_seq);
} else {
unixctl_command_reply(conn, "Not found");
}
} else {
in6_addr_set_mapped_ipv4(&ip6, ip);
plen += 96;
} else if (!scan_ipv6_route(argv[1], &ip6, &plen)) {
unixctl_command_reply(conn, "Invalid parameters");
}
if (rt_entry_delete(plen + 32, &ip6, plen)) {
unixctl_command_reply(conn, "OK");
seq_change(tnl_conf_seq);
} else {
unixctl_command_reply(conn, "Not found");
}
}
static void
@@ -259,16 +312,21 @@ ovs_router_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
ds_put_format(&ds, "Route Table:\n");
CLS_FOR_EACH(rt, cr, &cls) {
uint8_t plen;
if (rt->priority == rt->plen) {
ds_put_format(&ds, "Cached: ");
} else {
ds_put_format(&ds, "User: ");
}
ds_put_format(&ds, IP_FMT"/%"PRIu16" dev %s",
IP_ARGS(rt->nw_addr), rt->plen,
rt->output_bridge);
if (rt->gw) {
ds_put_format(&ds, " GW "IP_FMT, IP_ARGS(rt->gw));
print_ipv6_mapped(&ds, &rt->nw_addr);
plen = rt->plen;
if (IN6_IS_ADDR_V4MAPPED(&rt->nw_addr)) {
plen -= 96;
}
ds_put_format(&ds, "/%"PRIu16" dev %s", plen, rt->output_bridge);
if (ipv6_addr_is_set(&rt->gw)) {
ds_put_format(&ds, " GW ");
print_ipv6_mapped(&ds, &rt->gw);
}
ds_put_format(&ds, "\n");
}
@@ -281,25 +339,27 @@ ovs_router_lookup_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
const char *argv[], void *aux OVS_UNUSED)
{
ovs_be32 ip;
struct in6_addr ip6;
unsigned int plen;
char iface[IFNAMSIZ];
struct in6_addr gw;
if (scan_ipv4_route(argv[1], &ip, &plen) && plen == 32) {
char iface[IFNAMSIZ];
ovs_be32 gw;
if (ovs_router_lookup(ip, iface, &gw)) {
struct ds ds = DS_EMPTY_INITIALIZER;
ds_put_format(&ds, "gateway " IP_FMT "\n", IP_ARGS(gw));
ds_put_format(&ds, "dev %s\n", iface);
unixctl_command_reply(conn, ds_cstr(&ds));
ds_destroy(&ds);
} else {
unixctl_command_reply(conn, "Not found");
}
} else {
in6_addr_set_mapped_ipv4(&ip6, ip);
} else if (!(scan_ipv6_route(argv[1], &ip6, &plen) && plen == 128)) {
unixctl_command_reply(conn, "Invalid parameters");
}
if (ovs_router_lookup(&ip6, iface, &gw)) {
struct ds ds = DS_EMPTY_INITIALIZER;
ds_put_format(&ds, "gateway ");
print_ipv6_mapped(&ds, &ip6);
ds_put_format(&ds, "\ndev %s\n", iface);
unixctl_command_reply(conn, ds_cstr(&ds));
ds_destroy(&ds);
} else {
unixctl_command_reply(conn, "Not found");
}
}
void
@@ -324,11 +384,11 @@ void
ovs_router_init(void)
{
classifier_init(&cls, NULL);
unixctl_command_register("ovs/route/add", "ipv4_addr/prefix_len out_br_name gw", 2, 3,
unixctl_command_register("ovs/route/add", "ip_addr/prefix_len out_br_name gw", 2, 3,
ovs_router_add, NULL);
unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
unixctl_command_register("ovs/route/del", "ipv4_addr/prefix_len", 1, 1, ovs_router_del,
unixctl_command_register("ovs/route/del", "ip_addr/prefix_len", 1, 1, ovs_router_del,
NULL);
unixctl_command_register("ovs/route/lookup", "ipv4_addr", 1, 1,
unixctl_command_register("ovs/route/lookup", "ip_addr", 1, 1,
ovs_router_lookup_cmd, NULL);
}

View File

@@ -23,10 +23,12 @@
extern "C" {
#endif
bool ovs_router_lookup(ovs_be32 ip_dst, char out_dev[], ovs_be32 *gw);
bool ovs_router_lookup(const struct in6_addr *ip_dst, char out_dev[],
struct in6_addr *gw);
bool ovs_router_lookup4(ovs_be32 ip_dst, char out_dev[], ovs_be32 *gw);
void ovs_router_init(void);
void ovs_router_insert(ovs_be32 ip_dst, uint8_t plen,
const char output_bridge[], ovs_be32 gw);
void ovs_router_insert(const struct in6_addr *ip_dst, uint8_t plen,
const char output_bridge[], const struct in6_addr *gw);
void ovs_router_flush(void);
#ifdef __cplusplus
}

View File

@@ -30,6 +30,7 @@
#include "netlink-socket.h"
#include "ofpbuf.h"
#include "ovs-router.h"
#include "packets.h"
#include "rtnetlink.h"
#include "openvswitch/vlog.h"
@@ -40,8 +41,8 @@ struct route_data {
unsigned char rtm_dst_len;
/* Extracted from Netlink attributes. */
ovs_be32 rta_dst; /* 0 if missing. */
ovs_be32 rta_gw;
struct in6_addr rta_dst; /* 0 if missing. */
struct in6_addr rta_gw;
char ifname[IFNAMSIZ]; /* Interface name. */
};
@@ -61,8 +62,10 @@ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
static uint64_t rt_change_seq;
static struct nln *nln = NULL;
static struct nln *nln6 = NULL;
static struct route_table_msg rtmsg;
static struct nln_notifier *route_notifier = NULL;
static struct nln_notifier *route6_notifier = NULL;
static struct nln_notifier *name_notifier = NULL;
static bool route_table_valid = false;
@@ -90,15 +93,22 @@ route_table_init(void)
{
ovs_mutex_lock(&route_table_mutex);
ovs_assert(!nln);
ovs_assert(!nln6);
ovs_assert(!route_notifier);
ovs_assert(!route6_notifier);
ovs_router_init();
nln = nln_create(NETLINK_ROUTE, RTNLGRP_IPV4_ROUTE,
(nln_parse_func *) route_table_parse, &rtmsg);
nln6 = nln_create(NETLINK_ROUTE, RTNLGRP_IPV6_ROUTE,
(nln_parse_func *) route_table_parse, &rtmsg);
route_notifier =
nln_notifier_create(nln, (nln_notify_func *) route_table_change,
NULL);
route6_notifier =
nln_notifier_create(nln6, (nln_notify_func *) route_table_change,
NULL);
route_table_reset();
name_table_init();
@@ -112,9 +122,14 @@ route_table_run(void)
OVS_EXCLUDED(route_table_mutex)
{
ovs_mutex_lock(&route_table_mutex);
if (nln) {
if (nln || nln6) {
rtnetlink_run();
nln_run(nln);
if (nln) {
nln_run(nln);
}
if (nln6) {
nln_run(nln6);
}
if (!route_table_valid) {
route_table_reset();
@@ -129,9 +144,14 @@ route_table_wait(void)
OVS_EXCLUDED(route_table_mutex)
{
ovs_mutex_lock(&route_table_mutex);
if (nln) {
if (nln || nln6) {
rtnetlink_wait();
nln_wait(nln);
if (nln) {
nln_wait(nln);
}
if (nln6) {
nln_wait(nln6);
}
}
ovs_mutex_unlock(&route_table_mutex);
}
@@ -153,7 +173,7 @@ route_table_reset(void)
nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
rtmsg->rtgen_family = AF_INET;
rtmsg->rtgen_family = AF_UNSPEC;
nl_dump_start(&dump, NETLINK_ROUTE, &request);
ofpbuf_uninit(&request);
@@ -171,11 +191,10 @@ route_table_reset(void)
return nl_dump_done(&dump);
}
static bool
route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
{
bool parsed;
bool parsed, ipv4 = false;
static const struct nl_policy policy[] = {
[RTA_DST] = { .type = NL_A_U32, .optional = true },
@@ -183,23 +202,34 @@ route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
[RTA_GATEWAY] = { .type = NL_A_U32, .optional = true },
};
struct nlattr *attrs[ARRAY_SIZE(policy)];
static const struct nl_policy policy6[] = {
[RTA_DST] = { .type = NL_A_IPV6, .optional = true },
[RTA_OIF] = { .type = NL_A_U32, .optional = true },
[RTA_GATEWAY] = { .type = NL_A_IPV6, .optional = true },
};
parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
policy, attrs, ARRAY_SIZE(policy));
struct nlattr *attrs[ARRAY_SIZE(policy)];
const struct rtmsg *rtm;
rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
if (rtm->rtm_family == AF_INET) {
parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
policy, attrs, ARRAY_SIZE(policy));
ipv4 = true;
} else if (rtm->rtm_family == AF_INET6) {
parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
policy6, attrs, ARRAY_SIZE(policy6));
} else {
VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
return false;
}
if (parsed) {
const struct rtmsg *rtm;
const struct nlmsghdr *nlmsg;
int rta_oif; /* Output interface index. */
nlmsg = buf->data;
rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
if (rtm->rtm_family != AF_INET) {
VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
return false;
}
memset(change, 0, sizeof *change);
change->relevant = true;
@@ -213,22 +243,38 @@ route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
change->relevant = false;
}
change->nlmsg_type = nlmsg->nlmsg_type;
change->rd.rtm_dst_len = rtm->rtm_dst_len;
rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
change->rd.rtm_dst_len = rtm->rtm_dst_len + (ipv4 ? 96 : 0);
if (attrs[RTA_OIF]) {
rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
if (!if_indextoname(rta_oif, change->rd.ifname)) {
int error = errno;
if (!if_indextoname(rta_oif, change->rd.ifname)) {
int error = errno;
VLOG_DBG_RL(&rl, "Could not find interface name[%u]: %s",
rta_oif, ovs_strerror(error));
return false;
VLOG_DBG_RL(&rl, "Could not find interface name[%u]: %s",
rta_oif, ovs_strerror(error));
return false;
}
}
if (attrs[RTA_DST]) {
change->rd.rta_dst = nl_attr_get_be32(attrs[RTA_DST]);
if (ipv4) {
ovs_be32 dst;
dst = nl_attr_get_be32(attrs[RTA_DST]);
in6_addr_set_mapped_ipv4(&change->rd.rta_dst, dst);
} else {
change->rd.rta_dst = nl_attr_get_in6_addr(attrs[RTA_DST]);
}
} else if (ipv4) {
in6_addr_set_mapped_ipv4(&change->rd.rta_dst, 0);
}
if (attrs[RTA_GATEWAY]) {
change->rd.rta_gw = nl_attr_get_be32(attrs[RTA_GATEWAY]);
if (ipv4) {
ovs_be32 gw;
gw = nl_attr_get_be32(attrs[RTA_GATEWAY]);
in6_addr_set_mapped_ipv4(&change->rd.rta_gw, gw);
} else {
change->rd.rta_gw = nl_attr_get_in6_addr(attrs[RTA_GATEWAY]);
}
}
@@ -252,8 +298,8 @@ route_table_handle_msg(const struct route_table_msg *change)
if (change->relevant && change->nlmsg_type == RTM_NEWROUTE) {
const struct route_data *rd = &change->rd;
ovs_router_insert(rd->rta_dst, rd->rtm_dst_len,
rd->ifname, rd->rta_gw);
ovs_router_insert(&rd->rta_dst, rd->rtm_dst_len,
rd->ifname, &rd->rta_gw);
}
}

View File

@@ -455,7 +455,7 @@ sflow_choose_agent_address(const char *agent_device,
&& sa.ss.ss_family == AF_INET) {
ovs_be32 gw;
if (ovs_router_lookup(sa.sin.sin_addr.s_addr, name, &gw)
if (ovs_router_lookup4(sa.sin.sin_addr.s_addr, name, &gw)
&& !netdev_get_in4_by_name(name, &in4)) {
goto success;
}

View File

@@ -2661,7 +2661,7 @@ tnl_route_lookup_flow(const struct flow *oflow,
struct xlate_cfg *xcfg;
ovs_be32 gw;
if (!ovs_router_lookup(oflow->tunnel.ip_dst, out_dev, &gw)) {
if (!ovs_router_lookup4(oflow->tunnel.ip_dst, out_dev, &gw)) {
return -ENOENT;
}