diff mbox series

[v4] tools/xl: fix autoballoon regex

Message ID 57f69075b77651c64b7dc6d86acd03fe03b4a18f.1632907890.git.isaikin-dmitry@yandex.ru (mailing list archive)
State Superseded
Headers show
Series [v4] tools/xl: fix autoballoon regex | expand

Commit Message

Dmitry Isaykin Sept. 29, 2021, 12:44 p.m. UTC
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>
---
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 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Anthony PERARD Oct. 1, 2021, 11:36 a.m. UTC | #1
On Wed, Sep 29, 2021 at 03:44:33PM +0300, Dmitry Isaykin wrote:
> 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>
> ---

> +#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);

Reviewed-by: Anthony PERARD <anthony.perard@citrix.com>

Thanks,
diff mbox series

Patch

diff --git a/tools/xl/xl.c b/tools/xl/xl.c
index 4107d10fd4..58af7936fa 100644
--- a/tools/xl/xl.c
+++ b/tools/xl/xl.c
@@ -80,9 +80,14 @@  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;