Better abstract OpenFlow error codes.
[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-errors.h"
27 #include "ofp-util.h"
28 #include "openflow/nicira-ext.h"
29 #include "packets.h"
30 #include "vlog.h"
31
32 VLOG_DEFINE_THIS_MODULE(multipath);
33
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35 \f
36 /* multipath_check(). */
37 enum ofperr
38 multipath_check(const struct nx_action_multipath *mp, const struct flow *flow)
39 {
40     uint32_t n_links = ntohs(mp->max_link) + 1;
41     size_t min_n_bits = log_2_floor(n_links) + 1;
42     int ofs = nxm_decode_ofs(mp->ofs_nbits);
43     int n_bits = nxm_decode_n_bits(mp->ofs_nbits);
44     enum ofperr error;
45
46     error = nxm_dst_check(mp->dst, ofs, n_bits, flow);
47     if (error) {
48         return error;
49     }
50
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);
62     } else {
63         return 0;
64     }
65
66     return OFPERR_OFPBAC_BAD_ARGUMENT;
67 }
68 \f
69 /* multipath_execute(). */
70
71 static uint16_t multipath_algorithm(uint32_t hash, enum nx_mp_algorithm,
72                                     unsigned int n_links, unsigned int arg);
73
74 void
75 multipath_execute(const struct nx_action_multipath *mp, struct flow *flow)
76 {
77     /* Calculate value to store. */
78     uint32_t hash = flow_hash_fields(flow, ntohs(mp->fields),
79                                      ntohs(mp->basis));
80     uint16_t link = multipath_algorithm(hash, ntohs(mp->algorithm),
81                                         ntohs(mp->max_link) + 1,
82                                         ntohl(mp->arg));
83
84     nxm_reg_load(mp->dst, mp->ofs_nbits, link, flow);
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     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);
189     } else {
190         ovs_fatal(0, "%s: unknown fields `%s'", s_, fields);
191     }
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);
201     } else {
202         ovs_fatal(0, "%s: unknown algorithm `%s'", s_, algorithm);
203     }
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",
207                   s_, n_links);
208     }
209     mp->max_link = htons(n_links - 1);
210     mp->arg = htonl(atoi(arg));
211
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);
217     }
218     mp->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
219     mp->dst = htonl(header);
220
221     free(s);
222 }
223
224 void
225 multipath_format(const struct nx_action_multipath *mp, struct ds *s)
226 {
227     const char *fields, *algorithm;
228
229     uint16_t mp_fields    = ntohs(mp->fields);
230     uint16_t mp_algorithm = ntohs(mp->algorithm);
231
232     fields = flow_hash_fields_to_str(mp_fields);
233
234     switch ((enum nx_mp_algorithm) mp_algorithm) {
235     case NX_MP_ALG_MODULO_N:
236         algorithm = "modulo_n";
237         break;
238     case NX_MP_ALG_HASH_THRESHOLD:
239         algorithm = "hash_threshold";
240         break;
241     case NX_MP_ALG_HRW:
242         algorithm = "hrw";
243         break;
244     case NX_MP_ALG_ITER_HASH:
245         algorithm = "iter_hash";
246         break;
247     default:
248         algorithm = "<unknown>";
249     }
250
251     ds_put_format(s, "multipath(%s,%"PRIu16",%s,%d,%"PRIu16",",
252                   fields, ntohs(mp->basis), algorithm, ntohs(mp->max_link) + 1,
253                   ntohl(mp->arg));
254     nxm_format_field_bits(s, ntohl(mp->dst), nxm_decode_ofs(mp->ofs_nbits),
255                           nxm_decode_n_bits(mp->ofs_nbits));
256     ds_put_char(s, ')');
257 }