2 * Copyright (c) 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "multipath.h"
20 #include <arpa/inet.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include "dynamic-string.h"
26 #include "ofp-errors.h"
28 #include "openflow/nicira-ext.h"
32 VLOG_DEFINE_THIS_MODULE(multipath);
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 /* multipath_check(). */
38 multipath_check(const struct nx_action_multipath *mp, const struct flow *flow)
40 uint32_t n_links = ntohs(mp->max_link) + 1;
41 size_t min_n_bits = log_2_ceil(n_links);
42 int ofs = nxm_decode_ofs(mp->ofs_nbits);
43 int n_bits = nxm_decode_n_bits(mp->ofs_nbits);
46 error = nxm_dst_check(mp->dst, ofs, n_bits, flow);
51 if (!flow_hash_fields_valid(ntohs(mp->fields))) {
52 VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, ntohs(mp->fields));
53 } else if (mp->algorithm != htons(NX_MP_ALG_MODULO_N)
54 && mp->algorithm != htons(NX_MP_ALG_HASH_THRESHOLD)
55 && mp->algorithm != htons(NX_MP_ALG_HRW)
56 && mp->algorithm != htons(NX_MP_ALG_ITER_HASH)) {
57 VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16,
58 ntohs(mp->algorithm));
59 } else if (n_bits < min_n_bits) {
60 VLOG_WARN_RL(&rl, "multipath action requires at least %zu bits for "
61 "%"PRIu32" links", min_n_bits, n_links);
66 return OFPERR_OFPBAC_BAD_ARGUMENT;
69 /* multipath_execute(). */
71 static uint16_t multipath_algorithm(uint32_t hash, enum nx_mp_algorithm,
72 unsigned int n_links, unsigned int arg);
75 multipath_execute(const struct nx_action_multipath *mp, struct flow *flow)
77 /* Calculate value to store. */
78 uint32_t hash = flow_hash_fields(flow, ntohs(mp->fields),
80 uint16_t link = multipath_algorithm(hash, ntohs(mp->algorithm),
81 ntohs(mp->max_link) + 1,
84 nxm_reg_load(mp->dst, mp->ofs_nbits, link, flow);
88 algorithm_hrw(uint32_t hash, unsigned int n_links)
95 best_weight = hash_2words(hash, 0);
96 for (link = 1; link < n_links; link++) {
97 uint32_t weight = hash_2words(hash, link);
98 if (weight > best_weight) {
100 best_weight = weight;
106 /* Works for 'x' in the range [1,65536], which is all we need. */
108 round_up_pow2(unsigned int x)
119 algorithm_iter_hash(uint32_t hash, unsigned int n_links, unsigned int modulo)
124 if (modulo < n_links || modulo / 2 > n_links) {
125 modulo = round_up_pow2(n_links);
130 link = hash_2words(hash, i++) % modulo;
131 } while (link >= n_links);
137 multipath_algorithm(uint32_t hash, enum nx_mp_algorithm algorithm,
138 unsigned int n_links, unsigned int arg)
141 case NX_MP_ALG_MODULO_N:
142 return hash % n_links;
144 case NX_MP_ALG_HASH_THRESHOLD:
148 return hash / (UINT32_MAX / n_links + 1);
151 return (n_links <= 64
152 ? algorithm_hrw(hash, n_links)
153 : algorithm_iter_hash(hash, n_links, 0));
155 case NX_MP_ALG_ITER_HASH:
156 return algorithm_iter_hash(hash, n_links, arg);
162 /* multipath_parse(). */
165 multipath_parse(struct nx_action_multipath *mp, const char *s_)
167 char *s = xstrdup(s_);
168 char *save_ptr = NULL;
169 char *fields, *basis, *algorithm, *n_links_str, *arg, *dst;
174 fields = strtok_r(s, ", ", &save_ptr);
175 basis = strtok_r(NULL, ", ", &save_ptr);
176 algorithm = strtok_r(NULL, ", ", &save_ptr);
177 n_links_str = strtok_r(NULL, ", ", &save_ptr);
178 arg = strtok_r(NULL, ", ", &save_ptr);
179 dst = strtok_r(NULL, ", ", &save_ptr);
181 ovs_fatal(0, "%s: not enough arguments to multipath action", s_);
184 ofputil_init_NXAST_MULTIPATH(mp);
185 if (!strcasecmp(fields, "eth_src")) {
186 mp->fields = htons(NX_HASH_FIELDS_ETH_SRC);
187 } else if (!strcasecmp(fields, "symmetric_l4")) {
188 mp->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
190 ovs_fatal(0, "%s: unknown fields `%s'", s_, fields);
192 mp->basis = htons(atoi(basis));
193 if (!strcasecmp(algorithm, "modulo_n")) {
194 mp->algorithm = htons(NX_MP_ALG_MODULO_N);
195 } else if (!strcasecmp(algorithm, "hash_threshold")) {
196 mp->algorithm = htons(NX_MP_ALG_HASH_THRESHOLD);
197 } else if (!strcasecmp(algorithm, "hrw")) {
198 mp->algorithm = htons(NX_MP_ALG_HRW);
199 } else if (!strcasecmp(algorithm, "iter_hash")) {
200 mp->algorithm = htons(NX_MP_ALG_ITER_HASH);
202 ovs_fatal(0, "%s: unknown algorithm `%s'", s_, algorithm);
204 n_links = atoi(n_links_str);
205 if (n_links < 1 || n_links > 65536) {
206 ovs_fatal(0, "%s: n_links %d is not in valid range 1 to 65536",
209 mp->max_link = htons(n_links - 1);
210 mp->arg = htonl(atoi(arg));
212 nxm_parse_field_bits(dst, &header, &ofs, &n_bits);
213 if (n_bits < 16 && n_links > (1u << n_bits)) {
214 ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
215 "less than specified n_links %d",
216 s_, n_bits, 1u << n_bits, n_links);
218 mp->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
219 mp->dst = htonl(header);
225 multipath_format(const struct nx_action_multipath *mp, struct ds *s)
227 const char *fields, *algorithm;
229 uint16_t mp_fields = ntohs(mp->fields);
230 uint16_t mp_algorithm = ntohs(mp->algorithm);
232 fields = flow_hash_fields_to_str(mp_fields);
234 switch ((enum nx_mp_algorithm) mp_algorithm) {
235 case NX_MP_ALG_MODULO_N:
236 algorithm = "modulo_n";
238 case NX_MP_ALG_HASH_THRESHOLD:
239 algorithm = "hash_threshold";
244 case NX_MP_ALG_ITER_HASH:
245 algorithm = "iter_hash";
248 algorithm = "<unknown>";
251 ds_put_format(s, "multipath(%s,%"PRIu16",%s,%d,%"PRIu16",",
252 fields, ntohs(mp->basis), algorithm, ntohs(mp->max_link) + 1,
254 nxm_format_field_bits(s, ntohl(mp->dst), nxm_decode_ofs(mp->ofs_nbits),
255 nxm_decode_n_bits(mp->ofs_nbits));