aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/libxlu_vif.c
blob: 0665e624dc178a6ca8058e04a7baacaf1475bd37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "libxl_osdeps.h" /* must come before any other headers */
#include "libxlu_internal.h"

static const char *vif_bytes_per_sec_re = "^[0-9]+[GMK]?[Bb]/s$";
static const char *vif_internal_usec_re = "^[0-9]+[mu]?s?$";

static void xlu__vif_err(XLU_Config *cfg, const char *msg, const char *rate) {
    fprintf(cfg->report,
            "%s: config parsing error in vif: %s in `%s'\n",
            cfg->config_source, msg, rate);
}

static int vif_parse_rate_bytes_per_sec(XLU_Config *cfg, const char *bytes,
                                        uint64_t *bytes_per_sec)
{
    regex_t rec;
    uint64_t tmp = 0;
    const char *p;
    int rc = 0;

    regcomp(&rec, vif_bytes_per_sec_re, REG_EXTENDED|REG_NOSUB);
    if (regexec(&rec, bytes, 0, NULL, 0)) {
        xlu__vif_err(cfg, "invalid rate", bytes);
        rc = EINVAL;
        goto out;
    }

    p = bytes;
    tmp = strtoull(p, (char**)&p, 0);
    if (tmp == 0 || tmp > UINT32_MAX || errno == ERANGE) {
        xlu__vif_err(cfg, "rate overflow", bytes);
        rc = EOVERFLOW;
        goto out;
    }

    if (*p == 'G')
       tmp *= 1000 * 1000 * 1000;
    else if (*p == 'M')
       tmp *= 1000 * 1000;
    else if (*p == 'K')
       tmp *= 1000;
    if (*p == 'b' || *(p+1) == 'b')
       tmp /= 8;

    *bytes_per_sec = tmp;

out:
    regfree(&rec);
    return rc;
}

static int vif_parse_rate_interval_usecs(XLU_Config *cfg, const char *interval,
                                         uint32_t *interval_usecs)
{
    regex_t rec;
    uint64_t tmp = 0;
    const char *p;
    int rc = 0;

    regcomp(&rec, vif_internal_usec_re, REG_EXTENDED|REG_NOSUB);
    if (regexec(&rec, interval, 0, NULL, 0)) {
        xlu__vif_err(cfg, "invalid replenishment interval", interval);
        rc = EINVAL;
        goto out;
    }

    p = interval;
    tmp = strtoull(p, (char**)&p, 0);
    if (tmp == 0 || tmp > UINT32_MAX || errno == ERANGE) {
        xlu__vif_err(cfg, "replenishment interval overflow", interval);
        rc = EOVERFLOW;
        goto out;
    }

    if (*p == 's' || *p == '\0')
        tmp *= 1000 * 1000;
    else if (*p == 'm')
        tmp *= 1000;

    if (tmp > UINT32_MAX) {
        xlu__vif_err(cfg, "replenishment interval overflow", interval);
        rc = EOVERFLOW;
        goto out;
    }

    *interval_usecs = (uint32_t) tmp;

out:
    regfree(&rec);
    return rc;
}

int xlu_vif_parse_rate(XLU_Config *cfg, const char *rate, libxl_device_nic *nic)
{
    uint64_t bytes_per_sec = 0;
    uint64_t bytes_per_interval = 0;
    uint32_t interval_usecs = 50000UL; /* Default to 50ms */
    char *p, *tmprate;
    int rc = 0;

    tmprate = strdup(rate);
    if (tmprate == NULL) {
        rc = ENOMEM;
        goto out;
    }

    p = strchr(tmprate, '@');
    if (p != NULL)
        *p++ = 0;

    if (!strcmp(tmprate,"")) {
        xlu__vif_err(cfg, "no rate specified", rate);
        rc = EINVAL;
        goto out;
    }

    rc = vif_parse_rate_bytes_per_sec(cfg, tmprate, &bytes_per_sec);
    if (rc) goto out;

    if (p != NULL) {
        rc = vif_parse_rate_interval_usecs(cfg, p, &interval_usecs);
        if (rc) goto out;
    }

    if (interval_usecs != 0 && (bytes_per_sec > (UINT64_MAX / interval_usecs))) {
        xlu__vif_err(cfg, "rate overflow", rate);
        rc = EOVERFLOW;
        goto out;
    }

    bytes_per_interval =
        (((uint64_t) bytes_per_sec * (uint64_t) interval_usecs) / 1000000UL);

    nic->rate_interval_usecs = interval_usecs;
    nic->rate_bytes_per_interval = bytes_per_interval;

out:
    free(tmprate);
    return rc;
}

/*
 * Local variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */