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 (mp->fields != htons(NX_MP_FIELDS_ETH_SRC)
44 && mp->fields != htons(NX_MP_FIELDS_SYMMETRIC_L4)) {
45 VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, ntohs(mp->fields));
46 } else if (mp->algorithm != htons(NX_MP_ALG_MODULO_N)
47 && mp->algorithm != htons(NX_MP_ALG_HASH_THRESHOLD)
48 && mp->algorithm != htons(NX_MP_ALG_HRW)
49 && mp->algorithm != htons(NX_MP_ALG_ITER_HASH)) {
50 VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16,
51 ntohs(mp->algorithm));
52 } else if (!NXM_IS_NX_REG(dst) || NXM_NX_REG_IDX(dst) >= FLOW_N_REGS) {
53 VLOG_WARN_RL(&rl, "unsupported destination field %#"PRIx32, dst);
54 } else if (ofs + n_bits > nxm_field_bits(dst)) {
55 VLOG_WARN_RL(&rl, "destination overflows output field");
56 } else if (n_bits < 16 && ntohs(mp->max_link) > (1u << n_bits)) {
57 VLOG_WARN_RL(&rl, "max_link overflows output field");
62 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
65 /* multipath_execute(). */
67 static uint32_t multipath_hash(const struct flow *, enum nx_mp_fields,
69 static uint16_t multipath_algorithm(uint32_t hash, enum nx_mp_algorithm,
70 unsigned int n_links, unsigned int arg);
73 multipath_execute(const struct nx_action_multipath *mp, struct flow *flow)
75 /* Calculate value to store. */
76 uint32_t hash = multipath_hash(flow, ntohs(mp->fields), ntohs(mp->basis));
77 uint16_t link = multipath_algorithm(hash, ntohs(mp->algorithm),
78 ntohs(mp->max_link) + 1,
82 uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(mp->dst))];
83 int ofs = nxm_decode_ofs(mp->ofs_nbits);
84 int n_bits = nxm_decode_n_bits(mp->ofs_nbits);
85 uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
86 *reg = (*reg & ~(mask << ofs)) | (link << ofs);
90 hash_symmetric_l4(const struct flow *flow, uint16_t basis)
97 uint8_t eth_addr[ETH_ADDR_LEN];
103 memset(&fields, 0, sizeof fields);
104 for (i = 0; i < ETH_ADDR_LEN; i++) {
105 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
107 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
108 fields.eth_type = flow->dl_type;
109 if (fields.eth_type == htons(ETH_TYPE_IP)) {
110 fields.ip_addr = flow->nw_src ^ flow->nw_dst;
111 fields.ip_proto = flow->nw_proto;
112 if (fields.ip_proto == IP_TYPE_TCP || fields.ip_proto == IP_TYPE_UDP) {
113 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
115 fields.tp_addr = htons(0);
118 fields.ip_addr = htonl(0);
120 fields.tp_addr = htons(0);
122 return hash_bytes(&fields, sizeof fields, basis);
126 multipath_hash(const struct flow *flow, enum nx_mp_fields fields,
130 case NX_MP_FIELDS_ETH_SRC:
131 return hash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
133 case NX_MP_FIELDS_SYMMETRIC_L4:
134 return hash_symmetric_l4(flow, basis);
141 algorithm_hrw(uint32_t hash, unsigned int n_links)
143 uint32_t best_weight;
148 best_weight = hash_2words(hash, 0);
149 for (link = 1; link < n_links; link++) {
150 uint32_t weight = hash_2words(hash, link);
151 if (weight > best_weight) {
153 best_weight = weight;
159 /* Works for 'x' in the range [1,65536], which is all we need. */
161 round_up_pow2(unsigned int x)
172 algorithm_iter_hash(uint32_t hash, unsigned int n_links, unsigned int modulo)
177 if (modulo < n_links || modulo / 2 > n_links) {
178 modulo = round_up_pow2(n_links);
183 link = hash_2words(hash, i++) % modulo;
184 } while (link >= n_links);
190 multipath_algorithm(uint32_t hash, enum nx_mp_algorithm algorithm,
191 unsigned int n_links, unsigned int arg)
194 case NX_MP_ALG_MODULO_N:
195 return hash % n_links;
197 case NX_MP_ALG_HASH_THRESHOLD:
201 return hash / (UINT32_MAX / n_links + 1);
204 return (n_links <= 64
205 ? algorithm_hrw(hash, n_links)
206 : algorithm_iter_hash(hash, n_links, 0));
208 case NX_MP_ALG_ITER_HASH:
209 return algorithm_iter_hash(hash, n_links, arg);
215 /* multipath_parse(). */
218 multipath_parse(struct nx_action_multipath *mp, const char *s_)
220 char *s = xstrdup(s_);
221 char *save_ptr = NULL;
222 char *fields, *basis, *algorithm, *n_links, *arg, *dst;
226 fields = strtok_r(s, ", ", &save_ptr);
227 basis = strtok_r(NULL, ", ", &save_ptr);
228 algorithm = strtok_r(NULL, ", ", &save_ptr);
229 n_links = strtok_r(NULL, ", ", &save_ptr);
230 arg = strtok_r(NULL, ", ", &save_ptr);
231 dst = strtok_r(NULL, ", ", &save_ptr);
233 ovs_fatal(0, "%s: not enough arguments to multipath action", s);
236 memset(mp, 0, sizeof *mp);
237 mp->type = htons(OFPAT_VENDOR);
238 mp->len = htons(sizeof *mp);
239 mp->vendor = htonl(NX_VENDOR_ID);
240 mp->subtype = htons(NXAST_MULTIPATH);
241 if (!strcasecmp(fields, "eth_src")) {
242 mp->fields = htons(NX_MP_FIELDS_ETH_SRC);
243 } else if (!strcasecmp(fields, "symmetric_l4")) {
244 mp->fields = htons(NX_MP_FIELDS_SYMMETRIC_L4);
246 ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
248 mp->basis = htons(atoi(basis));
249 if (!strcasecmp(algorithm, "modulo_n")) {
250 mp->algorithm = htons(NX_MP_ALG_MODULO_N);
251 } else if (!strcasecmp(algorithm, "hash_threshold")) {
252 mp->algorithm = htons(NX_MP_ALG_HASH_THRESHOLD);
253 } else if (!strcasecmp(algorithm, "hrw")) {
254 mp->algorithm = htons(NX_MP_ALG_HRW);
255 } else if (!strcasecmp(algorithm, "iter_hash")) {
256 mp->algorithm = htons(NX_MP_ALG_ITER_HASH);
258 ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
260 mp->max_link = htons(atoi(n_links) - 1);
261 mp->arg = htonl(atoi(arg));
263 nxm_parse_field_bits(dst, &header, &ofs, &n_bits);
264 mp->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
265 mp->dst = htonl(header);
271 multipath_format(const struct nx_action_multipath *mp, struct ds *s)
273 const char *fields, *algorithm;
275 uint16_t mp_fields = ntohs(mp->fields);
276 uint16_t mp_algorithm = ntohs(mp->algorithm);
278 switch ((enum nx_mp_fields) mp_fields) {
279 case NX_MP_FIELDS_ETH_SRC:
282 case NX_MP_FIELDS_SYMMETRIC_L4:
283 fields = "symmetric_l4";
286 fields = "<unknown>";
289 switch ((enum nx_mp_algorithm) mp_algorithm) {
290 case NX_MP_ALG_MODULO_N:
291 algorithm = "modulo_n";
293 case NX_MP_ALG_HASH_THRESHOLD:
294 algorithm = "hash_threshold";
299 case NX_MP_ALG_ITER_HASH:
300 algorithm = "iter_hash";
303 algorithm = "<unknown>";
306 ds_put_format(s, "multipath(%s,%"PRIu16",%s,%d,%"PRIu16",",
307 fields, ntohs(mp->basis), algorithm, ntohs(mp->max_link) + 1,
309 nxm_format_field_bits(s, ntohl(mp->dst), nxm_decode_ofs(mp->ofs_nbits),
310 nxm_decode_n_bits(mp->ofs_nbits));