From a5d6be32c71e9e9b5b591ac960e459eff4d98a5e Mon Sep 17 00:00:00 2001
From: Pete Birley <pete@port.direct>
Date: Tue, 18 Sep 2018 12:40:13 -0500
Subject: [PATCH] SRIOV: Limit number of vfs to n-1 created by default

This PS udpates the sriov init script to by default create the
max number of vfs supported by the card -1. Which works round
issues encoutered with many cards that prevents ther theroretical
max being attainable.

Change-Id: I01f8ce1f36b6053a5ef68119d87b67050ffe99d1
Signed-off-by: Pete Birley <pete@port.direct>
---
 .../templates/bin/_neutron-sriov-agent-init.sh.tpl | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/neutron/templates/bin/_neutron-sriov-agent-init.sh.tpl b/neutron/templates/bin/_neutron-sriov-agent-init.sh.tpl
index 131f3825a0..cf307c3fee 100644
--- a/neutron/templates/bin/_neutron-sriov-agent-init.sh.tpl
+++ b/neutron/templates/bin/_neutron-sriov-agent-init.sh.tpl
@@ -22,7 +22,19 @@ set -ex
 if [ "x{{ $sriov.num_vfs }}" != "x" ]; then
   echo "{{ $sriov.num_vfs }}" > /sys/class/net/{{ $sriov.device }}/device/sriov_numvfs
 else
-  NUM_VFS=$(cat /sys/class/net/{{ $sriov.device }}/device/sriov_totalvfs)
+  #NOTE(portdirect): Many NICs have difficulty creating more than n-1 over their
+  # claimed limit, by default err on the side of caution and account for this
+  # limitation.
+  TOT_NUM_VFS=$(cat /sys/class/net/{{ $sriov.device }}/device/sriov_totalvfs)
+  if [[ "$TOT_NUM_VFS" -le "0" ]]; then
+    NUM_VFS="$TOT_NUM_VFS"
+  else
+    if [[ "$((TOT_NUM_VFS - 1 ))" -le "1" ]]; then
+      NUM_VFS=1
+    else
+      NUM_VFS="$((TOT_NUM_VFS - 1 ))"
+    fi
+  fi
   echo "${NUM_VFS}" > /sys/class/net/{{ $sriov.device }}/device/sriov_numvfs
 fi
 {{- if $sriov.mtu }}