Commit d737ccbed3e62dd45d631cf69183de005144d05b

Authored by Sven Eckelmann
Committed by Antonio Quartulli
1 parent 9e728e8438

batman-adv: Add function to convert string to batadv throughput

The code to convert the throughput information from a string to the
batman-adv internal (100Kibit/s) representation is duplicated in
batadv_parse_gw_bandwidth. Move this functionality to its own function
batadv_parse_throughput to reduce the code complexity.

Signed-off-by: Sven Eckelmann <sven@open-mesh.com>
Signed-off-by: Marek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: Antonio Quartulli <a@unstable.cc>

Showing 1 changed file with 49 additions and 68 deletions Side-by-side Diff

net/batman-adv/gateway_common.c
... ... @@ -31,27 +31,23 @@
31 31 #include "packet.h"
32 32  
33 33 /**
34   - * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
35   - * and upload bandwidth information
  34 + * batadv_parse_throughput - parse supplied string buffer to extract throughput
  35 + * information
36 36 * @net_dev: the soft interface net device
37 37 * @buff: string buffer to parse
38   - * @down: pointer holding the returned download bandwidth information
39   - * @up: pointer holding the returned upload bandwidth information
  38 + * @description: text shown when throughput string cannot be parsed
  39 + * @throughput: pointer holding the returned throughput information
40 40 *
41 41 * Returns false on parse error and true otherwise.
42 42 */
43   -static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
44   - u32 *down, u32 *up)
  43 +static bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
  44 + const char *description, u32 *throughput)
45 45 {
46 46 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
47   - char *slash_ptr, *tmp_ptr;
48   - u64 ldown, lup;
  47 + u64 lthroughput;
  48 + char *tmp_ptr;
49 49 int ret;
50 50  
51   - slash_ptr = strchr(buff, '/');
52   - if (slash_ptr)
53   - *slash_ptr = 0;
54   -
55 51 if (strlen(buff) > 4) {
56 52 tmp_ptr = buff + strlen(buff) - 4;
57 53  
58 54  
59 55  
60 56  
61 57  
62 58  
63 59  
64 60  
65 61  
66 62  
67 63  
68 64  
69 65  
70 66  
71 67  
... ... @@ -63,90 +59,75 @@
63 59 *tmp_ptr = '\0';
64 60 }
65 61  
66   - ret = kstrtou64(buff, 10, &ldown);
  62 + ret = kstrtou64(buff, 10, &lthroughput);
67 63 if (ret) {
68 64 batadv_err(net_dev,
69   - "Download speed of gateway mode invalid: %s\n",
70   - buff);
  65 + "Invalid throughput speed for %s: %s\n",
  66 + description, buff);
71 67 return false;
72 68 }
73 69  
74 70 switch (bw_unit_type) {
75 71 case BATADV_BW_UNIT_MBIT:
76 72 /* prevent overflow */
77   - if (U64_MAX / 10 < ldown) {
  73 + if (U64_MAX / 10 < lthroughput) {
78 74 batadv_err(net_dev,
79   - "Download speed of gateway mode too large: %s\n",
80   - buff);
  75 + "Throughput speed for %s too large: %s\n",
  76 + description, buff);
81 77 return false;
82 78 }
83 79  
84   - ldown *= 10;
  80 + lthroughput *= 10;
85 81 break;
86 82 case BATADV_BW_UNIT_KBIT:
87 83 default:
88   - ldown = div_u64(ldown, 100);
  84 + lthroughput = div_u64(lthroughput, 100);
89 85 break;
90 86 }
91 87  
92   - if (U32_MAX < ldown) {
  88 + if (lthroughput > U32_MAX) {
93 89 batadv_err(net_dev,
94   - "Download speed of gateway mode too large: %s\n",
95   - buff);
  90 + "Throughput speed for %s too large: %s\n",
  91 + description, buff);
96 92 return false;
97 93 }
98 94  
99   - *down = ldown;
  95 + *throughput = lthroughput;
100 96  
101   - /* we also got some upload info */
102   - if (slash_ptr) {
103   - bw_unit_type = BATADV_BW_UNIT_KBIT;
  97 + return true;
  98 +}
104 99  
105   - if (strlen(slash_ptr + 1) > 4) {
106   - tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
  100 +/**
  101 + * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
  102 + * and upload bandwidth information
  103 + * @net_dev: the soft interface net device
  104 + * @buff: string buffer to parse
  105 + * @down: pointer holding the returned download bandwidth information
  106 + * @up: pointer holding the returned upload bandwidth information
  107 + *
  108 + * Return: false on parse error and true otherwise.
  109 + */
  110 +static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
  111 + u32 *down, u32 *up)
  112 +{
  113 + char *slash_ptr;
  114 + bool ret;
107 115  
108   - if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
109   - bw_unit_type = BATADV_BW_UNIT_MBIT;
  116 + slash_ptr = strchr(buff, '/');
  117 + if (slash_ptr)
  118 + *slash_ptr = 0;
110 119  
111   - if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
112   - (bw_unit_type == BATADV_BW_UNIT_MBIT))
113   - *tmp_ptr = '\0';
114   - }
  120 + ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
  121 + down);
  122 + if (!ret)
  123 + return false;
115 124  
116   - ret = kstrtou64(slash_ptr + 1, 10, &lup);
117   - if (ret) {
118   - batadv_err(net_dev,
119   - "Upload speed of gateway mode invalid: %s\n",
120   - slash_ptr + 1);
  125 + /* we also got some upload info */
  126 + if (slash_ptr) {
  127 + ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
  128 + "upload gateway speed", up);
  129 + if (!ret)
121 130 return false;
122   - }
123   -
124   - switch (bw_unit_type) {
125   - case BATADV_BW_UNIT_MBIT:
126   - /* prevent overflow */
127   - if (U64_MAX / 10 < lup) {
128   - batadv_err(net_dev,
129   - "Upload speed of gateway mode too large: %s\n",
130   - slash_ptr + 1);
131   - return false;
132   - }
133   -
134   - lup *= 10;
135   - break;
136   - case BATADV_BW_UNIT_KBIT:
137   - default:
138   - lup = div_u64(lup, 100);
139   - break;
140   - }
141   -
142   - if (U32_MAX < lup) {
143   - batadv_err(net_dev,
144   - "Upload speed of gateway mode too large: %s\n",
145   - slash_ptr + 1);
146   - return false;
147   - }
148   -
149   - *up = lup;
150 131 }
151 132  
152 133 return true;