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"
27 #include "openflow/nicira-ext.h"
31 VLOG_DEFINE_THIS_MODULE(multipath);
33 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35 /* multipath_check(). */
37 multipath_check(const struct nx_action_multipath *mp, const struct flow *flow)
39 uint32_t n_links = ntohs(mp->max_link) + 1;
40 size_t min_n_bits = log_2_floor(n_links) + 1;
41 int ofs = nxm_decode_ofs(mp->ofs_nbits);
42 int n_bits = nxm_decode_n_bits(mp->ofs_nbits);
45 error = nxm_dst_check(mp->dst, ofs, n_bits, flow);
50 if (!flow_hash_fields_valid(ntohs(mp->fields))) {
51 VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, ntohs(mp->fields));
52 } else if (mp->algorithm != htons(NX_MP_ALG_MODULO_N)
53 && mp->algorithm != htons(NX_MP_ALG_HASH_THRESHOLD)
54 && mp->algorithm != htons(NX_MP_ALG_HRW)
55 && mp->algorithm != htons(NX_MP_ALG_ITER_HASH)) {
56 VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16,
57 ntohs(mp->algorithm));
58 } else if (n_bits < min_n_bits) {
59 VLOG_WARN_RL(&rl, "multipath action requires at least %zu bits for "
60 "%"PRIu32" links", min_n_bits, n_links);
65 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
68 /* multipath_execute(). */
70 static uint16_t multipath_algorithm(uint32_t hash, enum nx_mp_algorithm,
71 unsigned int n_links, unsigned int arg);
74 multipath_execute(const struct nx_action_multipath *mp, struct flow *flow)
76 /* Calculate value to store. */
77 uint32_t hash = flow_hash_fields(flow, ntohs(mp->fields),
79 uint16_t link = multipath_algorithm(hash, ntohs(mp->algorithm),
80 ntohs(mp->max_link) + 1,
83 nxm_reg_load(mp->dst, mp->ofs_nbits, link, flow);
87 algorithm_hrw(uint32_t hash, unsigned int n_links)
94 best_weight = hash_2words(hash, 0);
95 for (link = 1; link < n_links; link++) {
96 uint32_t weight = hash_2words(hash, link);
97 if (weight > best_weight) {
105 /* Works for 'x' in the range [1,65536], which is all we need. */
107 round_up_pow2(unsigned int x)
118 algorithm_iter_hash(uint32_t hash, unsigned int n_links, unsigned int modulo)
123 if (modulo < n_links || modulo / 2 > n_links) {
124 modulo = round_up_pow2(n_links);
129 link = hash_2words(hash, i++) % modulo;
130 } while (link >= n_links);
136 multipath_algorithm(uint32_t hash, enum nx_mp_algorithm algorithm,
137 unsigned int n_links, unsigned int arg)
140 case NX_MP_ALG_MODULO_N:
141 return hash % n_links;
143 case NX_MP_ALG_HASH_THRESHOLD:
147 return hash / (UINT32_MAX / n_links + 1);
150 return (n_links <= 64
151 ? algorithm_hrw(hash, n_links)
152 : algorithm_iter_hash(hash, n_links, 0));
154 case NX_MP_ALG_ITER_HASH:
155 return algorithm_iter_hash(hash, n_links, arg);
161 /* multipath_parse(). */
164 multipath_parse(struct nx_action_multipath *mp, const char *s_)
166 char *s = xstrdup(s_);
167 char *save_ptr = NULL;
168 char *fields, *basis, *algorithm, *n_links_str, *arg, *dst;
173 fields = strtok_r(s, ", ", &save_ptr);
174 basis = strtok_r(NULL, ", ", &save_ptr);
175 algorithm = strtok_r(NULL, ", ", &save_ptr);
176 n_links_str = strtok_r(NULL, ", ", &save_ptr);
177 arg = strtok_r(NULL, ", ", &save_ptr);
178 dst = strtok_r(NULL, ", ", &save_ptr);
180 ovs_fatal(0, "%s: not enough arguments to multipath action", s_);
183 ofputil_init_NXAST_MULTIPATH(mp);
184 if (!strcasecmp(fields, "eth_src")) {
185 mp->fields = htons(NX_HASH_FIELDS_ETH_SRC);
186 } else if (!strcasecmp(fields, "symmetric_l4")) {
187 mp->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
189 ovs_fatal(0, "%s: unknown fields `%s'", s_, fields);
191 mp->basis = htons(atoi(basis));
192 if (!strcasecmp(algorithm, "modulo_n")) {
193 mp->algorithm = htons(NX_MP_ALG_MODULO_N);
194 } else if (!strcasecmp(algorithm, "hash_threshold")) {
195 mp->algorithm = htons(NX_MP_ALG_HASH_THRESHOLD);
196 } else if (!strcasecmp(algorithm, "hrw")) {
197 mp->algorithm = htons(NX_MP_ALG_HRW);
198 } else if (!strcasecmp(algorithm, "iter_hash")) {
199 mp->algorithm = htons(NX_MP_ALG_ITER_HASH);
201 ovs_fatal(0, "%s: unknown algorithm `%s'", s_, algorithm);
203 n_links = atoi(n_links_str);
204 if (n_links < 1 || n_links > 65536) {
205 ovs_fatal(0, "%s: n_links %d is not in valid range 1 to 65536",
208 mp->max_link = htons(n_links - 1);
209 mp->arg = htonl(atoi(arg));
211 nxm_parse_field_bits(dst, &header, &ofs, &n_bits);
212 if (n_bits < 16 && n_links > (1u << n_bits)) {
213 ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
214 "less than specified n_links %d",
215 s_, n_bits, 1u << n_bits, n_links);
217 mp->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
218 mp->dst = htonl(header);
224 multipath_format(const struct nx_action_multipath *mp, struct ds *s)
226 const char *fields, *algorithm;
228 uint16_t mp_fields = ntohs(mp->fields);
229 uint16_t mp_algorithm = ntohs(mp->algorithm);
231 fields = flow_hash_fields_to_str(mp_fields);
233 switch ((enum nx_mp_algorithm) mp_algorithm) {
234 case NX_MP_ALG_MODULO_N:
235 algorithm = "modulo_n";
237 case NX_MP_ALG_HASH_THRESHOLD:
238 algorithm = "hash_threshold";
243 case NX_MP_ALG_ITER_HASH:
244 algorithm = "iter_hash";
247 algorithm = "<unknown>";
250 ds_put_format(s, "multipath(%s,%"PRIu16",%s,%d,%"PRIu16",",
251 fields, ntohs(mp->basis), algorithm, ntohs(mp->max_link) + 1,
253 nxm_format_field_bits(s, ntohl(mp->dst), nxm_decode_ofs(mp->ofs_nbits),
254 nxm_decode_n_bits(mp->ofs_nbits));