nicira-ext: Generalize nx_mp_fields into nx_hash_fields.
[sliver-openvswitch.git] / lib / multipath.c
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
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 <arpa/inet.h>
21 #include <inttypes.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include "dynamic-string.h"
25 #include "nx-match.h"
26 #include "ofp-util.h"
27 #include "openflow/nicira-ext.h"
28 #include "packets.h"
29 #include "vlog.h"
30
31 VLOG_DEFINE_THIS_MODULE(multipath);
32
33 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
34 \f
35 /* multipath_check(). */
36 int
37 multipath_check(const struct nx_action_multipath *mp)
38 {
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);
42
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");
57     } else {
58         return 0;
59     }
60
61     return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
62 }
63 \f
64 /* multipath_execute(). */
65
66 static uint16_t multipath_algorithm(uint32_t hash, enum nx_mp_algorithm,
67                                     unsigned int n_links, unsigned int arg);
68
69 void
70 multipath_execute(const struct nx_action_multipath *mp, struct flow *flow)
71 {
72     /* Calculate value to store. */
73     uint32_t hash = flow_hash_fields(flow, ntohs(mp->fields),
74                                      ntohs(mp->basis));
75     uint16_t link = multipath_algorithm(hash, ntohs(mp->algorithm),
76                                         ntohs(mp->max_link) + 1,
77                                         ntohl(mp->arg));
78
79     /* Store it. */
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);
85 }
86
87 static uint16_t
88 algorithm_hrw(uint32_t hash, unsigned int n_links)
89 {
90     uint32_t best_weight;
91     uint16_t best_link;
92     unsigned int link;
93
94     best_link = 0;
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) {
99             best_link = link;
100             best_weight = weight;
101         }
102     }
103     return best_link;
104 }
105
106 /* Works for 'x' in the range [1,65536], which is all we need.  */
107 static unsigned int
108 round_up_pow2(unsigned int x)
109 {
110     x--;
111     x |= x >> 1;
112     x |= x >> 2;
113     x |= x >> 4;
114     x |= x >> 8;
115     return x + 1;
116 }
117
118 static uint16_t
119 algorithm_iter_hash(uint32_t hash, unsigned int n_links, unsigned int modulo)
120 {
121     uint16_t link;
122     int i;
123
124     if (modulo < n_links || modulo / 2 > n_links) {
125         modulo = round_up_pow2(n_links);
126     }
127
128     i = 0;
129     do {
130         link = hash_2words(hash, i++) % modulo;
131     } while (link >= n_links);
132
133     return link;
134 }
135
136 static uint16_t
137 multipath_algorithm(uint32_t hash, enum nx_mp_algorithm algorithm,
138                     unsigned int n_links, unsigned int arg)
139 {
140     switch (algorithm) {
141     case NX_MP_ALG_MODULO_N:
142         return hash % n_links;
143
144     case NX_MP_ALG_HASH_THRESHOLD:
145         if (n_links == 1) {
146             return 0;
147         }
148         return hash / (UINT32_MAX / n_links + 1);
149
150     case NX_MP_ALG_HRW:
151         return (n_links <= 64
152                 ? algorithm_hrw(hash, n_links)
153                 : algorithm_iter_hash(hash, n_links, 0));
154
155     case NX_MP_ALG_ITER_HASH:
156         return algorithm_iter_hash(hash, n_links, arg);
157     }
158
159     NOT_REACHED();
160 }
161 \f
162 /* multipath_parse(). */
163
164 void
165 multipath_parse(struct nx_action_multipath *mp, const char *s_)
166 {
167     char *s = xstrdup(s_);
168     char *save_ptr = NULL;
169     char *fields, *basis, *algorithm, *n_links_str, *arg, *dst;
170     uint32_t header;
171     int ofs, n_bits;
172     int n_links;
173
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);
180     if (!dst) {
181         ovs_fatal(0, "%s: not enough arguments to multipath action", s_);
182     }
183
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);
193     } else {
194         ovs_fatal(0, "%s: unknown fields `%s'", s_, fields);
195     }
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);
205     } else {
206         ovs_fatal(0, "%s: unknown algorithm `%s'", s_, algorithm);
207     }
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",
211                   s_, n_links);
212     }
213     mp->max_link = htons(n_links - 1);
214     mp->arg = htonl(atoi(arg));
215
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_);
219     }
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);
224     }
225     mp->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
226     mp->dst = htonl(header);
227
228     free(s);
229 }
230
231 void
232 multipath_format(const struct nx_action_multipath *mp, struct ds *s)
233 {
234     const char *fields, *algorithm;
235
236     uint16_t mp_fields    = ntohs(mp->fields);
237     uint16_t mp_algorithm = ntohs(mp->algorithm);
238
239     fields = flow_hash_fields_to_str(mp_fields);
240
241     switch ((enum nx_mp_algorithm) mp_algorithm) {
242     case NX_MP_ALG_MODULO_N:
243         algorithm = "modulo_n";
244         break;
245     case NX_MP_ALG_HASH_THRESHOLD:
246         algorithm = "hash_threshold";
247         break;
248     case NX_MP_ALG_HRW:
249         algorithm = "hrw";
250         break;
251     case NX_MP_ALG_ITER_HASH:
252         algorithm = "iter_hash";
253         break;
254     default:
255         algorithm = "<unknown>";
256     }
257
258     ds_put_format(s, "multipath(%s,%"PRIu16",%s,%d,%"PRIu16",",
259                   fields, ntohs(mp->basis), algorithm, ntohs(mp->max_link) + 1,
260                   ntohl(mp->arg));
261     nxm_format_field_bits(s, ntohl(mp->dst), nxm_decode_ofs(mp->ofs_nbits),
262                           nxm_decode_n_bits(mp->ofs_nbits));
263     ds_put_char(s, ')');
264 }