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)
39 uint32_t dst = ntohl(mp->dst);
40 int ofs = nxm_decode_ofs(mp->ofs_nbits);
41 int n_bits = nxm_decode_n_bits(mp->ofs_nbits);
43 if (!flow_hash_fields_valid(ntohs(mp->fields))) {
44 VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, ntohs(mp->fields));
45 } else if (mp->algorithm != htons(NX_MP_ALG_MODULO_N)
46 && mp->algorithm != htons(NX_MP_ALG_HASH_THRESHOLD)
47 && mp->algorithm != htons(NX_MP_ALG_HRW)
48 && mp->algorithm != htons(NX_MP_ALG_ITER_HASH)) {
49 VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16,
50 ntohs(mp->algorithm));
51 } else if (!NXM_IS_NX_REG(dst) || NXM_NX_REG_IDX(dst) >= FLOW_N_REGS) {
52 VLOG_WARN_RL(&rl, "unsupported destination field %#"PRIx32, dst);
53 } else if (ofs + n_bits > nxm_field_bits(dst)) {
54 VLOG_WARN_RL(&rl, "destination overflows output field");
55 } else if (n_bits < 16 && ntohs(mp->max_link) > (1u << n_bits)) {
56 VLOG_WARN_RL(&rl, "max_link overflows output field");
61 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
64 /* multipath_execute(). */
66 static uint16_t multipath_algorithm(uint32_t hash, enum nx_mp_algorithm,
67 unsigned int n_links, unsigned int arg);
70 multipath_execute(const struct nx_action_multipath *mp, struct flow *flow)
72 /* Calculate value to store. */
73 uint32_t hash = flow_hash_fields(flow, ntohs(mp->fields),
75 uint16_t link = multipath_algorithm(hash, ntohs(mp->algorithm),
76 ntohs(mp->max_link) + 1,
80 uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(mp->dst))];
81 int ofs = nxm_decode_ofs(mp->ofs_nbits);
82 int n_bits = nxm_decode_n_bits(mp->ofs_nbits);
83 uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
84 *reg = (*reg & ~(mask << ofs)) | (link << ofs);
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 memset(mp, 0, sizeof *mp);
185 mp->type = htons(OFPAT_VENDOR);
186 mp->len = htons(sizeof *mp);
187 mp->vendor = htonl(NX_VENDOR_ID);
188 mp->subtype = htons(NXAST_MULTIPATH);
189 if (!strcasecmp(fields, "eth_src")) {
190 mp->fields = htons(NX_HASH_FIELDS_ETH_SRC);
191 } else if (!strcasecmp(fields, "symmetric_l4")) {
192 mp->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
194 ovs_fatal(0, "%s: unknown fields `%s'", s_, fields);
196 mp->basis = htons(atoi(basis));
197 if (!strcasecmp(algorithm, "modulo_n")) {
198 mp->algorithm = htons(NX_MP_ALG_MODULO_N);
199 } else if (!strcasecmp(algorithm, "hash_threshold")) {
200 mp->algorithm = htons(NX_MP_ALG_HASH_THRESHOLD);
201 } else if (!strcasecmp(algorithm, "hrw")) {
202 mp->algorithm = htons(NX_MP_ALG_HRW);
203 } else if (!strcasecmp(algorithm, "iter_hash")) {
204 mp->algorithm = htons(NX_MP_ALG_ITER_HASH);
206 ovs_fatal(0, "%s: unknown algorithm `%s'", s_, algorithm);
208 n_links = atoi(n_links_str);
209 if (n_links < 1 || n_links > 65536) {
210 ovs_fatal(0, "%s: n_links %d is not in valid range 1 to 65536",
213 mp->max_link = htons(n_links - 1);
214 mp->arg = htonl(atoi(arg));
216 nxm_parse_field_bits(dst, &header, &ofs, &n_bits);
217 if (!NXM_IS_NX_REG(header) || NXM_NX_REG_IDX(header) >= FLOW_N_REGS) {
218 ovs_fatal(0, "%s: destination field must be register", s_);
220 if (n_bits < 16 && n_links > (1u << n_bits)) {
221 ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
222 "less than specified n_links %d",
223 s_, n_bits, 1u << n_bits, n_links);
225 mp->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
226 mp->dst = htonl(header);
232 multipath_format(const struct nx_action_multipath *mp, struct ds *s)
234 const char *fields, *algorithm;
236 uint16_t mp_fields = ntohs(mp->fields);
237 uint16_t mp_algorithm = ntohs(mp->algorithm);
239 fields = flow_hash_fields_to_str(mp_fields);
241 switch ((enum nx_mp_algorithm) mp_algorithm) {
242 case NX_MP_ALG_MODULO_N:
243 algorithm = "modulo_n";
245 case NX_MP_ALG_HASH_THRESHOLD:
246 algorithm = "hash_threshold";
251 case NX_MP_ALG_ITER_HASH:
252 algorithm = "iter_hash";
255 algorithm = "<unknown>";
258 ds_put_format(s, "multipath(%s,%"PRIu16",%s,%d,%"PRIu16",",
259 fields, ntohs(mp->basis), algorithm, ntohs(mp->max_link) + 1,
261 nxm_format_field_bits(s, ntohl(mp->dst), nxm_decode_ofs(mp->ofs_nbits),
262 nxm_decode_n_bits(mp->ofs_nbits));