[PATCH v5] tools/xl: fix autoballoon regex

Dmitry Isaykin posted 1 patch 3 years, 2 months ago
Test gitlab-ci passed
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/54c1bcb40afa8a6f36ab4bec8c91523a535c911b.1633090722.git.isaikin-dmitry@yandex.ru
tools/xl/xl.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
[PATCH v5] tools/xl: fix autoballoon regex
Posted by Dmitry Isaykin 3 years, 2 months ago
This regex is used for auto-balloon mode detection based on Xen command line.

The case of specifying a negative size was handled incorrectly.
From misc/xen-command-line documentation:

    dom0_mem (x86)
    = List of ( min:<sz> | max:<sz> | <sz> )

    If a size is positive, it represents an absolute value.
    If a size is negative, it is subtracted from the total available memory.

Also add support for [tT] granularity suffix.
Also add support for memory fractions (i.e. '50%' or '1G+25%').

Signed-off-by: Dmitry Isaykin <isaikin-dmitry@yandex.ru>
Reviewed-by: Anthony PERARD <anthony.perard@citrix.com>
---
Changes in v5:
- add Anthony's reviewed-by

Changes in v4:
- improve regex after code review

Changes in v3:
- add support for [tT] granularity suffix
- add support for memory fractions

Changes in v2:
- add missing Signed-off-by tag

NB! New autoballon regex pattern is good enough, but not fully correct.
See test below.

```
import pytest
import re

size_pattern = r"-?[0-9]+[bBkKmMgGtT]?"
pattern = r"(^| )dom0_mem=((|min:|max:)({size}|({size}\+)?[0-9]{{1,2}}%),?)+($| )".format(size=size_pattern)

@pytest.mark.parametrize('cmdline', [
    'dom0_mem=1',
    ' dom0_mem=1',
    'dom0_mem=1G',
    'dom0_mem=25%',
    'dom0_mem=1g+25% ',
    ' dom0_mem=1G+10%,max:100T+50%',
    'dom0_mem=-1B',
    'dom0_mem=-10240M+5%,min:512M ',
    ' dom0_mem=min:-2048m,max:-2048M ',
])
def test_autoballoon_regex_match(cmdline):
    assert re.match(pattern, cmdline)

@pytest.mark.parametrize('cmdline', [
    'dom0_mem = 1',
    'dom0_mem= ',
    'dom0_mem',
    'DOM0_mem=10G',
    'dom0_mem=-10%', # does not pass
    ' dom0_mem=-10%+10G ',
    'dom0_mem=1G,', # does not pass
    'dom0_mem=1G,max:25%,', #does not pass
])
def test_autoballoon_regex_dont_match(cmdline):
    assert not re.match(pattern, cmdline)
```
---
 tools/xl/xl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/xl/xl.c b/tools/xl/xl.c
index 4107d10fd4..f422f9fed5 100644
--- a/tools/xl/xl.c
+++ b/tools/xl/xl.c
@@ -80,14 +80,20 @@ static int auto_autoballoon(void)
     if (!info)
         return 1; /* default to on */
 
+#define SIZE_PATTERN "-?[0-9]+[bBkKmMgGtT]?"
+
     ret = regcomp(&regex,
-                  "(^| )dom0_mem=((|min:|max:)[0-9]+[bBkKmMgG]?,?)+($| )",
+                  "(^| )dom0_mem=((|min:|max:)(" SIZE_PATTERN "|(" SIZE_PATTERN "\\+)?[0-9]{1,2}%),?)+($| )",
                   REG_NOSUB | REG_EXTENDED);
+
+#undef SIZE_PATTERN
+
     if (ret)
         return 1;
 
     ret = regexec(&regex, info->commandline, 0, NULL, 0);
     regfree(&regex);
+
     return ret == REG_NOMATCH;
 }
 
-- 
2.33.0