Skip to content

Commit

Permalink
OVN: add the possibility to configure a static IPv4/IPv6 address and …
Browse files Browse the repository at this point in the history
…dynamic MAC

Add the possibility to configure a static IPv4 and/or IPv6 address
and get MAC address dynamically allocated. This can be done using the
following commands:

$ovn-nbctl ls-add sw0
$ovn-nbctl set Logical-Switch sw0 other_config:subnet=192.168.0.0/24
$ovn-nbctl set Logical-switch sw0 other_config:ipv6_prefix=2001::0
$ovn-nbctl lsp-add sw0 lsp0 -- lsp-set-addresses lsp0 "dynamic 192.168.0.1 2001::1"

Acked-by: Mark Michelson <[email protected]>
Signed-off-by: Lorenzo Bianconi <[email protected]>
Signed-off-by: Ben Pfaff <[email protected]>
  • Loading branch information
LorenzoBianconi authored and blp committed Apr 16, 2019
1 parent c61a85f commit de0c1c3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 23 deletions.
6 changes: 5 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ Post-v2.11.0
See section 4.1.15 of ovsdb-server(7) for details of related OVSDB
protocol extension.
- OVN:
* Select IPAM mac_prefix in a random manner if not provided by the user
* IPAM/MACAM:
- select IPAM mac_prefix in a random manner if not provided by the user
- add the capability to specify a static IPv4 and/or IPv6 address and
get the L2 one allocated dynamically using the following syntax:
ovn-nbctl lsp-set-addresses <port> "dynamic <IPv4 addr> <IPv6 addr>"
* Added the HA chassis group support.
* Added 'external' logical port support.
- New QoS type "linux-netem" on Linux.
Expand Down
6 changes: 6 additions & 0 deletions ovn/lib/ovn-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ add_ipv6_netaddr(struct lport_addresses *laddrs, struct in6_addr addr,
bool
is_dynamic_lsp_address(const char *address)
{
char ipv6_s[IPV6_SCAN_LEN + 1];
struct eth_addr ea;
ovs_be32 ip;
int n;
return (!strcmp(address, "dynamic")
|| (ovs_scan(address, "dynamic "IP_SCAN_FMT"%n",
IP_SCAN_ARGS(&ip), &n)
&& address[n] == '\0')
|| (ovs_scan(address, "dynamic "IP_SCAN_FMT" "IPV6_SCAN_FMT"%n",
IP_SCAN_ARGS(&ip), ipv6_s, &n)
&& address[n] == '\0')
|| (ovs_scan(address, "dynamic "IPV6_SCAN_FMT"%n",
ipv6_s, &n) && address[n] == '\0')
|| (ovs_scan(address, ETH_ADDR_SCAN_FMT" dynamic%n",
ETH_ADDR_SCAN_ARGS(ea), &n) && address[n] == '\0'));
}
Expand Down
87 changes: 65 additions & 22 deletions ovn/northd/ovn-northd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ struct dynamic_address_update {
struct lport_addresses current_addresses;
struct eth_addr static_mac;
ovs_be32 static_ip;
struct in6_addr static_ipv6;
enum dynamic_update_type mac;
enum dynamic_update_type ipv4;
enum dynamic_update_type ipv6;
Expand Down Expand Up @@ -1187,13 +1188,16 @@ dynamic_ip4_changed(const char *lsp_addrs,
*/
return DYNAMIC;
} else {
char ipv6_s[IPV6_SCAN_LEN + 1];
ovs_be32 new_ip;
int n = 0;

if (ovs_scan(lsp_addrs, "dynamic "IP_SCAN_FMT"%n",
if ((ovs_scan(lsp_addrs, "dynamic "IP_SCAN_FMT"%n",
IP_SCAN_ARGS(&new_ip), &n)
&& lsp_addrs[n] == '\0') {

&& lsp_addrs[n] == '\0') ||
(ovs_scan(lsp_addrs, "dynamic "IP_SCAN_FMT" "IPV6_SCAN_FMT"%n",
IP_SCAN_ARGS(&new_ip), ipv6_s, &n)
&& lsp_addrs[n] == '\0')) {
index = ntohl(new_ip) - ipam->start_ipv4;
if (ntohl(new_ip) < ipam->start_ipv4 ||
index > ipam->total_ipv4s ||
Expand All @@ -1211,9 +1215,11 @@ dynamic_ip4_changed(const char *lsp_addrs,
}

static enum dynamic_update_type
dynamic_ip6_changed(struct dynamic_address_update *update)
dynamic_ip6_changed(const char *lsp_addrs,
struct dynamic_address_update *update)
{
bool dynamic_ip6 = update->op->od->ipam_info.ipv6_prefix_set;
struct eth_addr ea;

if (!dynamic_ip6) {
if (update->current_addresses.n_ipv6_addrs) {
Expand All @@ -1225,22 +1231,43 @@ dynamic_ip6_changed(struct dynamic_address_update *update)
}
}

if (update->mac != NONE) {
/* IPv6 address is based on MAC, so if MAC has been updated,
* then we have to update IPv6 address too.
*/
return DYNAMIC;
}

if (!update->current_addresses.n_ipv6_addrs) {
if (!update->current_addresses.n_ipv6_addrs ||
ovs_scan(lsp_addrs, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(ea))) {
/* IPv6 was previously static but now is dynamic */
return DYNAMIC;
}

struct in6_addr masked = ipv6_addr_bitand(
&update->current_addresses.ipv6_addrs[0].addr,
&update->op->od->ipam_info.ipv6_prefix);
if (!IN6_ARE_ADDR_EQUAL(&masked, &update->op->od->ipam_info.ipv6_prefix)) {
const struct lport_addresses *cur_addresses;
char ipv6_s[IPV6_SCAN_LEN + 1];
ovs_be32 new_ip;
int n = 0;

if ((ovs_scan(lsp_addrs, "dynamic "IPV6_SCAN_FMT"%n",
ipv6_s, &n) && lsp_addrs[n] == '\0') ||
(ovs_scan(lsp_addrs, "dynamic "IP_SCAN_FMT" "IPV6_SCAN_FMT"%n",
IP_SCAN_ARGS(&new_ip), ipv6_s, &n)
&& lsp_addrs[n] == '\0')) {
struct in6_addr ipv6;

if (!ipv6_parse(ipv6_s, &ipv6)) {
return DYNAMIC;
}

struct in6_addr masked = ipv6_addr_bitand(&ipv6,
&update->op->od->ipam_info.ipv6_prefix);
if (!IN6_ARE_ADDR_EQUAL(&masked,
&update->op->od->ipam_info.ipv6_prefix)) {
return DYNAMIC;
}

cur_addresses = &update->current_addresses;

if (!IN6_ARE_ADDR_EQUAL(&cur_addresses->ipv6_addrs[0].addr,
&ipv6)) {
update->static_ipv6 = ipv6;
return STATIC;
}
} else if (update->mac != NONE) {
return DYNAMIC;
}

Expand All @@ -1258,7 +1285,7 @@ dynamic_addresses_check_for_updates(const char *lsp_addrs,
{
update->mac = dynamic_mac_changed(lsp_addrs, update);
update->ipv4 = dynamic_ip4_changed(lsp_addrs, update);
update->ipv6 = dynamic_ip6_changed(update);
update->ipv6 = dynamic_ip6_changed(lsp_addrs, update);
if (update->mac == NONE &&
update->ipv4 == NONE &&
update->ipv6 == NONE) {
Expand Down Expand Up @@ -1299,6 +1326,8 @@ static void
set_dynamic_updates(const char *addrspec,
struct dynamic_address_update *update)
{
bool has_ipv4 = false, has_ipv6 = false;
char ipv6_s[IPV6_SCAN_LEN + 1];
struct eth_addr mac;
ovs_be32 ip;
int n = 0;
Expand All @@ -1311,17 +1340,30 @@ set_dynamic_updates(const char *addrspec,
update->mac = DYNAMIC;
}

if (ovs_scan(addrspec, "dynamic "IP_SCAN_FMT"%n",
IP_SCAN_ARGS(&ip), &n)
&& addrspec[n] == '\0') {
if ((ovs_scan(addrspec, "dynamic "IP_SCAN_FMT"%n",
IP_SCAN_ARGS(&ip), &n) && addrspec[n] == '\0')) {
has_ipv4 = true;
} else if ((ovs_scan(addrspec, "dynamic "IPV6_SCAN_FMT"%n",
ipv6_s, &n) && addrspec[n] == '\0')) {
has_ipv6 = true;
} else if ((ovs_scan(addrspec, "dynamic "IP_SCAN_FMT" "IPV6_SCAN_FMT"%n",
IP_SCAN_ARGS(&ip), ipv6_s, &n)
&& addrspec[n] == '\0')) {
has_ipv4 = has_ipv6 = true;
}

if (has_ipv4) {
update->ipv4 = STATIC;
update->static_ip = ip;
} else if (update->op->od->ipam_info.allocated_ipv4s) {
update->ipv4 = DYNAMIC;
} else {
update->ipv4 = NONE;
}
if (update->op->od->ipam_info.ipv6_prefix_set) {

if (has_ipv6 && ipv6_parse(ipv6_s, &update->static_ipv6)) {
update->ipv6 = STATIC;
} else if (update->op->od->ipam_info.ipv6_prefix_set) {
update->ipv6 = DYNAMIC;
} else {
update->ipv6 = NONE;
Expand Down Expand Up @@ -1372,7 +1414,8 @@ update_dynamic_addresses(struct dynamic_address_update *update)
case REMOVE:
break;
case STATIC:
OVS_NOT_REACHED();
ip6 = update->static_ipv6;
break;
case DYNAMIC:
in6_generate_eui64(mac, &update->od->ipam_info.ipv6_prefix, &ip6);
break;
Expand Down
24 changes: 24 additions & 0 deletions ovn/ovn-nb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,30 @@
</dl>
</dd>

<dt><code>Keyword "dynamic" followed by an IPv4/IPv6 address</code></dt>
<dd>

<p>
The keyword <code>dynamic</code> followed by an IPv4/IPv6
address indicates that <code>ovn-northd</code> should choose
a dynamic ethernet address and use the provided IPv4/IPv6 address
as network address.
</p>

<p>
Examples:
</p>

<dl>
<dt><code>dynamic 192.168.0.1 2001::1</code></dt>
<dd>
This indicates that <code>ovn-northd</code> should allocate
a unique MAC address and use the provided IPv4/IPv6 address
for the related port
</dd>
</dl>
</dd>

<dt><code>router</code></dt>
<dd>
<p>
Expand Down
2 changes: 2 additions & 0 deletions ovn/utilities/ovn-nbctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1570,13 +1570,15 @@ nbctl_lsp_set_addresses(struct ctl_context *ctx)

int i;
for (i = 2; i < ctx->argc; i++) {
char ipv6_s[IPV6_SCAN_LEN + 1];
struct eth_addr ea;
ovs_be32 ip;

if (strcmp(ctx->argv[i], "unknown") && strcmp(ctx->argv[i], "dynamic")
&& strcmp(ctx->argv[i], "router")
&& !ovs_scan(ctx->argv[i], ETH_ADDR_SCAN_FMT,
ETH_ADDR_SCAN_ARGS(ea))
&& !ovs_scan(ctx->argv[i], "dynamic "IPV6_SCAN_FMT, ipv6_s)
&& !ovs_scan(ctx->argv[i], "dynamic "IP_SCAN_FMT,
IP_SCAN_ARGS(&ip))) {
ctl_error(ctx, "%s: Invalid address format. See ovn-nb(5). "
Expand Down
14 changes: 14 additions & 0 deletions tests/ovn.at
Original file line number Diff line number Diff line change
Expand Up @@ -6348,6 +6348,20 @@ mac_prefix=$(ovn-nbctl --wait=sb get NB_Global . options:mac_prefix | tr -d \")
port_addr=$(ovn-nbctl get Logical-Switch-Port p91 dynamic_addresses | tr -d \")
AT_CHECK([test "$port_addr" = "${mac_prefix}:00:00:09"], [0], [])

ovn-nbctl --wait=hv set NB_Global . options:mac_prefix="00:11:22"
ovn-nbctl ls-add sw10
ovn-nbctl --wait=sb set Logical-Switch sw10 other_config:ipv6_prefix="ae01::"
ovn-nbctl --wait=sb lsp-add sw10 p101 -- lsp-set-addresses p101 "dynamic ae01::1"
AT_CHECK([ovn-nbctl get Logical-Switch-Port p101 dynamic_addresses], [0],
["00:11:22:00:00:0a ae01::1"
])

ovn-nbctl --wait=sb set Logical-Switch sw10 other_config:subnet=192.168.110.0/24
ovn-nbctl --wait=sb lsp-add sw10 p102 -- lsp-set-addresses p102 "dynamic 192.168.110.10 ae01::2"
AT_CHECK([ovn-nbctl get Logical-Switch-Port p102 dynamic_addresses], [0],
["00:11:22:a8:6e:0b 192.168.110.10 ae01::2"
])

as ovn-sb
OVS_APP_EXIT_AND_WAIT([ovsdb-server])

Expand Down

0 comments on commit de0c1c3

Please sign in to comment.