/src/openvswitch/lib/multipath.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2016, 2017, 2019 Nicira, Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at: |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include <config.h> |
18 | | |
19 | | #include "multipath.h" |
20 | | #include <sys/types.h> |
21 | | #include <netinet/in.h> |
22 | | #include <arpa/inet.h> |
23 | | #include <inttypes.h> |
24 | | #include "colors.h" |
25 | | #include "nx-match.h" |
26 | | #include "openflow/nicira-ext.h" |
27 | | #include "openvswitch/dynamic-string.h" |
28 | | #include "openvswitch/ofp-actions.h" |
29 | | #include "openvswitch/ofp-errors.h" |
30 | | #include "packets.h" |
31 | | #include "util.h" |
32 | | |
33 | | /* Checks that 'mp' is valid on flow. Returns 0 if it is valid, otherwise an |
34 | | * OFPERR_*. */ |
35 | | enum ofperr |
36 | | multipath_check(const struct ofpact_multipath *mp, |
37 | | const struct match *match) |
38 | 0 | { |
39 | 0 | return mf_check_dst(&mp->dst, match); |
40 | 0 | } |
41 | | |
42 | | /* multipath_execute(). */ |
43 | | |
44 | | static uint16_t multipath_algorithm(uint32_t hash, enum nx_mp_algorithm, |
45 | | unsigned int n_links, unsigned int arg); |
46 | | |
47 | | /* Executes 'mp' based on the current contents of 'flow', writing the results |
48 | | * back into 'flow'. Sets fields in 'wc' that were used to calculate |
49 | | * the result. */ |
50 | | void |
51 | | multipath_execute(const struct ofpact_multipath *mp, struct flow *flow, |
52 | | struct flow_wildcards *wc) |
53 | 0 | { |
54 | | /* Calculate value to store. */ |
55 | 0 | uint32_t hash = flow_hash_fields(flow, mp->fields, mp->basis); |
56 | 0 | uint16_t link = multipath_algorithm(hash, mp->algorithm, |
57 | 0 | mp->max_link + 1, mp->arg); |
58 | |
|
59 | 0 | flow_mask_hash_fields(flow, wc, mp->fields); |
60 | 0 | nxm_reg_load(&mp->dst, link, flow, wc); |
61 | 0 | } |
62 | | |
63 | | static uint16_t |
64 | | algorithm_hrw(uint32_t hash, unsigned int n_links) |
65 | 0 | { |
66 | 0 | uint32_t best_weight; |
67 | 0 | uint16_t best_link; |
68 | 0 | unsigned int link; |
69 | |
|
70 | 0 | best_link = 0; |
71 | 0 | best_weight = hash_2words(hash, 0); |
72 | 0 | for (link = 1; link < n_links; link++) { |
73 | 0 | uint32_t weight = hash_2words(hash, link); |
74 | 0 | if (weight > best_weight) { |
75 | 0 | best_link = link; |
76 | 0 | best_weight = weight; |
77 | 0 | } |
78 | 0 | } |
79 | 0 | return best_link; |
80 | 0 | } |
81 | | |
82 | | /* Works for 'x' in the range [1,65536], which is all we need. */ |
83 | | static unsigned int |
84 | | round_up_pow2(unsigned int x) |
85 | 0 | { |
86 | 0 | x--; |
87 | 0 | x |= x >> 1; |
88 | 0 | x |= x >> 2; |
89 | 0 | x |= x >> 4; |
90 | 0 | x |= x >> 8; |
91 | 0 | return x + 1; |
92 | 0 | } |
93 | | |
94 | | static uint16_t |
95 | | algorithm_iter_hash(uint32_t hash, unsigned int n_links, unsigned int modulo) |
96 | 0 | { |
97 | 0 | uint16_t link; |
98 | 0 | int i; |
99 | |
|
100 | 0 | if (modulo < n_links || modulo / 2 > n_links) { |
101 | 0 | modulo = round_up_pow2(n_links); |
102 | 0 | } |
103 | |
|
104 | 0 | i = 0; |
105 | 0 | do { |
106 | 0 | link = hash_2words(hash, i++) % modulo; |
107 | 0 | } while (link >= n_links); |
108 | |
|
109 | 0 | return link; |
110 | 0 | } |
111 | | |
112 | | static uint16_t |
113 | | multipath_algorithm(uint32_t hash, enum nx_mp_algorithm algorithm, |
114 | | unsigned int n_links, unsigned int arg) |
115 | 0 | { |
116 | 0 | switch (algorithm) { |
117 | 0 | case NX_MP_ALG_MODULO_N: |
118 | 0 | return hash % n_links; |
119 | | |
120 | 0 | case NX_MP_ALG_HASH_THRESHOLD: |
121 | 0 | if (n_links == 1) { |
122 | 0 | return 0; |
123 | 0 | } |
124 | 0 | return hash / (UINT32_MAX / n_links + 1); |
125 | | |
126 | 0 | case NX_MP_ALG_HRW: |
127 | 0 | return (n_links <= 64 |
128 | 0 | ? algorithm_hrw(hash, n_links) |
129 | 0 | : algorithm_iter_hash(hash, n_links, 0)); |
130 | | |
131 | 0 | case NX_MP_ALG_ITER_HASH: |
132 | 0 | return algorithm_iter_hash(hash, n_links, arg); |
133 | 0 | } |
134 | | |
135 | 0 | OVS_NOT_REACHED(); |
136 | 0 | } |
137 | | |
138 | | /* Parses 's_' as a set of arguments to the "multipath" action and initializes |
139 | | * 'mp' accordingly. ovs-actions(7) describes the format parsed. |
140 | | * |
141 | | * Returns NULL if successful, otherwise a malloc()'d string describing the |
142 | | * error. The caller is responsible for freeing the returned string.*/ |
143 | | static char * OVS_WARN_UNUSED_RESULT |
144 | | multipath_parse__(struct ofpact_multipath *mp, const char *s_, char *s) |
145 | 0 | { |
146 | 0 | char *save_ptr = NULL; |
147 | 0 | char *fields, *basis, *algorithm, *n_links_str, *arg, *dst; |
148 | 0 | char *error; |
149 | 0 | int n_links; |
150 | |
|
151 | 0 | fields = strtok_r(s, ", ", &save_ptr); |
152 | 0 | basis = strtok_r(NULL, ", ", &save_ptr); |
153 | 0 | algorithm = strtok_r(NULL, ", ", &save_ptr); |
154 | 0 | n_links_str = strtok_r(NULL, ", ", &save_ptr); |
155 | 0 | arg = strtok_r(NULL, ", ", &save_ptr); |
156 | 0 | dst = strtok_r(NULL, ", ", &save_ptr); |
157 | 0 | if (!dst) { |
158 | 0 | return xasprintf("%s: not enough arguments to multipath action", s_); |
159 | 0 | } |
160 | | |
161 | 0 | ofpact_init_MULTIPATH(mp); |
162 | 0 | if (!strcasecmp(fields, "eth_src")) { |
163 | 0 | mp->fields = NX_HASH_FIELDS_ETH_SRC; |
164 | 0 | } else if (!strcasecmp(fields, "symmetric_l4")) { |
165 | 0 | mp->fields = NX_HASH_FIELDS_SYMMETRIC_L4; |
166 | 0 | } else if (!strcasecmp(fields, "symmetric_l3l4")) { |
167 | 0 | mp->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4; |
168 | 0 | } else if (!strcasecmp(fields, "symmetric_l3l4+udp")) { |
169 | 0 | mp->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP; |
170 | 0 | } else if (!strcasecmp(fields, "nw_src")) { |
171 | 0 | mp->fields = NX_HASH_FIELDS_NW_SRC; |
172 | 0 | } else if (!strcasecmp(fields, "nw_dst")) { |
173 | 0 | mp->fields = NX_HASH_FIELDS_NW_DST; |
174 | 0 | } else if (!strcasecmp(fields, "symmetric_l3")) { |
175 | 0 | mp->fields = NX_HASH_FIELDS_SYMMETRIC_L3; |
176 | 0 | } else { |
177 | 0 | return xasprintf("%s: unknown fields `%s'", s_, fields); |
178 | 0 | } |
179 | 0 | mp->basis = atoi(basis); |
180 | 0 | if (!strcasecmp(algorithm, "modulo_n")) { |
181 | 0 | mp->algorithm = NX_MP_ALG_MODULO_N; |
182 | 0 | } else if (!strcasecmp(algorithm, "hash_threshold")) { |
183 | 0 | mp->algorithm = NX_MP_ALG_HASH_THRESHOLD; |
184 | 0 | } else if (!strcasecmp(algorithm, "hrw")) { |
185 | 0 | mp->algorithm = NX_MP_ALG_HRW; |
186 | 0 | } else if (!strcasecmp(algorithm, "iter_hash")) { |
187 | 0 | mp->algorithm = NX_MP_ALG_ITER_HASH; |
188 | 0 | } else { |
189 | 0 | return xasprintf("%s: unknown algorithm `%s'", s_, algorithm); |
190 | 0 | } |
191 | 0 | n_links = atoi(n_links_str); |
192 | 0 | if (n_links < 1 || n_links > 65536) { |
193 | 0 | return xasprintf("%s: n_links %d is not in valid range 1 to 65536", |
194 | 0 | s_, n_links); |
195 | 0 | } |
196 | 0 | mp->max_link = n_links - 1; |
197 | 0 | mp->arg = atoi(arg); |
198 | |
|
199 | 0 | error = mf_parse_subfield(&mp->dst, dst); |
200 | 0 | if (error) { |
201 | 0 | return error; |
202 | 0 | } |
203 | 0 | if (!mf_nxm_header(mp->dst.field->id)) { |
204 | 0 | return xasprintf("%s: experimenter OXM field '%s' not supported", |
205 | 0 | s_, dst); |
206 | 0 | } |
207 | 0 | if (mp->dst.n_bits < 16 && n_links > (1u << mp->dst.n_bits)) { |
208 | 0 | return xasprintf("%s: %d-bit destination field has %u possible " |
209 | 0 | "values, less than specified n_links %d", |
210 | 0 | s_, mp->dst.n_bits, 1u << mp->dst.n_bits, n_links); |
211 | 0 | } |
212 | | |
213 | 0 | return NULL; |
214 | 0 | } |
215 | | |
216 | | /* Parses 's_' as a set of arguments to the "multipath" action and initializes |
217 | | * 'mp' accordingly. ovs-actions(7) describes the format parsed. |
218 | | * |
219 | | * Returns NULL if successful, otherwise a malloc()'d string describing the |
220 | | * error. The caller is responsible for freeing the returned string. */ |
221 | | char * OVS_WARN_UNUSED_RESULT |
222 | | multipath_parse(struct ofpact_multipath *mp, const char *s_) |
223 | 0 | { |
224 | 0 | char *s = xstrdup(s_); |
225 | 0 | char *error = multipath_parse__(mp, s_, s); |
226 | 0 | free(s); |
227 | 0 | return error; |
228 | 0 | } |
229 | | |
230 | | /* Appends a description of 'mp' to 's', in the format that ovs-actions(7) |
231 | | * describes. */ |
232 | | void |
233 | | multipath_format(const struct ofpact_multipath *mp, struct ds *s) |
234 | 0 | { |
235 | 0 | const char *fields, *algorithm; |
236 | |
|
237 | 0 | fields = flow_hash_fields_to_str(mp->fields); |
238 | |
|
239 | 0 | switch (mp->algorithm) { |
240 | 0 | case NX_MP_ALG_MODULO_N: |
241 | 0 | algorithm = "modulo_n"; |
242 | 0 | break; |
243 | 0 | case NX_MP_ALG_HASH_THRESHOLD: |
244 | 0 | algorithm = "hash_threshold"; |
245 | 0 | break; |
246 | 0 | case NX_MP_ALG_HRW: |
247 | 0 | algorithm = "hrw"; |
248 | 0 | break; |
249 | 0 | case NX_MP_ALG_ITER_HASH: |
250 | 0 | algorithm = "iter_hash"; |
251 | 0 | break; |
252 | 0 | default: |
253 | 0 | algorithm = "<unknown>"; |
254 | 0 | } |
255 | | |
256 | 0 | ds_put_format(s, "%smultipath(%s%s,%"PRIu16",%s,%d,%"PRIu32",", |
257 | 0 | colors.paren, colors.end, fields, mp->basis, algorithm, |
258 | 0 | mp->max_link + 1, mp->arg); |
259 | 0 | mf_format_subfield(&mp->dst, s); |
260 | 0 | ds_put_format(s, "%s)%s", colors.paren, colors.end); |
261 | 0 | } |