]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-table.c
ovs-vswitchd: Implement OFPT_TABLE_FEATURES table modification request.
[mirror_ovs.git] / lib / ofp-table.c
1 /*
2 * Copyright (c) 2008-2017 Nicira, Inc.
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 #include "openvswitch/ofp-table.h"
19 #include "bitmap.h"
20 #include "nx-match.h"
21 #include "openvswitch/dynamic-string.h"
22 #include "openvswitch/json.h"
23 #include "openvswitch/ofp-actions.h"
24 #include "openvswitch/ofp-msgs.h"
25 #include "openvswitch/ofp-print.h"
26 #include "openvswitch/ofp-prop.h"
27 #include "openvswitch/ofpbuf.h"
28 #include "openvswitch/vlog.h"
29 #include "util.h"
30
31 VLOG_DEFINE_THIS_MODULE(ofp_table);
32
33 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
34
35 static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
36 enum ofputil_table_eviction,
37 enum ofputil_table_vacancy,
38 enum ofp_version);
39 static enum ofputil_table_vacancy ofputil_decode_table_vacancy(
40 ovs_be32 config, enum ofp_version);
41 static enum ofputil_table_eviction ofputil_decode_table_eviction(
42 ovs_be32 config, enum ofp_version);
43
44 const char *
45 ofputil_table_miss_to_string(enum ofputil_table_miss miss)
46 {
47 switch (miss) {
48 case OFPUTIL_TABLE_MISS_DEFAULT: return "default";
49 case OFPUTIL_TABLE_MISS_CONTROLLER: return "controller";
50 case OFPUTIL_TABLE_MISS_CONTINUE: return "continue";
51 case OFPUTIL_TABLE_MISS_DROP: return "drop";
52 default: return "***error***";
53 }
54 }
55
56 const char *
57 ofputil_table_eviction_to_string(enum ofputil_table_eviction eviction)
58 {
59 switch (eviction) {
60 case OFPUTIL_TABLE_EVICTION_DEFAULT: return "default";
61 case OFPUTIL_TABLE_EVICTION_ON: return "on";
62 case OFPUTIL_TABLE_EVICTION_OFF: return "off";
63 default: return "***error***";
64 }
65 }
66
67 const char *
68 ofputil_table_vacancy_to_string(enum ofputil_table_vacancy vacancy)
69 {
70 switch (vacancy) {
71 case OFPUTIL_TABLE_VACANCY_DEFAULT: return "default";
72 case OFPUTIL_TABLE_VACANCY_ON: return "on";
73 case OFPUTIL_TABLE_VACANCY_OFF: return "off";
74 default: return "***error***";
75 }
76 }
77
78 static bool
79 ofp15_table_features_command_is_valid(enum ofp15_table_features_command cmd)
80 {
81 switch (cmd) {
82 case OFPTFC15_REPLACE:
83 case OFPTFC15_MODIFY:
84 case OFPTFC15_ENABLE:
85 case OFPTFC15_DISABLE:
86 return true;
87
88 default:
89 return false;
90 }
91 }
92
93 static const char *
94 ofp15_table_features_command_to_string(enum ofp15_table_features_command cmd)
95 {
96 switch (cmd) {
97 case OFPTFC15_REPLACE: return "replace";
98 case OFPTFC15_MODIFY: return "modify";
99 case OFPTFC15_ENABLE: return "enable";
100 case OFPTFC15_DISABLE: return "disable";
101 default: return "***bad command***";
102 }
103 }
104 \f
105 /* ofputil_table_map. */
106
107 void
108 ofputil_table_map_init(struct ofputil_table_map *map)
109 {
110 namemap_init(&map->map);
111 }
112
113 void
114 ofputil_table_map_put(struct ofputil_table_map *map,
115 uint8_t table_id, const char *name)
116 {
117 namemap_put(&map->map, table_id, name);
118 }
119
120 const char *
121 ofputil_table_map_get_name(const struct ofputil_table_map *map,
122 uint8_t table_id)
123 {
124 struct namemap_node *node
125 = map ? namemap_find_by_number(&map->map, table_id) : NULL;
126 return node && !node->duplicate ? node->name : NULL;
127 }
128
129 uint8_t
130 ofputil_table_map_get_number(const struct ofputil_table_map *map,
131 const char *name)
132 {
133 struct namemap_node *node
134 = map ? namemap_find_by_name(&map->map, name) : NULL;
135 return node && !node->duplicate ? node->number : UINT8_MAX;
136 }
137
138 void
139 ofputil_table_map_destroy(struct ofputil_table_map *map)
140 {
141 namemap_destroy(&map->map);
142 }
143 \f
144 /* Table numbers. */
145
146 /* Stores the table number represented by 's' into '*tablep'. 's' may be an
147 * integer or, if 'table_map' is nonnull, a name (quoted or unquoted).
148 *
149 * Returns true if successful, false if 's' is not a valid OpenFlow table
150 * number or name. The caller should issue an error message in this case,
151 * because this function usually does not. (This gives the caller an
152 * opportunity to look up the table name another way, e.g. by contacting the
153 * switch and listing the names of all its tables). */
154 bool
155 ofputil_table_from_string(const char *s,
156 const struct ofputil_table_map *table_map,
157 uint8_t *tablep)
158 {
159 *tablep = 0;
160 if (*s == '-') {
161 VLOG_WARN("Negative value %s is not a valid table number.", s);
162 return false;
163 }
164
165 unsigned int table;
166 if (str_to_uint(s, 10, &table)) {
167 if (table > 255) {
168 VLOG_WARN("table %u is outside the supported range 0 through 255",
169 table);
170 return false;
171 }
172 *tablep = table;
173 return true;
174 } else {
175 if (s[0] != '"') {
176 table = ofputil_table_map_get_number(table_map, s);
177 } else {
178 size_t length = strlen(s);
179 char *name = NULL;
180 if (length > 1
181 && s[length - 1] == '"'
182 && json_string_unescape(s + 1, length - 2, &name)) {
183 table = ofputil_table_map_get_number(table_map, name);
184 }
185 free(name);
186 }
187 if (table != UINT8_MAX) {
188 *tablep = table;
189 return true;
190 }
191
192 return false;
193 }
194 }
195
196 /* Appends to 's' a string representation of the OpenFlow table number 'table',
197 * either the table number or a name drawn from 'table_map'. */
198 void
199 ofputil_format_table(uint8_t table, const struct ofputil_table_map *table_map,
200 struct ds *s)
201 {
202 const char *table_name = ofputil_table_map_get_name(table_map, table);
203 if (table_name) {
204 namemap_put_name(table_name, s);
205 } else {
206 ds_put_format(s, "%"PRIu8, table);
207 }
208 }
209
210 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
211 * representation of OpenFlow table number 'table', either the table's number
212 * or a name drawn from 'table_map'. */
213 void
214 ofputil_table_to_string(uint8_t table,
215 const struct ofputil_table_map *table_map,
216 char *namebuf, size_t bufsize)
217 {
218 const char *table_name = ofputil_table_map_get_name(table_map, table);
219 if (table_name) {
220 struct ds s = DS_EMPTY_INITIALIZER;
221 namemap_put_name(table_name, &s);
222 ovs_strlcpy(namebuf, ds_cstr(&s), bufsize);
223 ds_destroy(&s);
224 return;
225 }
226
227 snprintf(namebuf, bufsize, "%"PRIu8, table);
228 }
229 \f
230 /* Table features. */
231
232 static enum ofperr
233 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
234 uint64_t *typep)
235 {
236 enum ofperr error;
237
238 error = ofpprop_pull(msg, payload, typep);
239 if (payload && !error) {
240 ofpbuf_pull(payload, (char *)payload->msg - (char *)payload->header);
241 }
242 return error;
243 }
244
245 static enum ofperr
246 parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
247 uint64_t *ofpacts)
248 {
249 uint32_t types = 0;
250
251 while (payload->size > 0) {
252 enum ofperr error;
253 uint64_t type;
254
255 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &type);
256 if (error) {
257 return error;
258 }
259 if (type < CHAR_BIT * sizeof types) {
260 types |= 1u << type;
261 }
262 }
263
264 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
265 return 0;
266 }
267
268 static enum ofperr
269 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
270 {
271 *insts = 0;
272 while (payload->size > 0) {
273 enum ovs_instruction_type inst;
274 enum ofperr error;
275 uint64_t ofpit;
276
277 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
278 * It seems clear that they aren't padded to 8 bytes, though, because
279 * both standards say that "non-experimenter instructions are 4 bytes"
280 * and do not mention any padding before the first instruction ID.
281 * (There wouldn't be any point in padding to 8 bytes if the IDs were
282 * aligned on an odd 4-byte boundary.)
283 *
284 * Anyway, we just assume they're all glommed together on byte
285 * boundaries. */
286 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &ofpit);
287 if (error) {
288 return error;
289 }
290
291 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
292 if (!error) {
293 *insts |= 1u << inst;
294 } else if (!loose) {
295 return error;
296 }
297 }
298 return 0;
299 }
300
301 static enum ofperr
302 parse_table_features_next_table(struct ofpbuf *payload,
303 unsigned long int *next_tables)
304 {
305 size_t i;
306
307 memset(next_tables, 0, bitmap_n_bytes(255));
308 for (i = 0; i < payload->size; i++) {
309 uint8_t id = ((const uint8_t *) payload->data)[i];
310 if (id >= 255) {
311 return OFPERR_OFPBPC_BAD_VALUE;
312 }
313 bitmap_set1(next_tables, id);
314 }
315 return 0;
316 }
317
318 static enum ofperr
319 parse_oxms(struct ofpbuf *payload, bool loose,
320 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
321 {
322 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
323 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
324
325 while (payload->size > 0) {
326 const struct mf_field *field;
327 enum ofperr error;
328 bool hasmask;
329
330 error = nx_pull_header(payload, NULL, &field, &hasmask);
331 if (!error) {
332 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
333 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
334 return error;
335 }
336 }
337 if (exactp) {
338 *exactp = exact;
339 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
340 return OFPERR_OFPBMC_BAD_MASK;
341 }
342 if (maskedp) {
343 *maskedp = masked;
344 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
345 return OFPERR_OFPBMC_BAD_MASK;
346 }
347 return 0;
348 }
349
350 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
351 * ofputil_table_features in 'tf'.
352 *
353 * If 'raw_properties' is nonnull, this function ignores properties and values
354 * that it does not understand, as a controller would want to do when
355 * interpreting capabilities provided by a switch. In this mode, on success,
356 * it initializes 'raw_properties' to contain the properties that were parsed;
357 * this allows the caller to later re-serialize the same properties without
358 * change.
359 *
360 * If 'raw_properties' is null, this function treats unknown properties and
361 * values as an error, as a switch would want to do when interpreting a
362 * configuration request made by a controller.
363 *
364 * A single OpenFlow message can specify features for multiple tables. Calling
365 * this function multiple times for a single 'msg' iterates through the tables
366 * in the message. The caller must initially leave 'msg''s layer pointers null
367 * and not modify them between calls.
368 *
369 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
370 * a positive "enum ofperr" value. */
371 int
372 ofputil_decode_table_features(struct ofpbuf *msg,
373 struct ofputil_table_features *tf,
374 struct ofpbuf *raw_properties)
375 {
376 bool loose = raw_properties != NULL;
377
378 memset(tf, 0, sizeof *tf);
379
380 if (!msg->header) {
381 ofpraw_pull_assert(msg);
382 }
383
384 if (!msg->size) {
385 return EOF;
386 }
387
388 const struct ofp_header *oh = msg->header;
389 struct ofp13_table_features *otf = msg->data;
390 if (msg->size < sizeof *otf) {
391 return OFPERR_OFPBPC_BAD_LEN;
392 }
393
394 unsigned int len = ntohs(otf->length);
395 if (len < sizeof *otf || len % 8 || len > msg->size) {
396 return OFPERR_OFPBPC_BAD_LEN;
397 }
398
399 if (oh->version >= OFP15_VERSION) {
400 if (!ofp15_table_features_command_is_valid(otf->command)) {
401 return OFPERR_OFPTFFC_BAD_COMMAND;
402 }
403 tf->command = otf->command;
404 } else {
405 tf->command = OFPTFC15_REPLACE;
406 }
407
408 tf->table_id = otf->table_id;
409 if (tf->table_id == OFPTT_ALL) {
410 return OFPERR_OFPTFFC_BAD_TABLE;
411 }
412
413 ovs_strlcpy_arrays(tf->name, otf->name);
414 tf->metadata_match = otf->metadata_match;
415 tf->metadata_write = otf->metadata_write;
416 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
417 if (oh->version >= OFP14_VERSION) {
418 uint32_t caps = ntohl(otf->capabilities);
419 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
420 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
421 } else {
422 tf->supports_eviction = -1;
423 tf->supports_vacancy_events = -1;
424 }
425 tf->max_entries = ntohl(otf->max_entries);
426
427 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
428 len);
429 ofpbuf_pull(&properties, sizeof *otf);
430 tf->any_properties = properties.size > 0;
431 if (raw_properties) {
432 *raw_properties = properties;
433 }
434 uint32_t seen = 0;
435 while (properties.size > 0) {
436 struct ofpbuf payload;
437 enum ofperr error;
438 uint64_t type;
439
440 error = pull_table_feature_property(&properties, &payload, &type);
441 if (error) {
442 return error;
443 }
444
445 if (type < 32) {
446 uint32_t bit = 1u << type;
447 if (seen & bit) {
448 return OFPERR_OFPTFFC_BAD_FEATURES;
449 }
450 seen |= bit;
451 }
452
453 switch ((enum ofp13_table_feature_prop_type) type) {
454 case OFPTFPT13_INSTRUCTIONS:
455 error = parse_instruction_ids(&payload, loose,
456 &tf->nonmiss.instructions);
457 break;
458 case OFPTFPT13_INSTRUCTIONS_MISS:
459 error = parse_instruction_ids(&payload, loose,
460 &tf->miss.instructions);
461 break;
462
463 case OFPTFPT13_NEXT_TABLES:
464 error = parse_table_features_next_table(&payload,
465 tf->nonmiss.next);
466 break;
467 case OFPTFPT13_NEXT_TABLES_MISS:
468 error = parse_table_features_next_table(&payload, tf->miss.next);
469 break;
470
471 case OFPTFPT13_WRITE_ACTIONS:
472 error = parse_action_bitmap(&payload, oh->version,
473 &tf->nonmiss.write.ofpacts);
474 break;
475 case OFPTFPT13_WRITE_ACTIONS_MISS:
476 error = parse_action_bitmap(&payload, oh->version,
477 &tf->miss.write.ofpacts);
478 break;
479
480 case OFPTFPT13_APPLY_ACTIONS:
481 error = parse_action_bitmap(&payload, oh->version,
482 &tf->nonmiss.apply.ofpacts);
483 break;
484 case OFPTFPT13_APPLY_ACTIONS_MISS:
485 error = parse_action_bitmap(&payload, oh->version,
486 &tf->miss.apply.ofpacts);
487 break;
488
489 case OFPTFPT13_MATCH:
490 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
491 break;
492 case OFPTFPT13_WILDCARDS:
493 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
494 break;
495
496 case OFPTFPT13_WRITE_SETFIELD:
497 error = parse_oxms(&payload, loose,
498 &tf->nonmiss.write.set_fields, NULL);
499 break;
500 case OFPTFPT13_WRITE_SETFIELD_MISS:
501 error = parse_oxms(&payload, loose,
502 &tf->miss.write.set_fields, NULL);
503 break;
504 case OFPTFPT13_APPLY_SETFIELD:
505 error = parse_oxms(&payload, loose,
506 &tf->nonmiss.apply.set_fields, NULL);
507 break;
508 case OFPTFPT13_APPLY_SETFIELD_MISS:
509 error = parse_oxms(&payload, loose,
510 &tf->miss.apply.set_fields, NULL);
511 break;
512
513 case OFPTFPT13_EXPERIMENTER:
514 case OFPTFPT13_EXPERIMENTER_MISS:
515 default:
516 error = OFPPROP_UNKNOWN(loose, "table features", type);
517 break;
518 }
519 if (error) {
520 return error;
521 }
522 }
523
524 /* OpenFlow 1.3 and 1.4 always require all of the required properties.
525 * OpenFlow 1.5 requires all of them if any property is present. */
526 if ((seen & OFPTFPT13_REQUIRED) != OFPTFPT13_REQUIRED
527 && (tf->any_properties || oh->version < OFP15_VERSION)) {
528 VLOG_WARN_RL(&rl, "table features message missing required property");
529 return OFPERR_OFPTFFC_BAD_FEATURES;
530 }
531
532 /* Copy nonmiss to miss when appropriate. */
533 if (tf->any_properties) {
534 if (!(seen & (1u << OFPTFPT13_INSTRUCTIONS_MISS))) {
535 tf->miss.instructions = tf->nonmiss.instructions;
536 }
537 if (!(seen & (1u << OFPTFPT13_NEXT_TABLES_MISS))) {
538 memcpy(tf->miss.next, tf->nonmiss.next, sizeof tf->miss.next);
539 }
540 if (!(seen & (1u << OFPTFPT13_WRITE_ACTIONS_MISS))) {
541 tf->miss.write.ofpacts = tf->nonmiss.write.ofpacts;
542 }
543 if (!(seen & (1u << OFPTFPT13_APPLY_ACTIONS_MISS))) {
544 tf->miss.apply.ofpacts = tf->nonmiss.apply.ofpacts;
545 }
546 if (!(seen & (1u << OFPTFPT13_WRITE_SETFIELD_MISS))) {
547 tf->miss.write.set_fields = tf->nonmiss.write.set_fields;
548 }
549 if (!(seen & (1u << OFPTFPT13_APPLY_SETFIELD_MISS))) {
550 tf->miss.apply.set_fields = tf->nonmiss.apply.set_fields;
551 }
552 }
553
554 /* Fix inconsistencies:
555 *
556 * - Turn on 'match' bits that are set in 'mask', because maskable
557 * fields are matchable.
558 *
559 * - Turn on 'wildcard' bits that are set in 'mask', because a field
560 * that is arbitrarily maskable can be wildcarded entirely.
561 *
562 * - Turn off 'wildcard' bits that are not in 'match', because a field
563 * must be matchable for it to be meaningfully wildcarded. */
564 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
565 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
566 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
567
568 return 0;
569 }
570
571 /* Encodes and returns a request to obtain the table features of a switch.
572 * The message is encoded for OpenFlow version 'ofp_version'. */
573 struct ofpbuf *
574 ofputil_encode_table_features_request(enum ofp_version ofp_version)
575 {
576 struct ofpbuf *request = NULL;
577
578 switch (ofp_version) {
579 case OFP10_VERSION:
580 case OFP11_VERSION:
581 case OFP12_VERSION:
582 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
583 "(\'-O OpenFlow13\')");
584 case OFP13_VERSION:
585 case OFP14_VERSION:
586 case OFP15_VERSION:
587 case OFP16_VERSION:
588 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
589 ofp_version, 0);
590 break;
591 default:
592 OVS_NOT_REACHED();
593 }
594
595 return request;
596 }
597
598 static void
599 put_fields_property(struct ofpbuf *reply,
600 const struct mf_bitmap *fields,
601 const struct mf_bitmap *masks,
602 enum ofp13_table_feature_prop_type property,
603 enum ofp_version version)
604 {
605 size_t start_ofs;
606 int field;
607
608 start_ofs = ofpprop_start(reply, property);
609 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
610 nx_put_header(reply, field, version,
611 masks && bitmap_is_set(masks->bm, field));
612 }
613 ofpprop_end(reply, start_ofs);
614 }
615
616 static void
617 put_table_action_features(struct ofpbuf *reply,
618 const struct ofputil_table_action_features *taf,
619 enum ofp13_table_feature_prop_type actions_type,
620 enum ofp13_table_feature_prop_type set_fields_type,
621 int miss_offset, enum ofp_version version)
622 {
623 ofpprop_put_bitmap(reply, actions_type + miss_offset,
624 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
625 version)));
626 put_fields_property(reply, &taf->set_fields, NULL,
627 set_fields_type + miss_offset, version);
628 }
629
630 static void
631 put_table_instruction_features(
632 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
633 int miss_offset, enum ofp_version version)
634 {
635 size_t start_ofs;
636 uint8_t table_id;
637
638 ofpprop_put_bitmap(reply, OFPTFPT13_INSTRUCTIONS + miss_offset,
639 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
640 version)));
641
642 start_ofs = ofpprop_start(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
643 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
644 ofpbuf_put(reply, &table_id, 1);
645 }
646 ofpprop_end(reply, start_ofs);
647
648 put_table_action_features(reply, &tif->write,
649 OFPTFPT13_WRITE_ACTIONS,
650 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
651 put_table_action_features(reply, &tif->apply,
652 OFPTFPT13_APPLY_ACTIONS,
653 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
654 }
655
656 /* Appends a table features record to 'msgs', which must be a
657 * OFPT_TABLE_FEATURES request or reply. If 'raw_properties' is nonnull, then
658 * it uses its contents verbatim as the table features properties, ignoring the
659 * corresponding members of 'tf'. */
660 void
661 ofputil_append_table_features(const struct ofputil_table_features *tf,
662 const struct ofpbuf *raw_properties,
663 struct ovs_list *msgs)
664 {
665 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(msgs));
666 enum ofp_version version = ofpmp_version(msgs);
667 size_t start_ofs = msg->size;
668 struct ofp13_table_features *otf;
669
670 otf = ofpbuf_put_zeros(msg, sizeof *otf);
671 otf->table_id = tf->table_id;
672 otf->command = version >= OFP15_VERSION ? tf->command : 0;
673 ovs_strlcpy_arrays(otf->name, tf->name);
674 otf->metadata_match = tf->metadata_match;
675 otf->metadata_write = tf->metadata_write;
676 if (version >= OFP14_VERSION) {
677 if (tf->supports_eviction) {
678 otf->capabilities |= htonl(OFPTC14_EVICTION);
679 }
680 if (tf->supports_vacancy_events) {
681 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
682 }
683 }
684 otf->max_entries = htonl(tf->max_entries);
685
686 if (raw_properties) {
687 ofpbuf_put(msg, raw_properties->data, raw_properties->size);
688 } else if (tf->any_properties) {
689 put_table_instruction_features(msg, &tf->nonmiss, 0, version);
690 put_table_instruction_features(msg, &tf->miss, 1, version);
691
692 put_fields_property(msg, &tf->match, &tf->mask,
693 OFPTFPT13_MATCH, version);
694 put_fields_property(msg, &tf->wildcard, NULL,
695 OFPTFPT13_WILDCARDS, version);
696 }
697
698 otf = ofpbuf_at_assert(msg, start_ofs, sizeof *otf);
699 otf->length = htons(msg->size - start_ofs);
700 ofpmp_postappend(msgs, start_ofs);
701 }
702
703 static enum ofperr
704 parse_table_desc_vacancy_property(struct ofpbuf *property,
705 struct ofputil_table_desc *td)
706 {
707 struct ofp14_table_mod_prop_vacancy *otv = property->data;
708
709 if (property->size != sizeof *otv) {
710 return OFPERR_OFPBPC_BAD_LEN;
711 }
712
713 td->table_vacancy.vacancy_down = otv->vacancy_down;
714 td->table_vacancy.vacancy_up = otv->vacancy_up;
715 td->table_vacancy.vacancy = otv->vacancy;
716 return 0;
717 }
718
719 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
720 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
721 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
722 * value. */
723 int
724 ofputil_decode_table_desc(struct ofpbuf *msg,
725 struct ofputil_table_desc *td,
726 enum ofp_version version)
727 {
728 memset(td, 0, sizeof *td);
729
730 if (!msg->header) {
731 ofpraw_pull_assert(msg);
732 }
733
734 if (!msg->size) {
735 return EOF;
736 }
737
738 struct ofp14_table_desc *otd = ofpbuf_try_pull(msg, sizeof *otd);
739 if (!otd) {
740 VLOG_WARN_RL(&rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
741 "leftover bytes at end", msg->size);
742 return OFPERR_OFPBRC_BAD_LEN;
743 }
744
745 td->table_id = otd->table_id;
746 size_t length = ntohs(otd->length);
747 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
748 VLOG_WARN_RL(&rl, "OFP14_TABLE_DESC reply claims invalid "
749 "length %"PRIuSIZE, length);
750 return OFPERR_OFPBRC_BAD_LEN;
751 }
752 length -= sizeof *otd;
753
754 td->eviction = ofputil_decode_table_eviction(otd->config, version);
755 td->vacancy = ofputil_decode_table_vacancy(otd->config, version);
756 td->eviction_flags = UINT32_MAX;
757
758 struct ofpbuf properties = ofpbuf_const_initializer(
759 ofpbuf_pull(msg, length), length);
760 while (properties.size > 0) {
761 struct ofpbuf payload;
762 enum ofperr error;
763 uint64_t type;
764
765 error = ofpprop_pull(&properties, &payload, &type);
766 if (error) {
767 return error;
768 }
769
770 switch (type) {
771 case OFPTMPT14_EVICTION:
772 error = ofpprop_parse_u32(&payload, &td->eviction_flags);
773 break;
774
775 case OFPTMPT14_VACANCY:
776 error = parse_table_desc_vacancy_property(&payload, td);
777 break;
778
779 default:
780 error = OFPPROP_UNKNOWN(true, "table_desc", type);
781 break;
782 }
783
784 if (error) {
785 return error;
786 }
787 }
788
789 return 0;
790 }
791
792 /* Encodes and returns a request to obtain description of tables of a switch.
793 * The message is encoded for OpenFlow version 'ofp_version'. */
794 struct ofpbuf *
795 ofputil_encode_table_desc_request(enum ofp_version ofp_version)
796 {
797 struct ofpbuf *request = NULL;
798
799 if (ofp_version >= OFP14_VERSION) {
800 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
801 ofp_version, 0);
802 } else {
803 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
804 "(\'-O OpenFlow14\')");
805 }
806
807 return request;
808 }
809
810 /* Function to append Table desc information in a reply list. */
811 void
812 ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
813 struct ovs_list *replies,
814 enum ofp_version version)
815 {
816 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
817 size_t start_otd;
818 struct ofp14_table_desc *otd;
819
820 start_otd = reply->size;
821 ofpbuf_put_zeros(reply, sizeof *otd);
822 if (td->eviction_flags != UINT32_MAX) {
823 ofpprop_put_u32(reply, OFPTMPT14_EVICTION, td->eviction_flags);
824 }
825 if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
826 struct ofp14_table_mod_prop_vacancy *otv;
827
828 otv = ofpprop_put_zeros(reply, OFPTMPT14_VACANCY, sizeof *otv);
829 otv->vacancy_down = td->table_vacancy.vacancy_down;
830 otv->vacancy_up = td->table_vacancy.vacancy_up;
831 otv->vacancy = td->table_vacancy.vacancy;
832 }
833
834 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
835 otd->length = htons(reply->size - start_otd);
836 otd->table_id = td->table_id;
837 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
838 td->eviction, td->vacancy,
839 version);
840 ofpmp_postappend(replies, start_otd);
841 }
842
843 static const char *
844 ofputil_eviction_flag_to_string(uint32_t bit)
845 {
846 enum ofp14_table_mod_prop_eviction_flag eviction_flag = bit;
847
848 switch (eviction_flag) {
849 case OFPTMPEF14_OTHER: return "OTHER";
850 case OFPTMPEF14_IMPORTANCE: return "IMPORTANCE";
851 case OFPTMPEF14_LIFETIME: return "LIFETIME";
852 }
853
854 return NULL;
855 }
856
857 /* Appends to 'string' a description of the bitmap of OFPTMPEF14_* values in
858 * 'eviction_flags'. */
859 static void
860 ofputil_put_eviction_flags(struct ds *string, uint32_t eviction_flags)
861 {
862 if (eviction_flags != UINT32_MAX) {
863 ofp_print_bit_names(string, eviction_flags,
864 ofputil_eviction_flag_to_string, '|');
865 } else {
866 ds_put_cstr(string, "(default)");
867 }
868 }
869
870 void
871 ofputil_table_desc_format(struct ds *s, const struct ofputil_table_desc *td,
872 const struct ofputil_table_map *table_map)
873 {
874 ds_put_format(s, "\n table ");
875 ofputil_format_table(td->table_id, table_map, s);
876 ds_put_cstr(s, ":\n");
877 ds_put_format(s, " eviction=%s eviction_flags=",
878 ofputil_table_eviction_to_string(td->eviction));
879 ofputil_put_eviction_flags(s, td->eviction_flags);
880 ds_put_char(s, '\n');
881 ds_put_format(s, " vacancy=%s",
882 ofputil_table_vacancy_to_string(td->vacancy));
883 if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
884 ds_put_format(s, " vacancy_down=%"PRIu8"%%",
885 td->table_vacancy.vacancy_down);
886 ds_put_format(s, " vacancy_up=%"PRIu8"%%",
887 td->table_vacancy.vacancy_up);
888 ds_put_format(s, " vacancy=%"PRIu8"%%",
889 td->table_vacancy.vacancy);
890 }
891 ds_put_char(s, '\n');
892 }
893
894 /* This function parses Vacancy property, and decodes the
895 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
896 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
897 * greater than vacancy_up and also when current vacancy has non-zero
898 * value. Returns 0 on success. */
899 static enum ofperr
900 parse_table_mod_vacancy_property(struct ofpbuf *property,
901 struct ofputil_table_mod *tm)
902 {
903 struct ofp14_table_mod_prop_vacancy *otv = property->data;
904
905 if (property->size != sizeof *otv) {
906 return OFPERR_OFPBPC_BAD_LEN;
907 }
908 tm->table_vacancy.vacancy_down = otv->vacancy_down;
909 tm->table_vacancy.vacancy_up = otv->vacancy_up;
910 if (tm->table_vacancy.vacancy_down > tm->table_vacancy.vacancy_up) {
911 OFPPROP_LOG(&rl, false,
912 "Value of vacancy_down is greater than vacancy_up");
913 return OFPERR_OFPBPC_BAD_VALUE;
914 }
915 if (tm->table_vacancy.vacancy_down > 100 ||
916 tm->table_vacancy.vacancy_up > 100) {
917 OFPPROP_LOG(&rl, false, "Vacancy threshold percentage "
918 "should not be greater than 100");
919 return OFPERR_OFPBPC_BAD_VALUE;
920 }
921 tm->table_vacancy.vacancy = otv->vacancy;
922 if (tm->table_vacancy.vacancy) {
923 OFPPROP_LOG(&rl, false,
924 "Vacancy value should be zero for table-mod messages");
925 return OFPERR_OFPBPC_BAD_VALUE;
926 }
927 return 0;
928 }
929
930 /* Given 'config', taken from an OpenFlow 'version' message that specifies
931 * table configuration (a table mod, table stats, or table features message),
932 * returns the table vacancy configuration that it specifies.
933 *
934 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
935 * so for other 'version' this function always returns
936 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
937 static enum ofputil_table_vacancy
938 ofputil_decode_table_vacancy(ovs_be32 config, enum ofp_version version)
939 {
940 return (version < OFP14_VERSION ? OFPUTIL_TABLE_VACANCY_DEFAULT
941 : config & htonl(OFPTC14_VACANCY_EVENTS) ? OFPUTIL_TABLE_VACANCY_ON
942 : OFPUTIL_TABLE_VACANCY_OFF);
943 }
944
945 /* Given 'config', taken from an OpenFlow 'version' message that specifies
946 * table configuration (a table mod, table stats, or table features message),
947 * returns the table eviction configuration that it specifies.
948 *
949 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
950 * so for other 'version' values this function always returns
951 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
952 static enum ofputil_table_eviction
953 ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
954 {
955 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
956 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
957 : OFPUTIL_TABLE_EVICTION_OFF);
958 }
959
960 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
961 * OpenFlow messages of the given 'version', based on the provided 'miss' and
962 * 'eviction' values. */
963 static ovs_be32
964 ofputil_encode_table_config(enum ofputil_table_miss miss,
965 enum ofputil_table_eviction eviction,
966 enum ofputil_table_vacancy vacancy,
967 enum ofp_version version)
968 {
969 uint32_t config = 0;
970 /* Search for "OFPTC_* Table Configuration" in the documentation for more
971 * information on the crazy evolution of this field. */
972 switch (version) {
973 case OFP10_VERSION:
974 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
975 return htonl(0);
976
977 case OFP11_VERSION:
978 case OFP12_VERSION:
979 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
980 switch (miss) {
981 case OFPUTIL_TABLE_MISS_DEFAULT:
982 /* Really this shouldn't be used for encoding (the caller should
983 * provide a specific value) but I can't imagine that defaulting to
984 * the fall-through case here will hurt. */
985 case OFPUTIL_TABLE_MISS_CONTROLLER:
986 default:
987 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
988 case OFPUTIL_TABLE_MISS_CONTINUE:
989 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
990 case OFPUTIL_TABLE_MISS_DROP:
991 return htonl(OFPTC11_TABLE_MISS_DROP);
992 }
993 OVS_NOT_REACHED();
994
995 case OFP13_VERSION:
996 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
997 * flags, so this is correct. */
998 return htonl(0);
999
1000 case OFP14_VERSION:
1001 case OFP15_VERSION:
1002 case OFP16_VERSION:
1003 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
1004 * OFPTC14_VACANCY_EVENTS. */
1005 if (eviction == OFPUTIL_TABLE_EVICTION_ON) {
1006 config |= OFPTC14_EVICTION;
1007 }
1008 if (vacancy == OFPUTIL_TABLE_VACANCY_ON) {
1009 config |= OFPTC14_VACANCY_EVENTS;
1010 }
1011 return htonl(config);
1012 }
1013
1014 OVS_NOT_REACHED();
1015 }
1016
1017 /* Given 'config', taken from an OpenFlow 'version' message that specifies
1018 * table configuration (a table mod, table stats, or table features message),
1019 * returns the table miss configuration that it specifies.
1020 *
1021 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
1022 * other 'version' values this function always returns
1023 * OFPUTIL_TABLE_MISS_DEFAULT. */
1024 static enum ofputil_table_miss
1025 ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
1026 {
1027 uint32_t config = ntohl(config_);
1028
1029 if (version == OFP11_VERSION || version == OFP12_VERSION) {
1030 switch (config & OFPTC11_TABLE_MISS_MASK) {
1031 case OFPTC11_TABLE_MISS_CONTROLLER:
1032 return OFPUTIL_TABLE_MISS_CONTROLLER;
1033
1034 case OFPTC11_TABLE_MISS_CONTINUE:
1035 return OFPUTIL_TABLE_MISS_CONTINUE;
1036
1037 case OFPTC11_TABLE_MISS_DROP:
1038 return OFPUTIL_TABLE_MISS_DROP;
1039
1040 default:
1041 VLOG_WARN_RL(&rl, "bad table miss config %d", config);
1042 return OFPUTIL_TABLE_MISS_CONTROLLER;
1043 }
1044 } else {
1045 return OFPUTIL_TABLE_MISS_DEFAULT;
1046 }
1047 }
1048
1049 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
1050 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
1051 enum ofperr
1052 ofputil_decode_table_mod(const struct ofp_header *oh,
1053 struct ofputil_table_mod *pm)
1054 {
1055 memset(pm, 0, sizeof *pm);
1056 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
1057 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
1058 pm->eviction_flags = UINT32_MAX;
1059 pm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
1060
1061 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
1062 enum ofpraw raw = ofpraw_pull_assert(&b);
1063 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
1064 const struct ofp11_table_mod *otm = b.data;
1065
1066 pm->table_id = otm->table_id;
1067 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
1068 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
1069 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
1070
1071 pm->table_id = otm->table_id;
1072 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
1073 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
1074 pm->vacancy = ofputil_decode_table_vacancy(otm->config, oh->version);
1075 while (b.size > 0) {
1076 struct ofpbuf property;
1077 enum ofperr error;
1078 uint64_t type;
1079
1080 error = ofpprop_pull(&b, &property, &type);
1081 if (error) {
1082 return error;
1083 }
1084
1085 switch (type) {
1086 case OFPTMPT14_EVICTION:
1087 error = ofpprop_parse_u32(&property, &pm->eviction);
1088 break;
1089
1090 case OFPTMPT14_VACANCY:
1091 error = parse_table_mod_vacancy_property(&property, pm);
1092 break;
1093
1094 default:
1095 error = OFPERR_OFPBRC_BAD_TYPE;
1096 break;
1097 }
1098
1099 if (error) {
1100 return error;
1101 }
1102 }
1103 } else {
1104 return OFPERR_OFPBRC_BAD_TYPE;
1105 }
1106
1107 return 0;
1108 }
1109
1110 /* Converts the abstract form of a "table mod" message in '*tm' into an
1111 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
1112 * buffer owned by the caller. */
1113 struct ofpbuf *
1114 ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
1115 enum ofputil_protocol protocol)
1116 {
1117 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
1118 struct ofpbuf *b;
1119
1120 switch (ofp_version) {
1121 case OFP10_VERSION: {
1122 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
1123 "(\'-O OpenFlow11\')");
1124 break;
1125 }
1126 case OFP11_VERSION:
1127 case OFP12_VERSION:
1128 case OFP13_VERSION: {
1129 struct ofp11_table_mod *otm;
1130
1131 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
1132 otm = ofpbuf_put_zeros(b, sizeof *otm);
1133 otm->table_id = tm->table_id;
1134 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
1135 tm->vacancy, ofp_version);
1136 break;
1137 }
1138 case OFP14_VERSION:
1139 case OFP15_VERSION:
1140 case OFP16_VERSION: {
1141 struct ofp14_table_mod *otm;
1142
1143 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
1144 otm = ofpbuf_put_zeros(b, sizeof *otm);
1145 otm->table_id = tm->table_id;
1146 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
1147 tm->vacancy, ofp_version);
1148
1149 if (tm->eviction_flags != UINT32_MAX) {
1150 ofpprop_put_u32(b, OFPTMPT14_EVICTION, tm->eviction_flags);
1151 }
1152 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
1153 struct ofp14_table_mod_prop_vacancy *otv;
1154
1155 otv = ofpprop_put_zeros(b, OFPTMPT14_VACANCY, sizeof *otv);
1156 otv->vacancy_down = tm->table_vacancy.vacancy_down;
1157 otv->vacancy_up = tm->table_vacancy.vacancy_up;
1158 }
1159 break;
1160 }
1161 default:
1162 OVS_NOT_REACHED();
1163 }
1164
1165 return b;
1166 }
1167
1168 void
1169 ofputil_table_mod_format(struct ds *s, const struct ofputil_table_mod *tm,
1170 const struct ofputil_table_map *table_map)
1171 {
1172 if (tm->table_id == 0xff) {
1173 ds_put_cstr(s, " table_id: ALL_TABLES");
1174 } else {
1175 ds_put_format(s, " table_id=");
1176 ofputil_format_table(tm->table_id, table_map, s);
1177 }
1178
1179 if (tm->miss != OFPUTIL_TABLE_MISS_DEFAULT) {
1180 ds_put_format(s, ", flow_miss_config=%s",
1181 ofputil_table_miss_to_string(tm->miss));
1182 }
1183 if (tm->eviction != OFPUTIL_TABLE_EVICTION_DEFAULT) {
1184 ds_put_format(s, ", eviction=%s",
1185 ofputil_table_eviction_to_string(tm->eviction));
1186 }
1187 if (tm->eviction_flags != UINT32_MAX) {
1188 ds_put_cstr(s, "eviction_flags=");
1189 ofputil_put_eviction_flags(s, tm->eviction_flags);
1190 }
1191 if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) {
1192 ds_put_format(s, ", vacancy=%s",
1193 ofputil_table_vacancy_to_string(tm->vacancy));
1194 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
1195 ds_put_format(s, " vacancy:%"PRIu8""
1196 ",%"PRIu8"", tm->table_vacancy.vacancy_down,
1197 tm->table_vacancy.vacancy_up);
1198 }
1199 }
1200 }
1201
1202 /* Convert 'setting' (as described for the "mod-table" command
1203 * in ovs-ofctl man page) into 'tm->table_vacancy->vacancy_up' and
1204 * 'tm->table_vacancy->vacancy_down' threshold values.
1205 * For the two threshold values, value of vacancy_up is always greater
1206 * than value of vacancy_down.
1207 *
1208 * Returns NULL if successful, otherwise a malloc()'d string describing the
1209 * error. The caller is responsible for freeing the returned string. */
1210 static char * OVS_WARN_UNUSED_RESULT
1211 parse_ofp_table_vacancy(struct ofputil_table_mod *tm, const char *setting)
1212 {
1213 char *save_ptr = NULL;
1214 char *vac_up, *vac_down;
1215 char *value = xstrdup(setting);
1216 char *ret_msg;
1217 int vacancy_up, vacancy_down;
1218
1219 strtok_r(value, ":", &save_ptr);
1220 vac_down = strtok_r(NULL, ",", &save_ptr);
1221 if (!vac_down) {
1222 ret_msg = xasprintf("Vacancy down value missing");
1223 goto exit;
1224 }
1225 if (!str_to_int(vac_down, 0, &vacancy_down) ||
1226 vacancy_down < 0 || vacancy_down > 100) {
1227 ret_msg = xasprintf("Invalid vacancy down value \"%s\"", vac_down);
1228 goto exit;
1229 }
1230 vac_up = strtok_r(NULL, ",", &save_ptr);
1231 if (!vac_up) {
1232 ret_msg = xasprintf("Vacancy up value missing");
1233 goto exit;
1234 }
1235 if (!str_to_int(vac_up, 0, &vacancy_up) ||
1236 vacancy_up < 0 || vacancy_up > 100) {
1237 ret_msg = xasprintf("Invalid vacancy up value \"%s\"", vac_up);
1238 goto exit;
1239 }
1240 if (vacancy_down > vacancy_up) {
1241 ret_msg = xasprintf("Invalid vacancy range, vacancy up should be "
1242 "greater than vacancy down (%s)",
1243 ofperr_to_string(OFPERR_OFPBPC_BAD_VALUE));
1244 goto exit;
1245 }
1246
1247 free(value);
1248 tm->table_vacancy.vacancy_down = vacancy_down;
1249 tm->table_vacancy.vacancy_up = vacancy_up;
1250 return NULL;
1251
1252 exit:
1253 free(value);
1254 return ret_msg;
1255 }
1256
1257 /* Convert 'table_id' and 'setting' (as described for the "mod-table" command
1258 * in the ovs-ofctl man page) into 'tm' for sending a table_mod command to a
1259 * switch. If 'setting' sets the name of the table, puts the new name in
1260 * '*namep' (a suffix of 'setting'), otherwise stores NULL.
1261 *
1262 * Stores a bitmap of the OpenFlow versions that are usable for 'tm' into
1263 * '*usable_versions'.
1264 *
1265 * Returns NULL if successful, otherwise a malloc()'d string describing the
1266 * error. The caller is responsible for freeing the returned string. */
1267 char * OVS_WARN_UNUSED_RESULT
1268 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char **namep,
1269 const char *table_id, const char *setting,
1270 const struct ofputil_table_map *table_map,
1271 uint32_t *usable_versions)
1272 {
1273 *usable_versions = 0;
1274 *namep = NULL;
1275 if (!strcasecmp(table_id, "all")) {
1276 tm->table_id = OFPTT_ALL;
1277 } else if (!ofputil_table_from_string(table_id, table_map,
1278 &tm->table_id)) {
1279 return xasprintf("unknown table \"%s\"", table_id);
1280 }
1281
1282 tm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
1283 tm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
1284 tm->eviction_flags = UINT32_MAX;
1285 tm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
1286 tm->table_vacancy.vacancy_down = 0;
1287 tm->table_vacancy.vacancy_up = 0;
1288 tm->table_vacancy.vacancy = 0;
1289 /* Only OpenFlow 1.1 and 1.2 can configure table-miss via table_mod.
1290 * Only OpenFlow 1.4+ can configure eviction and vacancy events
1291 * via table_mod.
1292 */
1293 if (!strcmp(setting, "controller")) {
1294 tm->miss = OFPUTIL_TABLE_MISS_CONTROLLER;
1295 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
1296 } else if (!strcmp(setting, "continue")) {
1297 tm->miss = OFPUTIL_TABLE_MISS_CONTINUE;
1298 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
1299 } else if (!strcmp(setting, "drop")) {
1300 tm->miss = OFPUTIL_TABLE_MISS_DROP;
1301 *usable_versions = (1u << OFP11_VERSION) | (1u << OFP12_VERSION);
1302 } else if (!strcmp(setting, "evict")) {
1303 tm->eviction = OFPUTIL_TABLE_EVICTION_ON;
1304 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
1305 } else if (!strcmp(setting, "noevict")) {
1306 tm->eviction = OFPUTIL_TABLE_EVICTION_OFF;
1307 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
1308 } else if (!strncmp(setting, "vacancy", strcspn(setting, ":"))) {
1309 tm->vacancy = OFPUTIL_TABLE_VACANCY_ON;
1310 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
1311 char *error = parse_ofp_table_vacancy(tm, setting);
1312 if (error) {
1313 return error;
1314 }
1315 } else if (!strcmp(setting, "novacancy")) {
1316 tm->vacancy = OFPUTIL_TABLE_VACANCY_OFF;
1317 *usable_versions = (1 << OFP14_VERSION) | (1u << OFP15_VERSION);
1318 } else if (tm->table_id != OFPTT_ALL && !strncmp(setting, "name:", 5)) {
1319 *namep = setting + 5;
1320 *usable_versions = ((1 << OFP13_VERSION) | (1u << OFP14_VERSION)
1321 | (1u << OFP15_VERSION));
1322 } else {
1323 return xasprintf("invalid table_mod setting %s", setting);
1324 }
1325
1326 if (tm->table_id == 0xfe
1327 && tm->miss == OFPUTIL_TABLE_MISS_CONTINUE) {
1328 return xstrdup("last table's flow miss handling can not be continue");
1329 }
1330
1331 return NULL;
1332 }
1333
1334 static void
1335 print_table_action_features(struct ds *s,
1336 const struct ofputil_table_action_features *taf)
1337 {
1338 if (taf->ofpacts) {
1339 ds_put_cstr(s, " actions: ");
1340 ofpact_bitmap_format(taf->ofpacts, s);
1341 ds_put_char(s, '\n');
1342 }
1343
1344 if (!bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS)) {
1345 int i;
1346
1347 ds_put_cstr(s, " supported on Set-Field:");
1348 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, taf->set_fields.bm) {
1349 ds_put_format(s, " %s", mf_from_id(i)->name);
1350 }
1351 ds_put_char(s, '\n');
1352 }
1353 }
1354
1355 static bool
1356 table_action_features_equal(const struct ofputil_table_action_features *a,
1357 const struct ofputil_table_action_features *b)
1358 {
1359 return (a->ofpacts == b->ofpacts
1360 && bitmap_equal(a->set_fields.bm, b->set_fields.bm, MFF_N_IDS));
1361 }
1362
1363 static bool
1364 table_action_features_empty(const struct ofputil_table_action_features *taf)
1365 {
1366 return !taf->ofpacts && bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS);
1367 }
1368
1369 static void
1370 print_table_instruction_features(
1371 struct ds *s,
1372 const struct ofputil_table_instruction_features *tif,
1373 const struct ofputil_table_instruction_features *prev_tif)
1374 {
1375 int start, end;
1376
1377 if (!bitmap_is_all_zeros(tif->next, 255)) {
1378 ds_put_cstr(s, " next tables: ");
1379 for (start = bitmap_scan(tif->next, 1, 0, 255); start < 255;
1380 start = bitmap_scan(tif->next, 1, end, 255)) {
1381 end = bitmap_scan(tif->next, 0, start + 1, 255);
1382 if (end == start + 1) {
1383 ds_put_format(s, "%d,", start);
1384 } else {
1385 ds_put_format(s, "%d-%d,", start, end - 1);
1386 }
1387 }
1388 ds_chomp(s, ',');
1389 if (ds_last(s) == ' ') {
1390 ds_put_cstr(s, "none");
1391 }
1392 ds_put_char(s, '\n');
1393 }
1394
1395 if (tif->instructions) {
1396 if (prev_tif && tif->instructions == prev_tif->instructions) {
1397 ds_put_cstr(s, " (same instructions)\n");
1398 } else {
1399 ds_put_cstr(s, " instructions: ");
1400 int i;
1401
1402 for (i = 0; i < 32; i++) {
1403 if (tif->instructions & (1u << i)) {
1404 const char *name = ovs_instruction_name_from_type(i);
1405 if (name) {
1406 ds_put_cstr(s, name);
1407 } else {
1408 ds_put_format(s, "%d", i);
1409 }
1410 ds_put_char(s, ',');
1411 }
1412 }
1413 ds_chomp(s, ',');
1414 ds_put_char(s, '\n');
1415 }
1416 }
1417
1418 if (prev_tif
1419 && table_action_features_equal(&tif->write, &prev_tif->write)
1420 && table_action_features_equal(&tif->apply, &prev_tif->apply)
1421 && !bitmap_is_all_zeros(tif->write.set_fields.bm, MFF_N_IDS)) {
1422 ds_put_cstr(s, " (same actions)\n");
1423 } else if (!table_action_features_equal(&tif->write, &tif->apply)) {
1424 ds_put_cstr(s, " Write-Actions features:\n");
1425 print_table_action_features(s, &tif->write);
1426 ds_put_cstr(s, " Apply-Actions features:\n");
1427 print_table_action_features(s, &tif->apply);
1428 } else if (tif->write.ofpacts
1429 || !bitmap_is_all_zeros(tif->write.set_fields.bm, MFF_N_IDS)) {
1430 ds_put_cstr(s, " Write-Actions and Apply-Actions features:\n");
1431 print_table_action_features(s, &tif->write);
1432 }
1433 }
1434
1435 /* Compares bitmaps of next tables 'a' and 'b', for tables 'a_table_id' and
1436 * 'b_table_id', respectively. Returns true if the bitmaps are equal.
1437 *
1438 * The bitmaps are considered equal if b_table_id == a_table_id + 1 and the bit
1439 * for 'b_table_id' is set in 'a' but not in 'b'. This is because OpenFlow
1440 * requires that a table not be able to do a goto_table back to its own table
1441 * or an earlier one. Without considering these equivalent, every table will
1442 * be different from every one in some way, which just isn't useful in printing
1443 * table features. */
1444 static bool
1445 table_instruction_features_next_equal(const unsigned long *a, int a_table_id,
1446 const unsigned long *b, int b_table_id)
1447 {
1448 if (b_table_id == a_table_id + 1
1449 && bitmap_is_set(a, b_table_id)
1450 && !bitmap_is_set(b, b_table_id)) {
1451 for (size_t i = 0; i < BITMAP_N_LONGS(255); i++) {
1452 unsigned long diff = a[i] ^ b[i];
1453 if (i == b_table_id / BITMAP_ULONG_BITS) {
1454 diff &= ~bitmap_bit__(b_table_id);
1455 }
1456 if (diff) {
1457 return false;
1458 }
1459 }
1460 return true;
1461 } else if (a_table_id == b_table_id + 1) {
1462 return table_instruction_features_next_equal(b, b_table_id,
1463 a, a_table_id);
1464 } else {
1465 return bitmap_equal(a, b, 255);
1466 }
1467 }
1468
1469 static bool
1470 table_instruction_features_equal(
1471 const struct ofputil_table_instruction_features *a, int a_table_id,
1472 const struct ofputil_table_instruction_features *b, int b_table_id)
1473 {
1474 return (table_instruction_features_next_equal(a->next, a_table_id,
1475 b->next, b_table_id)
1476 && a->instructions == b->instructions
1477 && table_action_features_equal(&a->write, &b->write)
1478 && table_action_features_equal(&a->apply, &b->apply));
1479 }
1480
1481 static bool
1482 table_instruction_features_empty(
1483 const struct ofputil_table_instruction_features *tif)
1484 {
1485 return (bitmap_is_all_zeros(tif->next, 255)
1486 && !tif->instructions
1487 && table_action_features_empty(&tif->write)
1488 && table_action_features_empty(&tif->apply));
1489 }
1490
1491 static bool
1492 table_features_equal(const struct ofputil_table_features *a,
1493 const struct ofputil_table_features *b)
1494 {
1495 return (a->metadata_match == b->metadata_match
1496 && a->metadata_write == b->metadata_write
1497 && a->miss_config == b->miss_config
1498 && a->supports_eviction == b->supports_eviction
1499 && a->supports_vacancy_events == b->supports_vacancy_events
1500 && a->max_entries == b->max_entries
1501 && table_instruction_features_equal(&a->nonmiss, a->table_id,
1502 &b->nonmiss, b->table_id)
1503 && table_instruction_features_equal(&a->miss, a->table_id,
1504 &b->miss, b->table_id)
1505 && bitmap_equal(a->match.bm, b->match.bm, MFF_N_IDS));
1506 }
1507
1508 static bool
1509 table_features_empty(const struct ofputil_table_features *tf)
1510 {
1511 return (!tf->metadata_match
1512 && !tf->metadata_write
1513 && tf->miss_config == OFPUTIL_TABLE_MISS_DEFAULT
1514 && tf->supports_eviction < 0
1515 && tf->supports_vacancy_events < 0
1516 && !tf->max_entries
1517 && table_instruction_features_empty(&tf->nonmiss)
1518 && table_instruction_features_empty(&tf->miss)
1519 && bitmap_is_all_zeros(tf->match.bm, MFF_N_IDS));
1520 }
1521
1522 static bool
1523 table_stats_equal(const struct ofputil_table_stats *a,
1524 const struct ofputil_table_stats *b)
1525 {
1526 return (a->active_count == b->active_count
1527 && a->lookup_count == b->lookup_count
1528 && a->matched_count == b->matched_count);
1529 }
1530
1531 void
1532 ofputil_table_features_format(
1533 struct ds *s,
1534 const struct ofputil_table_features *features,
1535 const struct ofputil_table_features *prev_features,
1536 const struct ofputil_table_stats *stats,
1537 const struct ofputil_table_stats *prev_stats,
1538 int *first_ditto, int *last_ditto)
1539 {
1540 if (!prev_features && features->command != OFPTFC15_REPLACE) {
1541 ds_put_format(s, "\n command: %s",
1542 ofp15_table_features_command_to_string(
1543 features->command));
1544 }
1545
1546 int table = features->table_id;
1547 int prev_table = prev_features ? prev_features->table_id : 0;
1548
1549 bool same_stats = !stats || (prev_stats
1550 && table_stats_equal(stats, prev_stats));
1551 bool same_features = prev_features && table_features_equal(features,
1552 prev_features);
1553 if (same_stats && same_features && !features->name[0]) {
1554 if (*first_ditto < 0) {
1555 *first_ditto = table;
1556 }
1557 *last_ditto = table;
1558 return;
1559 }
1560 ofputil_table_features_format_finish(s, *first_ditto, *last_ditto);
1561 *first_ditto = -1;
1562
1563 ds_put_format(s, "\n table %d", table);
1564 if (features->name[0]) {
1565 ds_put_format(s, " (\"%s\")", features->name);
1566 }
1567 ds_put_char(s, ':');
1568
1569 if (same_stats && same_features) {
1570 ds_put_cstr(s, " ditto");
1571 return;
1572 }
1573 ds_put_char(s, '\n');
1574 if (stats) {
1575 ds_put_format(s, " active=%"PRIu32", ", stats->active_count);
1576 ds_put_format(s, "lookup=%"PRIu64", ", stats->lookup_count);
1577 ds_put_format(s, "matched=%"PRIu64"\n", stats->matched_count);
1578 }
1579 if (same_features) {
1580 if (!table_features_empty(features)) {
1581 ds_put_cstr(s, " (same features)\n");
1582 }
1583 return;
1584 }
1585 if (features->metadata_match || features->metadata_write) {
1586 ds_put_format(s, " metadata: match=%#"PRIx64" write=%#"PRIx64"\n",
1587 ntohll(features->metadata_match),
1588 ntohll(features->metadata_write));
1589 }
1590
1591 if (features->miss_config != OFPUTIL_TABLE_MISS_DEFAULT) {
1592 ds_put_format(s, " config=%s\n",
1593 ofputil_table_miss_to_string(features->miss_config));
1594 }
1595
1596 if (features->supports_eviction >= 0) {
1597 ds_put_format(s, " eviction: %ssupported\n",
1598 features->supports_eviction ? "" : "not ");
1599
1600 }
1601 if (features->supports_vacancy_events >= 0) {
1602 ds_put_format(s, " vacancy events: %ssupported\n",
1603 features->supports_vacancy_events ? "" : "not ");
1604
1605 }
1606
1607 if (features->max_entries) {
1608 ds_put_format(s, " max_entries=%"PRIu32"\n", features->max_entries);
1609 }
1610
1611 const struct ofputil_table_instruction_features *prev_nonmiss
1612 = prev_features ? &prev_features->nonmiss : NULL;
1613 const struct ofputil_table_instruction_features *prev_miss
1614 = prev_features ? &prev_features->miss : NULL;
1615 if (prev_features
1616 && table_instruction_features_equal(&features->nonmiss, table,
1617 prev_nonmiss, prev_table)
1618 && table_instruction_features_equal(&features->miss, table,
1619 prev_miss, prev_table)) {
1620 if (!table_instruction_features_empty(&features->nonmiss)) {
1621 ds_put_cstr(s, " (same instructions)\n");
1622 }
1623 } else if (!table_instruction_features_equal(&features->nonmiss, table,
1624 &features->miss, table)) {
1625 ds_put_cstr(s, " instructions (other than table miss):\n");
1626 print_table_instruction_features(s, &features->nonmiss, prev_nonmiss);
1627 ds_put_cstr(s, " instructions (table miss):\n");
1628 print_table_instruction_features(s, &features->miss, prev_miss);
1629 } else if (!table_instruction_features_empty(&features->nonmiss)) {
1630 ds_put_cstr(s, " instructions (table miss and others):\n");
1631 print_table_instruction_features(s, &features->nonmiss, prev_nonmiss);
1632 }
1633
1634 if (!bitmap_is_all_zeros(features->match.bm, MFF_N_IDS)) {
1635 if (prev_features
1636 && bitmap_equal(features->match.bm, prev_features->match.bm,
1637 MFF_N_IDS)) {
1638 ds_put_cstr(s, " (same matching)\n");
1639 } else {
1640 ds_put_cstr(s, " matching:\n");
1641
1642 int i;
1643 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, features->match.bm) {
1644 const struct mf_field *f = mf_from_id(i);
1645 bool mask = bitmap_is_set(features->mask.bm, i);
1646 bool wildcard = bitmap_is_set(features->wildcard.bm, i);
1647
1648 ds_put_format(s, " %s: %s\n",
1649 f->name,
1650 (mask ? "arbitrary mask"
1651 : wildcard ? "exact match or wildcard"
1652 : "must exact match"));
1653 }
1654 }
1655 }
1656 }
1657
1658 void
1659 ofputil_table_features_format_finish(struct ds *s,
1660 int first_ditto, int last_ditto)
1661 {
1662 if (first_ditto < 0) {
1663 return;
1664 }
1665
1666 ds_put_char(s, '\n');
1667 if (first_ditto == last_ditto) {
1668 ds_put_format(s, " table %d: ditto\n", first_ditto);
1669 } else {
1670 ds_put_format(s, " tables %d...%d: ditto\n", first_ditto, last_ditto);
1671 }
1672 }
1673
1674 /* Returns true if 'super' is a superset of 'sub', false otherwise. */
1675 static bool
1676 ofputil_table_action_features_is_superset(
1677 const struct ofputil_table_action_features *super,
1678 const struct ofputil_table_action_features *sub)
1679 {
1680 return (uint_is_superset(super->ofpacts, sub->ofpacts)
1681 && mf_bitmap_is_superset(&super->set_fields, &sub->set_fields));
1682 }
1683
1684 /* Returns true if 'super' is a superset of 'sub', false otherwise. */
1685 static bool
1686 ofputil_table_instruction_features_is_superset(
1687 const struct ofputil_table_instruction_features *super,
1688 const struct ofputil_table_instruction_features *sub)
1689 {
1690 return (bitmap_is_superset(super->next, sub->next, 255)
1691 && uint_is_superset(super->instructions, sub->instructions)
1692 && ofputil_table_action_features_is_superset(&super->write,
1693 &sub->write)
1694 && ofputil_table_action_features_is_superset(&super->apply,
1695 &sub->apply));
1696 }
1697
1698 /* Returns true if 'super' is a superset of 'sub', false otherwise. */
1699 bool
1700 ofputil_table_features_are_superset(
1701 const struct ofputil_table_features *super,
1702 const struct ofputil_table_features *sub)
1703 {
1704 return (be64_is_superset(super->metadata_match, sub->metadata_match)
1705 && be64_is_superset(super->metadata_write, sub->metadata_write)
1706 && super->max_entries >= sub->max_entries
1707 && super->supports_eviction >= sub->supports_eviction
1708 && super->supports_vacancy_events >= sub->supports_vacancy_events
1709 && ofputil_table_instruction_features_is_superset(&super->nonmiss,
1710 &sub->nonmiss)
1711 && ofputil_table_instruction_features_is_superset(&super->miss,
1712 &sub->miss)
1713 && mf_bitmap_is_superset(&super->match, &sub->match)
1714 && mf_bitmap_is_superset(&super->mask, &sub->mask)
1715 && mf_bitmap_is_superset(&super->wildcard, &sub->wildcard));
1716 }
1717 \f
1718 /* Table stats. */
1719
1720 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
1721 * matched and a field that must be wildcarded. This function returns a bitmap
1722 * that contains both kinds of fields. */
1723 static struct mf_bitmap
1724 wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
1725 {
1726 struct mf_bitmap wc = features->match;
1727 bitmap_not(wc.bm, MFF_N_IDS);
1728 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
1729 return wc;
1730 }
1731
1732 struct ofp10_wc_map {
1733 enum ofp10_flow_wildcards wc10;
1734 enum mf_field_id mf;
1735 };
1736
1737 static const struct ofp10_wc_map ofp10_wc_map[] = {
1738 { OFPFW10_IN_PORT, MFF_IN_PORT },
1739 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
1740 { OFPFW10_DL_SRC, MFF_ETH_SRC },
1741 { OFPFW10_DL_DST, MFF_ETH_DST},
1742 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
1743 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
1744 { OFPFW10_TP_SRC, MFF_TCP_SRC },
1745 { OFPFW10_TP_DST, MFF_TCP_DST },
1746 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
1747 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
1748 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
1749 { OFPFW10_NW_TOS, MFF_IP_DSCP },
1750 };
1751
1752 static ovs_be32
1753 mf_bitmap_to_of10(const struct mf_bitmap *fields)
1754 {
1755 const struct ofp10_wc_map *p;
1756 uint32_t wc10 = 0;
1757
1758 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
1759 if (bitmap_is_set(fields->bm, p->mf)) {
1760 wc10 |= p->wc10;
1761 }
1762 }
1763 return htonl(wc10);
1764 }
1765
1766 static struct mf_bitmap
1767 mf_bitmap_from_of10(ovs_be32 wc10_)
1768 {
1769 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
1770 const struct ofp10_wc_map *p;
1771 uint32_t wc10 = ntohl(wc10_);
1772
1773 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
1774 if (wc10 & p->wc10) {
1775 bitmap_set1(fields.bm, p->mf);
1776 }
1777 }
1778 return fields;
1779 }
1780
1781 static void
1782 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
1783 const struct ofputil_table_features *features,
1784 struct ofpbuf *buf)
1785 {
1786 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
1787 struct ofp10_table_stats *out;
1788
1789 out = ofpbuf_put_zeros(buf, sizeof *out);
1790 out->table_id = features->table_id;
1791 ovs_strlcpy_arrays(out->name, features->name);
1792 out->wildcards = mf_bitmap_to_of10(&wc);
1793 out->max_entries = htonl(features->max_entries);
1794 out->active_count = htonl(stats->active_count);
1795 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
1796 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
1797 }
1798
1799 struct ofp11_wc_map {
1800 enum ofp11_flow_match_fields wc11;
1801 enum mf_field_id mf;
1802 };
1803
1804 static const struct ofp11_wc_map ofp11_wc_map[] = {
1805 { OFPFMF11_IN_PORT, MFF_IN_PORT },
1806 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
1807 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
1808 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
1809 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
1810 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
1811 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
1812 { OFPFMF11_TP_DST, MFF_TCP_DST },
1813 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
1814 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
1815 /* I don't know what OFPFMF11_TYPE means. */
1816 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
1817 { OFPFMF11_DL_DST, MFF_ETH_DST },
1818 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
1819 { OFPFMF11_NW_DST, MFF_IPV4_DST },
1820 { OFPFMF11_METADATA, MFF_METADATA },
1821 };
1822
1823 static ovs_be32
1824 mf_bitmap_to_of11(const struct mf_bitmap *fields)
1825 {
1826 const struct ofp11_wc_map *p;
1827 uint32_t wc11 = 0;
1828
1829 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
1830 if (bitmap_is_set(fields->bm, p->mf)) {
1831 wc11 |= p->wc11;
1832 }
1833 }
1834 return htonl(wc11);
1835 }
1836
1837 static struct mf_bitmap
1838 mf_bitmap_from_of11(ovs_be32 wc11_)
1839 {
1840 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
1841 const struct ofp11_wc_map *p;
1842 uint32_t wc11 = ntohl(wc11_);
1843
1844 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
1845 if (wc11 & p->wc11) {
1846 bitmap_set1(fields.bm, p->mf);
1847 }
1848 }
1849 return fields;
1850 }
1851
1852 static void
1853 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
1854 const struct ofputil_table_features *features,
1855 struct ofpbuf *buf)
1856 {
1857 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
1858 struct ofp11_table_stats *out;
1859
1860 out = ofpbuf_put_zeros(buf, sizeof *out);
1861 out->table_id = features->table_id;
1862 ovs_strlcpy_arrays(out->name, features->name);
1863 out->wildcards = mf_bitmap_to_of11(&wc);
1864 out->match = mf_bitmap_to_of11(&features->match);
1865 out->instructions = ovsinst_bitmap_to_openflow(
1866 features->nonmiss.instructions, OFP11_VERSION);
1867 out->write_actions = ofpact_bitmap_to_openflow(
1868 features->nonmiss.write.ofpacts, OFP11_VERSION);
1869 out->apply_actions = ofpact_bitmap_to_openflow(
1870 features->nonmiss.apply.ofpacts, OFP11_VERSION);
1871 out->config = htonl(features->miss_config);
1872 out->max_entries = htonl(features->max_entries);
1873 out->active_count = htonl(stats->active_count);
1874 out->lookup_count = htonll(stats->lookup_count);
1875 out->matched_count = htonll(stats->matched_count);
1876 }
1877
1878 static void
1879 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
1880 const struct ofputil_table_features *features,
1881 struct ofpbuf *buf)
1882 {
1883 struct ofp12_table_stats *out;
1884
1885 out = ofpbuf_put_zeros(buf, sizeof *out);
1886 out->table_id = features->table_id;
1887 ovs_strlcpy_arrays(out->name, features->name);
1888 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
1889 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
1890 OFP12_VERSION);
1891 out->write_actions = ofpact_bitmap_to_openflow(
1892 features->nonmiss.write.ofpacts, OFP12_VERSION);
1893 out->apply_actions = ofpact_bitmap_to_openflow(
1894 features->nonmiss.apply.ofpacts, OFP12_VERSION);
1895 out->write_setfields = oxm_bitmap_from_mf_bitmap(
1896 &features->nonmiss.write.set_fields, OFP12_VERSION);
1897 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
1898 &features->nonmiss.apply.set_fields, OFP12_VERSION);
1899 out->metadata_match = features->metadata_match;
1900 out->metadata_write = features->metadata_write;
1901 out->instructions = ovsinst_bitmap_to_openflow(
1902 features->nonmiss.instructions, OFP12_VERSION);
1903 out->config = ofputil_encode_table_config(features->miss_config,
1904 OFPUTIL_TABLE_EVICTION_DEFAULT,
1905 OFPUTIL_TABLE_VACANCY_DEFAULT,
1906 OFP12_VERSION);
1907 out->max_entries = htonl(features->max_entries);
1908 out->active_count = htonl(stats->active_count);
1909 out->lookup_count = htonll(stats->lookup_count);
1910 out->matched_count = htonll(stats->matched_count);
1911 }
1912
1913 static void
1914 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
1915 struct ofpbuf *buf)
1916 {
1917 struct ofp13_table_stats *out;
1918
1919 out = ofpbuf_put_zeros(buf, sizeof *out);
1920 out->table_id = stats->table_id;
1921 out->active_count = htonl(stats->active_count);
1922 out->lookup_count = htonll(stats->lookup_count);
1923 out->matched_count = htonll(stats->matched_count);
1924 }
1925
1926 struct ofpbuf *
1927 ofputil_encode_table_stats_reply(const struct ofp_header *request)
1928 {
1929 return ofpraw_alloc_stats_reply(request, 0);
1930 }
1931
1932 void
1933 ofputil_append_table_stats_reply(struct ofpbuf *reply,
1934 const struct ofputil_table_stats *stats,
1935 const struct ofputil_table_features *features)
1936 {
1937 struct ofp_header *oh = reply->header;
1938
1939 ovs_assert(stats->table_id == features->table_id);
1940
1941 switch ((enum ofp_version) oh->version) {
1942 case OFP10_VERSION:
1943 ofputil_put_ofp10_table_stats(stats, features, reply);
1944 break;
1945
1946 case OFP11_VERSION:
1947 ofputil_put_ofp11_table_stats(stats, features, reply);
1948 break;
1949
1950 case OFP12_VERSION:
1951 ofputil_put_ofp12_table_stats(stats, features, reply);
1952 break;
1953
1954 case OFP13_VERSION:
1955 case OFP14_VERSION:
1956 case OFP15_VERSION:
1957 case OFP16_VERSION:
1958 ofputil_put_ofp13_table_stats(stats, reply);
1959 break;
1960
1961 default:
1962 OVS_NOT_REACHED();
1963 }
1964 }
1965
1966 static int
1967 ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
1968 struct ofputil_table_stats *stats,
1969 struct ofputil_table_features *features)
1970 {
1971 struct ofp10_table_stats *ots;
1972
1973 ots = ofpbuf_try_pull(msg, sizeof *ots);
1974 if (!ots) {
1975 return OFPERR_OFPBRC_BAD_LEN;
1976 }
1977
1978 features->table_id = ots->table_id;
1979 ovs_strlcpy_arrays(features->name, ots->name);
1980 features->max_entries = ntohl(ots->max_entries);
1981 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
1982
1983 stats->table_id = ots->table_id;
1984 stats->active_count = ntohl(ots->active_count);
1985 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
1986 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
1987
1988 return 0;
1989 }
1990
1991 static int
1992 ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
1993 struct ofputil_table_stats *stats,
1994 struct ofputil_table_features *features)
1995 {
1996 struct ofp11_table_stats *ots;
1997
1998 ots = ofpbuf_try_pull(msg, sizeof *ots);
1999 if (!ots) {
2000 return OFPERR_OFPBRC_BAD_LEN;
2001 }
2002
2003 features->table_id = ots->table_id;
2004 ovs_strlcpy_arrays(features->name, ots->name);
2005 features->max_entries = ntohl(ots->max_entries);
2006 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
2007 ots->instructions, OFP11_VERSION);
2008 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
2009 ots->write_actions, OFP11_VERSION);
2010 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
2011 ots->write_actions, OFP11_VERSION);
2012 features->miss = features->nonmiss;
2013 features->miss_config = ofputil_decode_table_miss(ots->config,
2014 OFP11_VERSION);
2015 features->match = mf_bitmap_from_of11(ots->match);
2016 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
2017 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
2018
2019 stats->table_id = ots->table_id;
2020 stats->active_count = ntohl(ots->active_count);
2021 stats->lookup_count = ntohll(ots->lookup_count);
2022 stats->matched_count = ntohll(ots->matched_count);
2023
2024 return 0;
2025 }
2026
2027 static int
2028 ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
2029 struct ofputil_table_stats *stats,
2030 struct ofputil_table_features *features)
2031 {
2032 struct ofp12_table_stats *ots;
2033
2034 ots = ofpbuf_try_pull(msg, sizeof *ots);
2035 if (!ots) {
2036 return OFPERR_OFPBRC_BAD_LEN;
2037 }
2038
2039 features->table_id = ots->table_id;
2040 ovs_strlcpy_arrays(features->name, ots->name);
2041 features->metadata_match = ots->metadata_match;
2042 features->metadata_write = ots->metadata_write;
2043 features->miss_config = ofputil_decode_table_miss(ots->config,
2044 OFP12_VERSION);
2045 features->max_entries = ntohl(ots->max_entries);
2046
2047 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
2048 ots->instructions, OFP12_VERSION);
2049 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
2050 ots->write_actions, OFP12_VERSION);
2051 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
2052 ots->apply_actions, OFP12_VERSION);
2053 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
2054 ots->write_setfields, OFP12_VERSION);
2055 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
2056 ots->apply_setfields, OFP12_VERSION);
2057 features->miss = features->nonmiss;
2058
2059 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
2060 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
2061 OFP12_VERSION);
2062 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
2063
2064 stats->table_id = ots->table_id;
2065 stats->active_count = ntohl(ots->active_count);
2066 stats->lookup_count = ntohll(ots->lookup_count);
2067 stats->matched_count = ntohll(ots->matched_count);
2068
2069 return 0;
2070 }
2071
2072 static int
2073 ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
2074 struct ofputil_table_stats *stats,
2075 struct ofputil_table_features *features)
2076 {
2077 struct ofp13_table_stats *ots;
2078
2079 ots = ofpbuf_try_pull(msg, sizeof *ots);
2080 if (!ots) {
2081 return OFPERR_OFPBRC_BAD_LEN;
2082 }
2083
2084 features->table_id = ots->table_id;
2085
2086 stats->table_id = ots->table_id;
2087 stats->active_count = ntohl(ots->active_count);
2088 stats->lookup_count = ntohll(ots->lookup_count);
2089 stats->matched_count = ntohll(ots->matched_count);
2090
2091 return 0;
2092 }
2093
2094 int
2095 ofputil_decode_table_stats_reply(struct ofpbuf *msg,
2096 struct ofputil_table_stats *stats,
2097 struct ofputil_table_features *features)
2098 {
2099 const struct ofp_header *oh;
2100
2101 if (!msg->header) {
2102 ofpraw_pull_assert(msg);
2103 }
2104 oh = msg->header;
2105
2106 if (!msg->size) {
2107 return EOF;
2108 }
2109
2110 memset(stats, 0, sizeof *stats);
2111 memset(features, 0, sizeof *features);
2112 features->supports_eviction = -1;
2113 features->supports_vacancy_events = -1;
2114
2115 switch ((enum ofp_version) oh->version) {
2116 case OFP10_VERSION:
2117 return ofputil_decode_ofp10_table_stats(msg, stats, features);
2118
2119 case OFP11_VERSION:
2120 return ofputil_decode_ofp11_table_stats(msg, stats, features);
2121
2122 case OFP12_VERSION:
2123 return ofputil_decode_ofp12_table_stats(msg, stats, features);
2124
2125 case OFP13_VERSION:
2126 case OFP14_VERSION:
2127 case OFP15_VERSION:
2128 case OFP16_VERSION:
2129 return ofputil_decode_ofp13_table_stats(msg, stats, features);
2130
2131 default:
2132 OVS_NOT_REACHED();
2133 }
2134 }
2135 \f
2136 /* Returns a string form of 'reason'. The return value is either a statically
2137 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
2138 * 'bufsize' should be at least OFP_ASYNC_CONFIG_REASON_BUFSIZE. */
2139 const char *
2140 ofp_table_reason_to_string(enum ofp14_table_reason reason,
2141 char *reasonbuf, size_t bufsize)
2142 {
2143 switch (reason) {
2144 case OFPTR_VACANCY_DOWN:
2145 return "vacancy_down";
2146
2147 case OFPTR_VACANCY_UP:
2148 return "vacancy_up";
2149
2150 default:
2151 snprintf(reasonbuf, bufsize, "%d", (int) reason);
2152 return reasonbuf;
2153 }
2154 }
2155
2156 static void
2157 ofputil_put_ofp14_table_desc(const struct ofputil_table_desc *td,
2158 struct ofpbuf *b, enum ofp_version version)
2159 {
2160 struct ofp14_table_desc *otd;
2161 struct ofp14_table_mod_prop_vacancy *otv;
2162 size_t start_otd;
2163
2164 start_otd = b->size;
2165 ofpbuf_put_zeros(b, sizeof *otd);
2166
2167 ofpprop_put_u32(b, OFPTMPT14_EVICTION, td->eviction_flags);
2168
2169 otv = ofpbuf_put_zeros(b, sizeof *otv);
2170 otv->type = htons(OFPTMPT14_VACANCY);
2171 otv->length = htons(sizeof *otv);
2172 otv->vacancy_down = td->table_vacancy.vacancy_down;
2173 otv->vacancy_up = td->table_vacancy.vacancy_up;
2174 otv->vacancy = td->table_vacancy.vacancy;
2175
2176 otd = ofpbuf_at_assert(b, start_otd, sizeof *otd);
2177 otd->length = htons(b->size - start_otd);
2178 otd->table_id = td->table_id;
2179 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
2180 td->eviction, td->vacancy,
2181 version);
2182 }
2183
2184 /* Converts the abstract form of a "table status" message in '*ts' into an
2185 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2186 * a buffer owned by the caller. */
2187 struct ofpbuf *
2188 ofputil_encode_table_status(const struct ofputil_table_status *ts,
2189 enum ofputil_protocol protocol)
2190 {
2191 enum ofp_version version;
2192 struct ofpbuf *b;
2193
2194 version = ofputil_protocol_to_ofp_version(protocol);
2195 if (version >= OFP14_VERSION) {
2196 enum ofpraw raw;
2197 struct ofp14_table_status *ots;
2198
2199 raw = OFPRAW_OFPT14_TABLE_STATUS;
2200 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
2201 ots = ofpbuf_put_zeros(b, sizeof *ots);
2202 ots->reason = ts->reason;
2203 ofputil_put_ofp14_table_desc(&ts->desc, b, version);
2204 ofpmsg_update_length(b);
2205 return b;
2206 } else {
2207 return NULL;
2208 }
2209 }
2210
2211 /* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
2212 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
2213 enum ofperr
2214 ofputil_decode_table_status(const struct ofp_header *oh,
2215 struct ofputil_table_status *ts)
2216 {
2217 const struct ofp14_table_status *ots;
2218 struct ofpbuf b;
2219 enum ofperr error;
2220 enum ofpraw raw;
2221
2222 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2223 raw = ofpraw_pull_assert(&b);
2224 ots = ofpbuf_pull(&b, sizeof *ots);
2225
2226 if (raw == OFPRAW_OFPT14_TABLE_STATUS) {
2227 if (ots->reason != OFPTR_VACANCY_DOWN
2228 && ots->reason != OFPTR_VACANCY_UP) {
2229 return OFPERR_OFPBPC_BAD_VALUE;
2230 }
2231 ts->reason = ots->reason;
2232
2233 error = ofputil_decode_table_desc(&b, &ts->desc, oh->version);
2234 return error;
2235 } else {
2236 return OFPERR_OFPBRC_BAD_VERSION;
2237 }
2238
2239 return 0;
2240 }
2241
2242 void
2243 ofputil_format_table_status(struct ds *string,
2244 const struct ofputil_table_status *ts,
2245 const struct ofputil_table_map *table_map)
2246 {
2247 if (ts->reason == OFPTR_VACANCY_DOWN) {
2248 ds_put_format(string, " reason=VACANCY_DOWN");
2249 } else if (ts->reason == OFPTR_VACANCY_UP) {
2250 ds_put_format(string, " reason=VACANCY_UP");
2251 }
2252
2253 ds_put_format(string, "\ntable_desc:-");
2254 ofputil_table_desc_format(string, &ts->desc, table_map);
2255 }