[ovs-dev,v3] OVN: add static IP support to IPAM

Message ID 290a6b8c4d8049ddaba9ced057436e8a2fd901b8.1546018243.git.lorenzo.bianconi@redhat.com
State New
Headers show
Series
  • [ovs-dev,v3] OVN: add static IP support to IPAM
Related show

Commit Message

Lorenzo Bianconi Dec. 28, 2018, 5:38 p.m.
Add the capability to IPAM/MACAM framework to specify a static ip address
and get the L2 one allocated dynamically using the following syntax:

$ovn-nbctl lsp-set-addresses <port> "dynamic <IP>"

The static ip address needs to belong to the subnet configured for the
logical switch

Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
---
Changes since v2:
- add a NEWS item
- this patch is based on "OVN: add mac address only support to IPAM/MACAM"
  https://patchwork.ozlabs.org/patch/1019217/
Changes since v1:
- use syntax "dynamic <IP>" to configure the static ip address
---
 NEWS                      |  6 +++++-
 ovn/lib/ovn-util.c        |  8 ++++++++
 ovn/northd/ovn-northd.c   | 37 +++++++++++++++++++++++++++++++++----
 ovn/utilities/ovn-nbctl.c |  5 ++++-
 tests/ovn.at              | 12 ++++++++++++
 5 files changed, 62 insertions(+), 6 deletions(-)

Comments

0-day Robot Dec. 28, 2018, 6:06 p.m. | #1
Bleep bloop.  Greetings Lorenzo Bianconi, I am a robot and I have tried out your patch.
Thanks for your contribution.

I encountered some error that I wasn't expecting.  See the details below.


git-am:
Failed to merge in the changes.
Patch failed at 0001 OVN: add static IP support to IPAM
The copy of the patch that failed is found in:
   /var/lib/jenkins/jobs/upstream_build_from_pw/workspace/.git/rebase-apply/patch
When you have resolved this problem, run "git am --resolved".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".


Please check this out.  If you feel there has been an error, please email aconole@bytheb.org

Thanks,
0-day Robot
Ben Pfaff Dec. 28, 2018, 6:16 p.m. | #2
On Fri, Dec 28, 2018 at 06:38:47PM +0100, Lorenzo Bianconi wrote:
> Add the capability to IPAM/MACAM framework to specify a static ip address
> and get the L2 one allocated dynamically using the following syntax:
> 
> $ovn-nbctl lsp-set-addresses <port> "dynamic <IP>"
> 
> The static ip address needs to belong to the subnet configured for the
> logical switch
> 
> Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>

Thanks, applied to master.

Patch

diff --git a/NEWS b/NEWS
index 9de87a736..2de844f30 100644
--- a/NEWS
+++ b/NEWS
@@ -14,7 +14,11 @@  Post-v2.10.0
        version.
      * New support for IPSEC encrypted tunnels between hypervisors.
      * ovn-ctl: allow passing user:group ids to the OVN daemons.
-     * IPAM/MACAM: add the capability to dynamically assign just L2 addresses
+     * IPAM/MACAM:
+       - add the capability to dynamically assign just L2 addresses
+       - add the capability to specify a static ip address and get the L2 one
+         allocated dynamically using the following syntax:
+           ovn-nbctl lsp-set-addresses <port> "dynamic <IP>"
    - DPDK:
      * Add option for simple round-robin based Rxq to PMD assignment.
        It can be set with pmd-rxq-assign.
diff --git a/ovn/lib/ovn-util.c b/ovn/lib/ovn-util.c
index e9464e926..aa03919bb 100644
--- a/ovn/lib/ovn-util.c
+++ b/ovn/lib/ovn-util.c
@@ -72,13 +72,21 @@  add_ipv6_netaddr(struct lport_addresses *laddrs, struct in6_addr addr,
  *    "xx:xx:xx:xx:xx:xx dynamic":
  *        Use specified MAC address, but allocate an IP address
  *        dynamically.
+ *
+ *    "dynamic x.x.x.x":
+ *        Use specified IP address, but allocate a MAC address
+ *        dynamically.
  */
 bool
 is_dynamic_lsp_address(const char *address)
 {
     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, ETH_ADDR_SCAN_FMT" dynamic%n",
                          ETH_ADDR_SCAN_ARGS(ea), &n) && address[n] == '\0'));
 }
diff --git a/ovn/northd/ovn-northd.c b/ovn/northd/ovn-northd.c
index c0b56c4c2..3fd8a8757 100644
--- a/ovn/northd/ovn-northd.c
+++ b/ovn/northd/ovn-northd.c
@@ -1109,6 +1109,7 @@  struct dynamic_address_update {
 
     struct lport_addresses current_addresses;
     struct eth_addr static_mac;
+    ovs_be32 static_ip;
     enum dynamic_update_type mac;
     enum dynamic_update_type ipv4;
     enum dynamic_update_type ipv6;
@@ -1147,7 +1148,8 @@  dynamic_mac_changed(const char *lsp_addresses,
 }
 
 static enum dynamic_update_type
-dynamic_ip4_changed(struct dynamic_address_update *update)
+dynamic_ip4_changed(const char *lsp_addrs,
+                    struct dynamic_address_update *update)
 {
     const struct ipam_info *ipam = &update->op->od->ipam_info;
     const struct lport_addresses *cur_addresses = &update->current_addresses;
@@ -1180,6 +1182,25 @@  dynamic_ip4_changed(struct dynamic_address_update *update)
          */
         return DYNAMIC;
     } else {
+        ovs_be32 new_ip;
+        int n = 0;
+
+        if (ovs_scan(lsp_addrs, "dynamic "IP_SCAN_FMT"%n",
+                     IP_SCAN_ARGS(&new_ip), &n)
+            && lsp_addrs[n] == '\0') {
+
+            index = ntohl(new_ip) - ipam->start_ipv4;
+            if (ntohl(new_ip) < ipam->start_ipv4 ||
+                index > ipam->total_ipv4s ||
+                bitmap_is_set(ipam->allocated_ipv4s, index)) {
+                /* new static ip is not valid */
+                return DYNAMIC;
+            } else if (cur_addresses->ipv4_addrs[0].addr != new_ip) {
+                update->ipv4 = STATIC;
+                update->static_ip = new_ip;
+                return STATIC;
+            }
+        }
         return NONE;
     }
 }
@@ -1231,7 +1252,7 @@  dynamic_addresses_check_for_updates(const char *lsp_addrs,
                                     struct dynamic_address_update *update)
 {
     update->mac = dynamic_mac_changed(lsp_addrs, update);
-    update->ipv4 = dynamic_ip4_changed(update);
+    update->ipv4 = dynamic_ip4_changed(lsp_addrs, update);
     update->ipv6 = dynamic_ip6_changed(update);
     if (update->mac == NONE &&
         update->ipv4 == NONE &&
@@ -1274,6 +1295,7 @@  set_dynamic_updates(const char *addrspec,
                     struct dynamic_address_update *update)
 {
     struct eth_addr mac;
+    ovs_be32 ip;
     int n = 0;
     if (ovs_scan(addrspec, ETH_ADDR_SCAN_FMT" dynamic%n",
                  ETH_ADDR_SCAN_ARGS(mac), &n)
@@ -1283,7 +1305,13 @@  set_dynamic_updates(const char *addrspec,
     } else {
         update->mac = DYNAMIC;
     }
-    if (update->op->od->ipam_info.allocated_ipv4s) {
+
+    if (ovs_scan(addrspec, "dynamic "IP_SCAN_FMT"%n",
+                 IP_SCAN_ARGS(&ip), &n)
+        && addrspec[n] == '\0') {
+        update->ipv4 = STATIC;
+        update->static_ip = ip;
+    } else if (update->op->od->ipam_info.allocated_ipv4s) {
         update->ipv4 = DYNAMIC;
     } else {
         update->ipv4 = NONE;
@@ -1308,7 +1336,8 @@  update_dynamic_addresses(struct dynamic_address_update *update)
     case REMOVE:
         break;
     case STATIC:
-        OVS_NOT_REACHED();
+        ip4 = update->static_ip;
+        break;
     case DYNAMIC:
         ip4 = htonl(ipam_get_unused_ip(update->od));
     }
diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
index 9d1b22089..2fa0b3389 100644
--- a/ovn/utilities/ovn-nbctl.c
+++ b/ovn/utilities/ovn-nbctl.c
@@ -1529,11 +1529,14 @@  nbctl_lsp_set_addresses(struct ctl_context *ctx)
     int i;
     for (i = 2; i < ctx->argc; i++) {
         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))) {
+                         ETH_ADDR_SCAN_ARGS(ea))
+            && !ovs_scan(ctx->argv[i], "dynamic "IP_SCAN_FMT,
+                         IP_SCAN_ARGS(&ip))) {
             ctl_error(ctx, "%s: Invalid address format. See ovn-nb(5). "
                       "Hint: An Ethernet address must be "
                       "listed before an IP address, together as a single "
diff --git a/tests/ovn.at b/tests/ovn.at
index 5490fcd9c..8bada3241 100644
--- a/tests/ovn.at
+++ b/tests/ovn.at
@@ -5724,6 +5724,18 @@  AT_CHECK([ovn-nbctl get Logical-Switch-Port p41 dynamic_addresses], [0],
          ["f0:00:00:00:10:2b 192.168.1.3"
 ])
 
+# Add static ip address
+ovn-nbctl --wait=sb lsp-set-addresses p41 "dynamic 192.168.1.100"
+ovn-nbctl list Logical-Switch-Port p41
+ovn-nbctl --wait=sb lsp-add sw5 p42 -- lsp-set-addresses p42 \
+"dynamic 192.168.1.101"
+AT_CHECK([ovn-nbctl get Logical-Switch-Port p41 dynamic_addresses], [0],
+         ["0a:00:00:a8:01:65 192.168.1.100"
+])
+AT_CHECK([ovn-nbctl get Logical-Switch-Port p42 dynamic_addresses], [0],
+         ["0a:00:00:a8:01:66 192.168.1.101"
+])
+
 # define a mac address prefix
 ovn-nbctl ls-add sw6
 ovn-nbctl --wait=hv set NB_Global . options:mac_prefix="00:11:22:33:44:55"