]> git.proxmox.com Git - ovs.git/blame - lib/ofp-print.c
classifier: Print ARP opcode as "arp_op" instead of "opcode".
[ovs.git] / lib / ofp-print.c
CommitLineData
064af421 1/*
c4617b3c 2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "ofp-print.h"
064af421
BP
19
20#include <errno.h>
21#include <inttypes.h>
7f3adc00 22#include <sys/types.h>
064af421
BP
23#include <netinet/in.h>
24#include <sys/wait.h>
25#include <stdarg.h>
26#include <stdlib.h>
27#include <ctype.h>
28
10a24935 29#include "byte-order.h"
064af421
BP
30#include "compiler.h"
31#include "dynamic-string.h"
32#include "flow.h"
53ddd40a 33#include "multipath.h"
7fa91113 34#include "nx-match.h"
69b6be19 35#include "ofp-util.h"
064af421
BP
36#include "ofpbuf.h"
37#include "openflow/openflow.h"
38#include "openflow/nicira-ext.h"
39#include "packets.h"
40#include "pcap.h"
e41a9130 41#include "type-props.h"
c4617b3c 42#include "unaligned.h"
064af421
BP
43#include "util.h"
44
45static void ofp_print_port_name(struct ds *string, uint16_t port);
d2805da2 46static void ofp_print_queue_name(struct ds *string, uint32_t port);
7fa91113
BP
47static void ofp_print_error(struct ds *, int error);
48
064af421
BP
49
50/* Returns a string that represents the contents of the Ethernet frame in the
51 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
52 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
53 * bytes were captured).
54 *
55 * The caller must free the returned string.
56 *
57 * This starts and kills a tcpdump subprocess so it's quite expensive. */
58char *
67a4917b 59ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
064af421
BP
60{
61 struct ds ds = DS_EMPTY_INITIALIZER;
62 struct ofpbuf buf;
63
64 char command[128];
65 FILE *pcap;
66 FILE *tcpdump;
67 int status;
68 int c;
69
0bc9407d 70 ofpbuf_use_const(&buf, data, len);
064af421
BP
71
72 pcap = tmpfile();
73 if (!pcap) {
74 ovs_error(errno, "tmpfile");
75 return xstrdup("<error>");
76 }
77 pcap_write_header(pcap);
78 pcap_write(pcap, &buf);
79 fflush(pcap);
80 if (ferror(pcap)) {
81 ovs_error(errno, "error writing temporary file");
82 }
83 rewind(pcap);
84
6d38ac7c 85 snprintf(command, sizeof command, "/usr/sbin/tcpdump -t -e -n -r /dev/fd/%d 2>/dev/null",
064af421
BP
86 fileno(pcap));
87 tcpdump = popen(command, "r");
88 fclose(pcap);
89 if (!tcpdump) {
90 ovs_error(errno, "exec(\"%s\")", command);
91 return xstrdup("<error>");
92 }
93
94 while ((c = getc(tcpdump)) != EOF) {
95 ds_put_char(&ds, c);
96 }
97
98 status = pclose(tcpdump);
99 if (WIFEXITED(status)) {
100 if (WEXITSTATUS(status))
101 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
102 } else if (WIFSIGNALED(status)) {
d295e8e9 103 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
064af421
BP
104 }
105 return ds_cstr(&ds);
106}
107
064af421 108static void
d1e2cf21
BP
109ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
110 int verbosity)
064af421 111{
d1e2cf21 112 size_t len = ntohs(op->header.length);
064af421
BP
113 size_t data_len;
114
115 ds_put_format(string, " total_len=%"PRIu16" in_port=",
116 ntohs(op->total_len));
117 ofp_print_port_name(string, ntohs(op->in_port));
118
119 if (op->reason == OFPR_ACTION)
120 ds_put_cstr(string, " (via action)");
121 else if (op->reason != OFPR_NO_MATCH)
122 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
123
124 data_len = len - offsetof(struct ofp_packet_in, data);
125 ds_put_format(string, " data_len=%zu", data_len);
d84d4b88 126 if (op->buffer_id == htonl(UINT32_MAX)) {
064af421
BP
127 ds_put_format(string, " (unbuffered)");
128 if (ntohs(op->total_len) != data_len)
129 ds_put_format(string, " (***total_len != data_len***)");
130 } else {
131 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
132 if (ntohs(op->total_len) < data_len)
133 ds_put_format(string, " (***total_len < data_len***)");
134 }
135 ds_put_char(string, '\n');
136
137 if (verbosity > 0) {
ae412e7d 138 struct flow flow;
064af421 139 struct ofpbuf packet;
8321fb9c 140
0bc9407d 141 ofpbuf_use_const(&packet, op->data, data_len);
659586ef 142 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
8321fb9c 143 flow_format(string, &flow);
064af421
BP
144 ds_put_char(string, '\n');
145 }
146 if (verbosity > 1) {
147 char *packet = ofp_packet_to_string(op->data, data_len,
d295e8e9 148 ntohs(op->total_len));
064af421
BP
149 ds_put_cstr(string, packet);
150 free(packet);
151 }
152}
153
d295e8e9 154static void ofp_print_port_name(struct ds *string, uint16_t port)
064af421
BP
155{
156 const char *name;
157 switch (port) {
158 case OFPP_IN_PORT:
159 name = "IN_PORT";
160 break;
161 case OFPP_TABLE:
162 name = "TABLE";
163 break;
164 case OFPP_NORMAL:
165 name = "NORMAL";
166 break;
167 case OFPP_FLOOD:
168 name = "FLOOD";
169 break;
170 case OFPP_ALL:
171 name = "ALL";
172 break;
173 case OFPP_CONTROLLER:
174 name = "CONTROLLER";
175 break;
176 case OFPP_LOCAL:
177 name = "LOCAL";
178 break;
179 case OFPP_NONE:
180 name = "NONE";
181 break;
182 default:
183 ds_put_format(string, "%"PRIu16, port);
184 return;
185 }
186 ds_put_cstr(string, name);
187}
188
f393f81e 189
96fc46e8
BP
190static void
191print_note(struct ds *string, const struct nx_action_note *nan)
192{
193 size_t len;
194 size_t i;
195
196 ds_put_cstr(string, "note:");
197 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
198 for (i = 0; i < len; i++) {
199 if (i) {
200 ds_put_char(string, '.');
201 }
202 ds_put_format(string, "%02"PRIx8, nan->note[i]);
203 }
204}
205
f393f81e
BP
206static int
207nx_action_len(enum nx_action_subtype subtype)
208{
209 switch (subtype) {
210 case NXAST_SNAT__OBSOLETE: return -1;
211 case NXAST_RESUBMIT: return sizeof(struct nx_action_resubmit);
212 case NXAST_SET_TUNNEL: return sizeof(struct nx_action_set_tunnel);
6c222e55 213 case NXAST_DROP_SPOOFED_ARP__OBSOLETE: return -1;
f393f81e
BP
214 case NXAST_SET_QUEUE: return sizeof(struct nx_action_set_queue);
215 case NXAST_POP_QUEUE: return sizeof(struct nx_action_pop_queue);
216 case NXAST_REG_MOVE: return sizeof(struct nx_action_reg_move);
217 case NXAST_REG_LOAD: return sizeof(struct nx_action_reg_load);
218 case NXAST_NOTE: return -1;
b9298d3f 219 case NXAST_SET_TUNNEL64: return sizeof(struct nx_action_set_tunnel64);
53ddd40a 220 case NXAST_MULTIPATH: return sizeof(struct nx_action_multipath);
3b6a2571 221 case NXAST_AUTOPATH: return sizeof (struct nx_action_autopath);
f393f81e
BP
222 default: return -1;
223 }
224}
225
064af421
BP
226static void
227ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
228{
5160ab34 229 int subtype = ntohs(nah->subtype);
f393f81e
BP
230 int required_len = nx_action_len(subtype);
231 int len = ntohs(nah->len);
232
233 if (required_len != -1 && required_len != len) {
234 ds_put_format(string, "***Nicira action %"PRIu16" wrong length: %d***",
235 subtype, len);
236 return;
237 }
e41a9130
BP
238
239 if (subtype <= TYPE_MAXIMUM(enum nx_action_subtype)) {
b9298d3f 240 const struct nx_action_set_tunnel64 *nast64;
e41a9130
BP
241 const struct nx_action_set_tunnel *nast;
242 const struct nx_action_set_queue *nasq;
243 const struct nx_action_resubmit *nar;
f393f81e
BP
244 const struct nx_action_reg_move *move;
245 const struct nx_action_reg_load *load;
53ddd40a 246 const struct nx_action_multipath *nam;
3b6a2571 247 const struct nx_action_autopath *naa;
e41a9130
BP
248
249 switch ((enum nx_action_subtype) subtype) {
250 case NXAST_RESUBMIT:
251 nar = (struct nx_action_resubmit *)nah;
252 ds_put_format(string, "resubmit:");
253 ofp_print_port_name(string, ntohs(nar->in_port));
254 return;
255
256 case NXAST_SET_TUNNEL:
257 nast = (struct nx_action_set_tunnel *)nah;
258 ds_put_format(string, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
259 return;
260
e41a9130
BP
261 case NXAST_SET_QUEUE:
262 nasq = (struct nx_action_set_queue *)nah;
263 ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
264 return;
265
266 case NXAST_POP_QUEUE:
267 ds_put_cstr(string, "pop_queue");
268 return;
269
270 case NXAST_NOTE:
271 print_note(string, (const struct nx_action_note *) nah);
272 return;
273
274 case NXAST_REG_MOVE:
f393f81e
BP
275 move = (const struct nx_action_reg_move *) nah;
276 nxm_format_reg_move(move, string);
277 return;
278
e41a9130 279 case NXAST_REG_LOAD:
f393f81e
BP
280 load = (const struct nx_action_reg_load *) nah;
281 nxm_format_reg_load(load, string);
e41a9130
BP
282 return;
283
b9298d3f 284 case NXAST_SET_TUNNEL64:
53ddd40a 285 nast64 = (const struct nx_action_set_tunnel64 *) nah;
b9298d3f
BP
286 ds_put_format(string, "set_tunnel64:%#"PRIx64,
287 ntohll(nast64->tun_id));
288 return;
289
53ddd40a
BP
290 case NXAST_MULTIPATH:
291 nam = (const struct nx_action_multipath *) nah;
292 multipath_format(nam, string);
293 return;
294
3b6a2571
EJ
295 case NXAST_AUTOPATH:
296 naa = (const struct nx_action_autopath *)nah;
297 ds_put_format(string, "autopath(%u,", ntohl(naa->id));
298 nxm_format_field_bits(string, ntohl(naa->dst),
299 nxm_decode_ofs(naa->ofs_nbits),
300 nxm_decode_n_bits(naa->ofs_nbits));
301 ds_put_char(string, ')');
302 return;
303
e41a9130 304 case NXAST_SNAT__OBSOLETE:
6c222e55 305 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
e41a9130
BP
306 default:
307 break;
308 }
eedc0097
JP
309 }
310
5160ab34 311 ds_put_format(string, "***unknown Nicira action:%d***", subtype);
e41a9130 312}
96fc46e8 313
e41a9130
BP
314static int
315ofp_action_len(enum ofp_action_type type)
316{
317 switch (type) {
318 case OFPAT_OUTPUT: return sizeof(struct ofp_action_output);
319 case OFPAT_SET_VLAN_VID: return sizeof(struct ofp_action_vlan_vid);
320 case OFPAT_SET_VLAN_PCP: return sizeof(struct ofp_action_vlan_pcp);
321 case OFPAT_STRIP_VLAN: return sizeof(struct ofp_action_header);
322 case OFPAT_SET_DL_SRC: return sizeof(struct ofp_action_dl_addr);
323 case OFPAT_SET_DL_DST: return sizeof(struct ofp_action_dl_addr);
324 case OFPAT_SET_NW_SRC: return sizeof(struct ofp_action_nw_addr);
325 case OFPAT_SET_NW_DST: return sizeof(struct ofp_action_nw_addr);
326 case OFPAT_SET_NW_TOS: return sizeof(struct ofp_action_nw_tos);
327 case OFPAT_SET_TP_SRC: return sizeof(struct ofp_action_tp_port);
328 case OFPAT_SET_TP_DST: return sizeof(struct ofp_action_tp_port);
329 case OFPAT_ENQUEUE: return sizeof(struct ofp_action_enqueue);
330 case OFPAT_VENDOR: return -1;
331 default: return -1;
064af421
BP
332 }
333}
334
335static int
d295e8e9
JP
336ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
337 size_t actions_len)
064af421 338{
e41a9130
BP
339 enum ofp_action_type type;
340 int required_len;
064af421
BP
341 size_t len;
342
064af421
BP
343 if (actions_len < sizeof *ah) {
344 ds_put_format(string, "***action array too short for next action***\n");
345 return -1;
346 }
347
348 type = ntohs(ah->type);
349 len = ntohs(ah->len);
350 if (actions_len < len) {
e41a9130 351 ds_put_format(string, "***truncated action %d***\n", (int) type);
064af421
BP
352 return -1;
353 }
354
1d87ef5c
BP
355 if (!len) {
356 ds_put_format(string, "***zero-length action***\n");
357 return 8;
358 }
359
69b6be19 360 if ((len % OFP_ACTION_ALIGN) != 0) {
d295e8e9 361 ds_put_format(string,
e41a9130
BP
362 "***action %d length not a multiple of %d***\n",
363 (int) type, OFP_ACTION_ALIGN);
064af421
BP
364 return -1;
365 }
366
e41a9130
BP
367 required_len = ofp_action_len(type);
368 if (required_len >= 0 && len != required_len) {
369 ds_put_format(string,
370 "***action %d wrong length: %zu***\n", (int) type, len);
371 return -1;
064af421 372 }
d295e8e9 373
064af421
BP
374 switch (type) {
375 case OFPAT_OUTPUT: {
376 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
d295e8e9 377 uint16_t port = ntohs(oa->port);
064af421
BP
378 if (port < OFPP_MAX) {
379 ds_put_format(string, "output:%"PRIu16, port);
380 } else {
381 ofp_print_port_name(string, port);
382 if (port == OFPP_CONTROLLER) {
383 if (oa->max_len) {
384 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
385 } else {
386 ds_put_cstr(string, ":all");
387 }
388 }
389 }
390 break;
391 }
392
c4d279ab
BP
393 case OFPAT_ENQUEUE: {
394 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
395 unsigned int port = ntohs(ea->port);
396 unsigned int queue_id = ntohl(ea->queue_id);
397 ds_put_format(string, "enqueue:");
398 if (port != OFPP_IN_PORT) {
399 ds_put_format(string, "%u", port);
400 } else {
401 ds_put_cstr(string, "IN_PORT");
402 }
403 ds_put_format(string, "q%u", queue_id);
404 break;
405 }
406
064af421
BP
407 case OFPAT_SET_VLAN_VID: {
408 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
409 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
410 break;
411 }
412
413 case OFPAT_SET_VLAN_PCP: {
414 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
415 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
416 break;
417 }
418
419 case OFPAT_STRIP_VLAN:
420 ds_put_cstr(string, "strip_vlan");
421 break;
422
423 case OFPAT_SET_DL_SRC: {
424 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
d295e8e9 425 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
064af421
BP
426 ETH_ADDR_ARGS(da->dl_addr));
427 break;
428 }
429
430 case OFPAT_SET_DL_DST: {
431 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
d295e8e9 432 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
064af421
BP
433 ETH_ADDR_ARGS(da->dl_addr));
434 break;
435 }
436
437 case OFPAT_SET_NW_SRC: {
438 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
439 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
440 break;
441 }
442
443 case OFPAT_SET_NW_DST: {
444 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
445 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
446 break;
447 }
448
959a2ecd
JP
449 case OFPAT_SET_NW_TOS: {
450 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
451 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
452 break;
453 }
454
064af421
BP
455 case OFPAT_SET_TP_SRC: {
456 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
457 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
458 break;
459 }
460
461 case OFPAT_SET_TP_DST: {
462 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
463 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
464 break;
465 }
466
467 case OFPAT_VENDOR: {
d295e8e9 468 struct ofp_action_vendor_header *avh
064af421
BP
469 = (struct ofp_action_vendor_header *)ah;
470 if (len < sizeof *avh) {
471 ds_put_format(string, "***ofpat_vendor truncated***\n");
472 return -1;
473 }
474 if (avh->vendor == htonl(NX_VENDOR_ID)) {
475 ofp_print_nx_action(string, (struct nx_action_header *)avh);
476 } else {
477 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
478 }
479 break;
480 }
481
482 default:
e41a9130 483 ds_put_format(string, "(decoder %d not implemented)", (int) type);
064af421
BP
484 break;
485 }
486
487 return len;
488}
489
d295e8e9 490void
064af421 491ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
d295e8e9 492 size_t actions_len)
064af421
BP
493{
494 uint8_t *p = (uint8_t *)action;
495 int len = 0;
496
497 ds_put_cstr(string, "actions=");
498 if (!actions_len) {
499 ds_put_cstr(string, "drop");
500 }
501 while (actions_len > 0) {
502 if (len) {
503 ds_put_cstr(string, ",");
504 }
d295e8e9 505 len = ofp_print_action(string, (struct ofp_action_header *)p,
064af421
BP
506 actions_len);
507 if (len < 0) {
508 return;
509 }
510 p += len;
511 actions_len -= len;
512 }
513}
514
d1e2cf21
BP
515static void
516ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
517 int verbosity)
064af421 518{
d1e2cf21 519 size_t len = ntohs(opo->header.length);
064af421
BP
520 size_t actions_len = ntohs(opo->actions_len);
521
522 ds_put_cstr(string, " in_port=");
523 ofp_print_port_name(string, ntohs(opo->in_port));
524
525 ds_put_format(string, " actions_len=%zu ", actions_len);
526 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
527 ds_put_format(string, "***packet too short for action length***\n");
528 return;
529 }
530 ofp_print_actions(string, opo->actions, actions_len);
531
532 if (ntohl(opo->buffer_id) == UINT32_MAX) {
533 int data_len = len - sizeof *opo - actions_len;
534 ds_put_format(string, " data_len=%d", data_len);
535 if (verbosity > 0 && len > sizeof *opo) {
536 char *packet = ofp_packet_to_string(
537 (uint8_t *)opo->actions + actions_len, data_len, data_len);
538 ds_put_char(string, '\n');
539 ds_put_cstr(string, packet);
540 free(packet);
541 }
542 } else {
543 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
544 }
545 ds_put_char(string, '\n');
546}
547
548/* qsort comparison function. */
549static int
550compare_ports(const void *a_, const void *b_)
551{
552 const struct ofp_phy_port *a = a_;
553 const struct ofp_phy_port *b = b_;
554 uint16_t ap = ntohs(a->port_no);
555 uint16_t bp = ntohs(b->port_no);
556
557 return ap < bp ? -1 : ap > bp;
558}
559
560static void ofp_print_port_features(struct ds *string, uint32_t features)
561{
562 if (features == 0) {
563 ds_put_cstr(string, "Unsupported\n");
564 return;
565 }
566 if (features & OFPPF_10MB_HD) {
567 ds_put_cstr(string, "10MB-HD ");
568 }
569 if (features & OFPPF_10MB_FD) {
570 ds_put_cstr(string, "10MB-FD ");
571 }
572 if (features & OFPPF_100MB_HD) {
573 ds_put_cstr(string, "100MB-HD ");
574 }
575 if (features & OFPPF_100MB_FD) {
576 ds_put_cstr(string, "100MB-FD ");
577 }
578 if (features & OFPPF_1GB_HD) {
579 ds_put_cstr(string, "1GB-HD ");
580 }
581 if (features & OFPPF_1GB_FD) {
582 ds_put_cstr(string, "1GB-FD ");
583 }
584 if (features & OFPPF_10GB_FD) {
585 ds_put_cstr(string, "10GB-FD ");
586 }
587 if (features & OFPPF_COPPER) {
588 ds_put_cstr(string, "COPPER ");
589 }
590 if (features & OFPPF_FIBER) {
591 ds_put_cstr(string, "FIBER ");
592 }
593 if (features & OFPPF_AUTONEG) {
594 ds_put_cstr(string, "AUTO_NEG ");
595 }
596 if (features & OFPPF_PAUSE) {
597 ds_put_cstr(string, "AUTO_PAUSE ");
598 }
599 if (features & OFPPF_PAUSE_ASYM) {
600 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
601 }
602 ds_put_char(string, '\n');
603}
604
605static void
606ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
607{
0b61210e 608 char name[OFP_MAX_PORT_NAME_LEN];
064af421
BP
609 int j;
610
611 memcpy(name, port->name, sizeof name);
612 for (j = 0; j < sizeof name - 1; j++) {
858f2852 613 if (!isprint((unsigned char) name[j])) {
064af421
BP
614 break;
615 }
616 }
617 name[j] = '\0';
618
619 ds_put_char(string, ' ');
620 ofp_print_port_name(string, ntohs(port->port_no));
621 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
622 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
623 ntohl(port->state));
624 if (port->curr) {
625 ds_put_format(string, " current: ");
626 ofp_print_port_features(string, ntohl(port->curr));
627 }
628 if (port->advertised) {
629 ds_put_format(string, " advertised: ");
630 ofp_print_port_features(string, ntohl(port->advertised));
631 }
632 if (port->supported) {
633 ds_put_format(string, " supported: ");
634 ofp_print_port_features(string, ntohl(port->supported));
635 }
636 if (port->peer) {
637 ds_put_format(string, " peer: ");
638 ofp_print_port_features(string, ntohl(port->peer));
639 }
640}
641
064af421 642static void
d1e2cf21
BP
643ofp_print_switch_features(struct ds *string,
644 const struct ofp_switch_features *osf)
064af421 645{
d1e2cf21 646 size_t len = ntohs(osf->header.length);
064af421
BP
647 struct ofp_phy_port *port_list;
648 int n_ports;
649 int i;
650
d295e8e9 651 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
064af421
BP
652 osf->header.version, ntohll(osf->datapath_id));
653 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
654 ntohl(osf->n_buffers));
655 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
656 ntohl(osf->capabilities), ntohl(osf->actions));
657
064af421
BP
658 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
659
d295e8e9 660 port_list = xmemdup(osf->ports, len - sizeof *osf);
064af421
BP
661 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
662 for (i = 0; i < n_ports; i++) {
663 ofp_print_phy_port(string, &port_list[i]);
664 }
665 free(port_list);
666}
667
064af421 668static void
d1e2cf21 669ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
064af421 670{
064af421
BP
671 uint16_t flags;
672
673 flags = ntohs(osc->flags);
3b62feba
BP
674
675 ds_put_cstr(string, " frags=");
676 switch (flags & OFPC_FRAG_MASK) {
677 case OFPC_FRAG_NORMAL:
678 ds_put_cstr(string, "normal");
679 flags &= ~OFPC_FRAG_MASK;
680 break;
681 case OFPC_FRAG_DROP:
682 ds_put_cstr(string, "drop");
683 flags &= ~OFPC_FRAG_MASK;
684 break;
685 case OFPC_FRAG_REASM:
686 ds_put_cstr(string, "reassemble");
687 flags &= ~OFPC_FRAG_MASK;
688 break;
689 }
064af421
BP
690 if (flags) {
691 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
692 }
693
694 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
695}
696
697static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 698 int verbosity, const char *format, ...)
064af421
BP
699 __attribute__((format(printf, 5, 6)));
700
701static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 702 int verbosity, const char *format, ...)
064af421
BP
703{
704 if (is_wild && verbosity < 2) {
705 return;
706 }
707 ds_put_cstr(string, leader);
708 if (!is_wild) {
709 va_list args;
710
711 va_start(args, format);
712 ds_put_format_valist(string, format, args);
713 va_end(args);
714 } else {
715 ds_put_char(string, '*');
716 }
717 ds_put_char(string, ',');
718}
719
720static void
dbba996b 721print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
064af421
BP
722 uint32_t wild_bits, int verbosity)
723{
724 if (wild_bits >= 32 && verbosity < 2) {
725 return;
726 }
727 ds_put_cstr(string, leader);
728 if (wild_bits < 32) {
729 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
730 if (wild_bits) {
731 ds_put_format(string, "/%d", 32 - wild_bits);
732 }
733 } else {
734 ds_put_char(string, '*');
735 }
736 ds_put_char(string, ',');
737}
738
4f2cad2c 739void
064af421
BP
740ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
741{
742 char *s = ofp_match_to_string(om, verbosity);
743 ds_put_cstr(f, s);
744 free(s);
745}
746
747char *
748ofp_match_to_string(const struct ofp_match *om, int verbosity)
749{
750 struct ds f = DS_EMPTY_INITIALIZER;
751 uint32_t w = ntohl(om->wildcards);
752 bool skip_type = false;
753 bool skip_proto = false;
754
755 if (!(w & OFPFW_DL_TYPE)) {
756 skip_type = true;
757 if (om->dl_type == htons(ETH_TYPE_IP)) {
758 if (!(w & OFPFW_NW_PROTO)) {
759 skip_proto = true;
6767a2cc 760 if (om->nw_proto == IPPROTO_ICMP) {
064af421 761 ds_put_cstr(&f, "icmp,");
6767a2cc 762 } else if (om->nw_proto == IPPROTO_TCP) {
064af421 763 ds_put_cstr(&f, "tcp,");
6767a2cc 764 } else if (om->nw_proto == IPPROTO_UDP) {
064af421
BP
765 ds_put_cstr(&f, "udp,");
766 } else {
767 ds_put_cstr(&f, "ip,");
768 skip_proto = false;
769 }
770 } else {
771 ds_put_cstr(&f, "ip,");
772 }
773 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
774 ds_put_cstr(&f, "arp,");
775 } else {
776 skip_type = false;
777 }
778 }
779 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
780 "%d", ntohs(om->in_port));
781 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
6a1f89c8 782 "%d", ntohs(om->dl_vlan));
959a2ecd
JP
783 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
784 "%d", om->dl_vlan_pcp);
064af421
BP
785 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
786 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
787 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
788 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
789 if (!skip_type) {
790 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
791 "0x%04x", ntohs(om->dl_type));
792 }
793 print_ip_netmask(&f, "nw_src=", om->nw_src,
794 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
795 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
796 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
797 if (!skip_proto) {
fb892732 798 if (om->dl_type == htons(ETH_TYPE_ARP)) {
fb115f91 799 print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
fb892732
JP
800 "%u", om->nw_proto);
801 } else {
802 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
803 "%u", om->nw_proto);
804 }
064af421 805 }
1a960c80
RL
806 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
807 "%u", om->nw_tos);
6767a2cc 808 if (om->nw_proto == IPPROTO_ICMP) {
064af421
BP
809 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
810 "%d", ntohs(om->icmp_type));
811 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
812 "%d", ntohs(om->icmp_code));
813 } else {
814 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
815 "%d", ntohs(om->tp_src));
816 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
817 "%d", ntohs(om->tp_dst));
818 }
7fa91113
BP
819 if (ds_last(&f) == ',') {
820 f.length--;
821 }
064af421
BP
822 return ds_cstr(&f);
823}
824
064af421 825static void
7fa91113
BP
826ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
827 enum ofputil_msg_code code, int verbosity)
064af421 828{
7fa91113 829 struct flow_mod fm;
f904747b 830 bool need_priority;
7fa91113 831 int error;
064af421 832
00794817 833 error = ofputil_decode_flow_mod(&fm, oh, true);
7fa91113
BP
834 if (error) {
835 ofp_print_error(s, error);
836 return;
3ff4f871
BP
837 }
838
7fa91113
BP
839 ds_put_char(s, ' ');
840 switch (fm.command) {
064af421 841 case OFPFC_ADD:
7fa91113 842 ds_put_cstr(s, "ADD");
064af421
BP
843 break;
844 case OFPFC_MODIFY:
7fa91113 845 ds_put_cstr(s, "MOD");
064af421
BP
846 break;
847 case OFPFC_MODIFY_STRICT:
7fa91113 848 ds_put_cstr(s, "MOD_STRICT");
064af421
BP
849 break;
850 case OFPFC_DELETE:
7fa91113 851 ds_put_cstr(s, "DEL");
064af421
BP
852 break;
853 case OFPFC_DELETE_STRICT:
7fa91113 854 ds_put_cstr(s, "DEL_STRICT");
064af421
BP
855 break;
856 default:
7fa91113
BP
857 ds_put_format(s, "cmd:%d", fm.command);
858 }
6c1491fb 859 if (fm.table_id != 0) {
e896c2d4 860 ds_put_format(s, " table:%d", fm.table_id);
6c1491fb 861 }
7fa91113
BP
862
863 ds_put_char(s, ' ');
864 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
865 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
7fa91113 866 ofp_print_match(s, &ofm->match, verbosity);
f904747b
BP
867
868 /* ofp_print_match() doesn't print priority. */
869 need_priority = true;
7fa91113
BP
870 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
871 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
872 const void *nxm = nfm + 1;
f904747b
BP
873 char *nxm_s;
874
875 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
7fa91113
BP
876 ds_put_cstr(s, nxm_s);
877 free(nxm_s);
f904747b
BP
878
879 /* nx_match_to_string() doesn't print priority. */
880 need_priority = true;
7fa91113
BP
881 } else {
882 cls_rule_format(&fm.cr, s);
f904747b
BP
883
884 /* cls_rule_format() does print priority. */
885 need_priority = false;
3ff4f871 886 }
7fa91113
BP
887
888 if (ds_last(s) != ' ') {
889 ds_put_char(s, ' ');
3ff4f871 890 }
7fa91113
BP
891 if (fm.cookie != htonll(0)) {
892 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
3ff4f871 893 }
7fa91113
BP
894 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
895 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
064af421 896 }
7fa91113
BP
897 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
898 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
3ff4f871 899 }
f904747b 900 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
7fa91113 901 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
3ff4f871 902 }
7fa91113
BP
903 if (fm.buffer_id != UINT32_MAX) {
904 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
3ff4f871 905 }
7fa91113
BP
906 if (fm.flags != 0) {
907 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
908 }
909
910 ofp_print_actions(s, (const struct ofp_action_header *) fm.actions,
911 fm.n_actions * sizeof *fm.actions);
064af421
BP
912}
913
09862ec6
BP
914static void
915ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
916{
917 ds_put_format(string, "%u", sec);
918 if (nsec > 0) {
919 ds_put_format(string, ".%09u", nsec);
920 while (string->string[string->length - 1] == '0') {
921 string->length--;
922 }
923 }
924 ds_put_char(string, 's');
925}
926
064af421 927static void
9b045a0c 928ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
064af421 929{
9b045a0c
BP
930 struct ofputil_flow_removed fr;
931 int error;
932
b78f6b77 933 error = ofputil_decode_flow_removed(&fr, oh);
9b045a0c
BP
934 if (error) {
935 ofp_print_error(string, error);
936 return;
937 }
938
fbd76b2e 939 ds_put_char(string, ' ');
9b045a0c
BP
940 cls_rule_format(&fr.rule, string);
941
064af421 942 ds_put_cstr(string, " reason=");
9b045a0c 943 switch (fr.reason) {
ca069229 944 case OFPRR_IDLE_TIMEOUT:
064af421
BP
945 ds_put_cstr(string, "idle");
946 break;
ca069229 947 case OFPRR_HARD_TIMEOUT:
064af421
BP
948 ds_put_cstr(string, "hard");
949 break;
ca069229
JP
950 case OFPRR_DELETE:
951 ds_put_cstr(string, "delete");
952 break;
064af421 953 default:
9b045a0c 954 ds_put_format(string, "**%"PRIu8"**", fr.reason);
064af421
BP
955 break;
956 }
3ff4f871 957
9b045a0c
BP
958 if (fr.cookie != htonll(0)) {
959 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
3ff4f871 960 }
09862ec6 961 ds_put_cstr(string, " duration");
9b045a0c 962 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
09862ec6 963 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
9b045a0c 964 fr.idle_timeout, fr.packet_count, fr.byte_count);
064af421
BP
965}
966
967static void
d1e2cf21 968ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
064af421 969{
064af421 970 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
d295e8e9 971 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
064af421
BP
972 ntohl(opm->config), ntohl(opm->mask));
973 ds_put_format(string, " advertise: ");
974 if (opm->advertise) {
975 ofp_print_port_features(string, ntohl(opm->advertise));
976 } else {
977 ds_put_format(string, "UNCHANGED\n");
978 }
979}
980
7fa91113
BP
981static void
982ofp_print_error(struct ds *string, int error)
983{
7fa91113
BP
984 if (string->length) {
985 ds_put_char(string, ' ');
986 }
dc4762ed
BP
987 ds_put_cstr(string, "***decode error: ");
988 ofputil_format_error(string, error);
989 ds_put_cstr(string, "***\n");
f74be05a
JP
990}
991
064af421 992static void
d1e2cf21 993ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
064af421 994{
d1e2cf21 995 size_t len = ntohs(oem->header.length);
dc4762ed
BP
996 size_t payload_ofs, payload_len;
997 const void *payload;
998 int error;
064af421
BP
999 char *s;
1000
dc4762ed
BP
1001 error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
1002 if (!is_ofp_error(error)) {
1003 ofp_print_error(string, error);
1004 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1005 return;
f74be05a
JP
1006 }
1007
dc4762ed
BP
1008 ds_put_char(string, ' ');
1009 ofputil_format_error(string, error);
1010 ds_put_char(string, '\n');
064af421 1011
dc4762ed
BP
1012 payload = (const uint8_t *) oem + payload_ofs;
1013 payload_len = len - payload_ofs;
1014 switch (get_ofp_err_type(error)) {
064af421 1015 case OFPET_HELLO_FAILED:
dc4762ed 1016 ds_put_printable(string, payload, payload_len);
064af421
BP
1017 break;
1018
1019 case OFPET_BAD_REQUEST:
dc4762ed 1020 s = ofp_to_string(payload, payload_len, 1);
064af421
BP
1021 ds_put_cstr(string, s);
1022 free(s);
1023 break;
1024
1025 default:
dc4762ed 1026 ds_put_hex_dump(string, payload, payload_len, 0, true);
064af421
BP
1027 break;
1028 }
1029}
1030
064af421 1031static void
d1e2cf21 1032ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
064af421 1033{
064af421
BP
1034 if (ops->reason == OFPPR_ADD) {
1035 ds_put_format(string, " ADD:");
1036 } else if (ops->reason == OFPPR_DELETE) {
1037 ds_put_format(string, " DEL:");
1038 } else if (ops->reason == OFPPR_MODIFY) {
1039 ds_put_format(string, " MOD:");
1040 }
1041
1042 ofp_print_phy_port(string, &ops->desc);
1043}
1044
1045static void
63f2140a 1046ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
064af421 1047{
fbd76b2e 1048 ds_put_char(string, '\n');
d295e8e9 1049 ds_put_format(string, "Manufacturer: %.*s\n",
dd70b475
JP
1050 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1051 ds_put_format(string, "Hardware: %.*s\n",
1052 (int) sizeof ods->hw_desc, ods->hw_desc);
1053 ds_put_format(string, "Software: %.*s\n",
1054 (int) sizeof ods->sw_desc, ods->sw_desc);
1055 ds_put_format(string, "Serial Num: %.*s\n",
1056 (int) sizeof ods->serial_num, ods->serial_num);
1057 ds_put_format(string, "DP Description: %.*s\n",
1058 (int) sizeof ods->dp_desc, ods->dp_desc);
064af421
BP
1059}
1060
1061static void
76c93b22
BP
1062ofp_print_flow_stats_request(struct ds *string,
1063 const struct ofp_stats_msg *osm)
064af421 1064{
2af0c0ef
BP
1065 struct flow_stats_request fsr;
1066 int error;
064af421 1067
76c93b22 1068 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
2af0c0ef
BP
1069 if (error) {
1070 ofp_print_error(string, error);
1071 return;
1072 }
1073
1074 if (fsr.table_id != 0xff) {
e896c2d4 1075 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
064af421
BP
1076 }
1077
2af0c0ef
BP
1078 if (fsr.out_port != OFPP_NONE) {
1079 ds_put_cstr(string, " out_port=");
1080 ofp_print_port_name(string, fsr.out_port);
1081 }
1082
54ae6fa8
BP
1083 /* A flow stats request doesn't include a priority, but cls_rule_format()
1084 * will print one unless it is OFP_DEFAULT_PRIORITY. */
1085 fsr.match.priority = OFP_DEFAULT_PRIORITY;
1086
2af0c0ef
BP
1087 ds_put_char(string, ' ');
1088 cls_rule_format(&fsr.match, string);
064af421
BP
1089}
1090
1091static void
4ffd1b43 1092ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
064af421 1093{
4ffd1b43 1094 struct ofpbuf b;
064af421 1095
4ffd1b43
BP
1096 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1097 for (;;) {
1098 struct ofputil_flow_stats fs;
1099 int retval;
fab8fadb 1100
b78f6b77 1101 retval = ofputil_decode_flow_stats_reply(&fs, &b);
4ffd1b43
BP
1102 if (retval) {
1103 if (retval != EOF) {
1104 ds_put_cstr(string, " ***parse error***");
064af421
BP
1105 }
1106 break;
1107 }
1108
8961de6a
BP
1109 ds_put_char(string, '\n');
1110
09862ec6 1111 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
4ffd1b43
BP
1112 ntohll(fs.cookie));
1113 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
e896c2d4 1114 ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
4ffd1b43
BP
1115 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1116 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1117 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1118 ds_put_format(string, "idle_timeout=%"PRIu16",", fs.idle_timeout);
c6430da5 1119 }
4ffd1b43
BP
1120 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1121 ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
c6430da5
BP
1122 }
1123
4ffd1b43 1124 cls_rule_format(&fs.rule, string);
c6430da5 1125 ds_put_char(string, ' ');
4ffd1b43
BP
1126 ofp_print_actions(string,
1127 (const struct ofp_action_header *) fs.actions,
1128 fs.n_actions * sizeof *fs.actions);
c6430da5
BP
1129 }
1130}
1131
064af421 1132static void
63f2140a
BP
1133ofp_print_ofpst_aggregate_reply(struct ds *string,
1134 const struct ofp_aggregate_stats_reply *asr)
064af421 1135{
c4617b3c
BP
1136 ds_put_format(string, " packet_count=%"PRIu64,
1137 ntohll(get_32aligned_be64(&asr->packet_count)));
1138 ds_put_format(string, " byte_count=%"PRIu64,
1139 ntohll(get_32aligned_be64(&asr->byte_count)));
064af421
BP
1140 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1141}
1142
a2ad9ecd
BP
1143static void
1144ofp_print_nxst_aggregate_reply(struct ds *string,
1145 const struct nx_aggregate_stats_reply *nasr)
1146{
675baf0c
BP
1147 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1148 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1149 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
a2ad9ecd
BP
1150}
1151
d295e8e9 1152static void print_port_stat(struct ds *string, const char *leader,
c4617b3c 1153 const ovs_32aligned_be64 *statp, int more)
064af421 1154{
c4617b3c
BP
1155 uint64_t stat = ntohll(get_32aligned_be64(statp));
1156
064af421 1157 ds_put_cstr(string, leader);
c4617b3c 1158 if (stat != UINT64_MAX) {
064af421
BP
1159 ds_put_format(string, "%"PRIu64, stat);
1160 } else {
1161 ds_put_char(string, '?');
1162 }
1163 if (more) {
1164 ds_put_cstr(string, ", ");
1165 } else {
1166 ds_put_cstr(string, "\n");
1167 }
1168}
1169
abaad8cf 1170static void
63f2140a
BP
1171ofp_print_ofpst_port_request(struct ds *string,
1172 const struct ofp_port_stats_request *psr)
abaad8cf 1173{
fbd76b2e 1174 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
abaad8cf
JP
1175}
1176
064af421 1177static void
d1e2cf21
BP
1178ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1179 int verbosity)
064af421 1180{
d1e2cf21
BP
1181 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1182 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
064af421
BP
1183 ds_put_format(string, " %zu ports\n", n);
1184 if (verbosity < 1) {
1185 return;
1186 }
1187
1188 for (; n--; ps++) {
1189 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1190
1191 ds_put_cstr(string, "rx ");
c4617b3c
BP
1192 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1193 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1194 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1195 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1196 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1197 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1198 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
064af421
BP
1199
1200 ds_put_cstr(string, " tx ");
c4617b3c
BP
1201 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1202 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1203 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1204 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1205 print_port_stat(string, "coll=", &ps->collisions, 0);
064af421
BP
1206 }
1207}
1208
1209static void
d1e2cf21
BP
1210ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1211 int verbosity)
064af421 1212{
d1e2cf21
BP
1213 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1214 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
064af421
BP
1215 ds_put_format(string, " %zu tables\n", n);
1216 if (verbosity < 1) {
1217 return;
1218 }
1219
1220 for (; n--; ts++) {
1221 char name[OFP_MAX_TABLE_NAME_LEN + 1];
e868fb3d 1222 ovs_strlcpy(name, ts->name, sizeof name);
064af421
BP
1223
1224 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1225 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1226 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1227 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1228 ds_put_cstr(string, " ");
d295e8e9 1229 ds_put_format(string, "lookup=%"PRIu64", ",
c4617b3c 1230 ntohll(get_32aligned_be64(&ts->lookup_count)));
064af421 1231 ds_put_format(string, "matched=%"PRIu64"\n",
c4617b3c 1232 ntohll(get_32aligned_be64(&ts->matched_count)));
064af421
BP
1233 }
1234}
1235
d2805da2
BP
1236static void
1237ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1238{
1239 if (queue_id == OFPQ_ALL) {
1240 ds_put_cstr(string, "ALL");
1241 } else {
1242 ds_put_format(string, "%"PRIu32, queue_id);
1243 }
1244}
1245
1246static void
63f2140a
BP
1247ofp_print_ofpst_queue_request(struct ds *string,
1248 const struct ofp_queue_stats_request *qsr)
d2805da2 1249{
d2805da2
BP
1250 ds_put_cstr(string, "port=");
1251 ofp_print_port_name(string, ntohs(qsr->port_no));
1252
1253 ds_put_cstr(string, " queue=");
1254 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1255}
1256
1257static void
d1e2cf21
BP
1258ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1259 int verbosity)
d2805da2 1260{
d1e2cf21
BP
1261 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1262 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
d2805da2
BP
1263 ds_put_format(string, " %zu queues\n", n);
1264 if (verbosity < 1) {
1265 return;
1266 }
1267
1268 for (; n--; qs++) {
1269 ds_put_cstr(string, " port ");
1270 ofp_print_port_name(string, ntohs(qs->port_no));
1271 ds_put_cstr(string, " queue ");
1272 ofp_print_queue_name(string, ntohl(qs->queue_id));
1273 ds_put_cstr(string, ": ");
1274
c4617b3c
BP
1275 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1276 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1277 print_port_stat(string, "errors=", &qs->tx_errors, 0);
d2805da2
BP
1278 }
1279}
1280
064af421 1281static void
d1e2cf21 1282ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
064af421 1283{
28c8bad1 1284 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
064af421
BP
1285
1286 if (srq->flags) {
1287 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1288 ntohs(srq->flags));
1289 }
064af421
BP
1290}
1291
1292static void
d1e2cf21 1293ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
064af421 1294{
28c8bad1 1295 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
064af421 1296
d1e2cf21 1297 if (srp->flags) {
064af421 1298 uint16_t flags = ntohs(srp->flags);
d1e2cf21
BP
1299
1300 ds_put_cstr(string, " flags=");
064af421
BP
1301 if (flags & OFPSF_REPLY_MORE) {
1302 ds_put_cstr(string, "[more]");
1303 flags &= ~OFPSF_REPLY_MORE;
1304 }
1305 if (flags) {
d1e2cf21
BP
1306 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1307 flags);
064af421
BP
1308 }
1309 }
064af421
BP
1310}
1311
1312static void
d1e2cf21 1313ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
064af421 1314{
d1e2cf21 1315 size_t len = ntohs(oh->length);
064af421 1316
d1e2cf21 1317 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
064af421 1318 if (verbosity > 1) {
d1e2cf21 1319 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
064af421
BP
1320 }
1321}
1322
61fe3a7b
BP
1323static void
1324ofp_print_nxt_role_message(struct ds *string,
1325 const struct nx_role_request *nrr)
1326{
1327 unsigned int role = ntohl(nrr->role);
1328
1329 ds_put_cstr(string, " role=");
1330 if (role == NX_ROLE_OTHER) {
1331 ds_put_cstr(string, "other");
1332 } else if (role == NX_ROLE_MASTER) {
1333 ds_put_cstr(string, "master");
1334 } else if (role == NX_ROLE_SLAVE) {
1335 ds_put_cstr(string, "slave");
1336 } else {
1337 ds_put_format(string, "%u", role);
1338 }
1339}
1340
6c1491fb
BP
1341static void
1342ofp_print_nxt_flow_mod_table_id(struct ds *string,
1343 const struct nxt_flow_mod_table_id *nfmti)
1344{
1345 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1346}
1347
7fa91113
BP
1348static void
1349ofp_print_nxt_set_flow_format(struct ds *string,
1350 const struct nxt_set_flow_format *nsff)
1351{
1352 uint32_t format = ntohl(nsff->format);
1353
1354 ds_put_cstr(string, " format=");
1355 if (ofputil_flow_format_is_valid(format)) {
1356 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1357 } else {
1358 ds_put_format(string, "%"PRIu32, format);
1359 }
1360}
1361
d1e2cf21
BP
1362static void
1363ofp_to_string__(const struct ofp_header *oh,
1364 const struct ofputil_msg_type *type, struct ds *string,
1365 int verbosity)
1366{
7fa91113 1367 enum ofputil_msg_code code;
d1e2cf21
BP
1368 const void *msg = oh;
1369
1370 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1371 ofputil_msg_type_name(type), ntohl(oh->xid));
1372
7fa91113
BP
1373 code = ofputil_msg_type_code(type);
1374 switch (code) {
d1e2cf21
BP
1375 case OFPUTIL_INVALID:
1376 break;
1377
1378 case OFPUTIL_OFPT_HELLO:
dc4762ed
BP
1379 ds_put_char(string, '\n');
1380 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1381 0, true);
d1e2cf21
BP
1382 break;
1383
1384 case OFPUTIL_OFPT_ERROR:
1385 ofp_print_error_msg(string, msg);
1386 break;
1387
1388 case OFPUTIL_OFPT_ECHO_REQUEST:
1389 case OFPUTIL_OFPT_ECHO_REPLY:
1390 ofp_print_echo(string, oh, verbosity);
1391 break;
1392
1393 case OFPUTIL_OFPT_FEATURES_REQUEST:
1394 break;
1395
1396 case OFPUTIL_OFPT_FEATURES_REPLY:
1397 ofp_print_switch_features(string, msg);
1398 break;
1399
1400 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1401 break;
1402
1403 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1404 case OFPUTIL_OFPT_SET_CONFIG:
1405 ofp_print_switch_config(string, msg);
1406 break;
1407
1408 case OFPUTIL_OFPT_PACKET_IN:
1409 ofp_print_packet_in(string, msg, verbosity);
1410 break;
1411
1412 case OFPUTIL_OFPT_FLOW_REMOVED:
9b045a0c
BP
1413 case OFPUTIL_NXT_FLOW_REMOVED:
1414 ofp_print_flow_removed(string, msg);
d1e2cf21
BP
1415 break;
1416
1417 case OFPUTIL_OFPT_PORT_STATUS:
1418 ofp_print_port_status(string, msg);
1419 break;
1420
1421 case OFPUTIL_OFPT_PACKET_OUT:
1422 ofp_print_packet_out(string, msg, verbosity);
1423 break;
1424
1425 case OFPUTIL_OFPT_FLOW_MOD:
7fa91113 1426 ofp_print_flow_mod(string, msg, code, verbosity);
d1e2cf21
BP
1427 break;
1428
1429 case OFPUTIL_OFPT_PORT_MOD:
1430 ofp_print_port_mod(string, msg);
1431 break;
1432
1433 case OFPUTIL_OFPT_BARRIER_REQUEST:
1434 case OFPUTIL_OFPT_BARRIER_REPLY:
1435 break;
1436
1437 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1438 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1439 /* XXX */
1440 break;
1441
1442 case OFPUTIL_OFPST_DESC_REQUEST:
1443 ofp_print_stats_request(string, oh);
1444 break;
1445
1446 case OFPUTIL_OFPST_FLOW_REQUEST:
2af0c0ef 1447 case OFPUTIL_NXST_FLOW_REQUEST:
d1e2cf21 1448 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
2af0c0ef 1449 case OFPUTIL_NXST_AGGREGATE_REQUEST:
d1e2cf21 1450 ofp_print_stats_request(string, oh);
76c93b22 1451 ofp_print_flow_stats_request(string, msg);
d1e2cf21
BP
1452 break;
1453
1454 case OFPUTIL_OFPST_TABLE_REQUEST:
1455 ofp_print_stats_request(string, oh);
1456 break;
1457
1458 case OFPUTIL_OFPST_PORT_REQUEST:
1459 ofp_print_stats_request(string, oh);
63f2140a 1460 ofp_print_ofpst_port_request(string, msg);
d1e2cf21
BP
1461 break;
1462
1463 case OFPUTIL_OFPST_QUEUE_REQUEST:
1464 ofp_print_stats_request(string, oh);
63f2140a 1465 ofp_print_ofpst_queue_request(string, msg);
d1e2cf21
BP
1466 break;
1467
1468 case OFPUTIL_OFPST_DESC_REPLY:
1469 ofp_print_stats_reply(string, oh);
63f2140a 1470 ofp_print_ofpst_desc_reply(string, msg);
d1e2cf21
BP
1471 break;
1472
1473 case OFPUTIL_OFPST_FLOW_REPLY:
4ffd1b43 1474 case OFPUTIL_NXST_FLOW_REPLY:
d1e2cf21 1475 ofp_print_stats_reply(string, oh);
4ffd1b43 1476 ofp_print_flow_stats_reply(string, oh);
d1e2cf21
BP
1477 break;
1478
1479 case OFPUTIL_OFPST_QUEUE_REPLY:
1480 ofp_print_stats_reply(string, oh);
1481 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1482 break;
1483
1484 case OFPUTIL_OFPST_PORT_REPLY:
1485 ofp_print_stats_reply(string, oh);
1486 ofp_print_ofpst_port_reply(string, oh, verbosity);
1487 break;
1488
1489 case OFPUTIL_OFPST_TABLE_REPLY:
1490 ofp_print_stats_reply(string, oh);
1491 ofp_print_ofpst_table_reply(string, oh, verbosity);
1492 break;
1493
1494 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1495 ofp_print_stats_reply(string, oh);
63f2140a 1496 ofp_print_ofpst_aggregate_reply(string, msg);
d1e2cf21 1497 break;
064af421 1498
d1e2cf21
BP
1499 case OFPUTIL_NXT_ROLE_REQUEST:
1500 case OFPUTIL_NXT_ROLE_REPLY:
61fe3a7b 1501 ofp_print_nxt_role_message(string, msg);
7fa91113
BP
1502 break;
1503
6c1491fb
BP
1504 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1505 ofp_print_nxt_flow_mod_table_id(string, msg);
1506 break;
1507
d1e2cf21 1508 case OFPUTIL_NXT_SET_FLOW_FORMAT:
7fa91113
BP
1509 ofp_print_nxt_set_flow_format(string, msg);
1510 break;
1511
d1e2cf21 1512 case OFPUTIL_NXT_FLOW_MOD:
7fa91113
BP
1513 ofp_print_flow_mod(string, msg, code, verbosity);
1514 break;
1515
d1e2cf21 1516 case OFPUTIL_NXST_AGGREGATE_REPLY:
a2ad9ecd 1517 ofp_print_stats_reply(string, oh);
9b045a0c 1518 ofp_print_nxst_aggregate_reply(string, msg);
d1e2cf21 1519 break;
246e61ea 1520 }
d1e2cf21 1521}
064af421
BP
1522
1523/* Composes and returns a string representing the OpenFlow packet of 'len'
1524 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1525 * verbosity and higher numbers increase verbosity. The caller is responsible
1526 * for freeing the string. */
1527char *
1528ofp_to_string(const void *oh_, size_t len, int verbosity)
1529{
1530 struct ds string = DS_EMPTY_INITIALIZER;
1531 const struct ofp_header *oh = oh_;
064af421 1532
49ad0403
BP
1533 if (!len) {
1534 ds_put_cstr(&string, "OpenFlow message is empty\n");
1535 } else if (len < sizeof(struct ofp_header)) {
1536 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1537 len);
064af421 1538 } else if (oh->version != OFP_VERSION) {
d1e2cf21
BP
1539 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1540 oh->version);
1541 } else if (ntohs(oh->length) > len) {
1542 ds_put_format(&string,
49ad0403 1543 "(***truncated to %zu bytes from %"PRIu16"***)\n",
d1e2cf21
BP
1544 len, ntohs(oh->length));
1545 } else if (ntohs(oh->length) < len) {
1546 ds_put_format(&string,
1547 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1548 ntohs(oh->length), len);
1549 } else {
1550 const struct ofputil_msg_type *type;
d1e2cf21
BP
1551 int error;
1552
1553 error = ofputil_decode_msg_type(oh, &type);
1554 if (!error) {
1555 ofp_to_string__(oh, type, &string, verbosity);
7fa91113
BP
1556 if (verbosity >= 5) {
1557 if (ds_last(&string) != '\n') {
1558 ds_put_char(&string, '\n');
1559 }
d1e2cf21
BP
1560 ds_put_hex_dump(&string, oh, len, 0, true);
1561 }
7fa91113 1562 if (ds_last(&string) != '\n') {
d1e2cf21
BP
1563 ds_put_char(&string, '\n');
1564 }
1565 return ds_steal_cstr(&string);
064af421 1566 }
064af421 1567
7fa91113 1568 ofp_print_error(&string, error);
064af421 1569 }
d1e2cf21
BP
1570 ds_put_hex_dump(&string, oh, len, 0, true);
1571 return ds_steal_cstr(&string);
064af421
BP
1572}
1573
1574/* Returns the name for the specified OpenFlow message type as a string,
1575 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1576 * hex number, e.g. "0x55".
1577 *
1578 * The caller must free the returned string when it is no longer needed. */
1579char *
1580ofp_message_type_to_string(uint8_t type)
1581{
d1e2cf21 1582 const char *name;
064af421 1583
d1e2cf21
BP
1584 switch (type) {
1585 case OFPT_HELLO:
1586 name = "HELLO";
1587 break;
1588 case OFPT_ERROR:
1589 name = "ERROR";
1590 break;
1591 case OFPT_ECHO_REQUEST:
1592 name = "ECHO_REQUEST";
1593 break;
1594 case OFPT_ECHO_REPLY:
1595 name = "ECHO_REPLY";
1596 break;
1597 case OFPT_VENDOR:
1598 name = "VENDOR";
1599 break;
1600 case OFPT_FEATURES_REQUEST:
1601 name = "FEATURES_REQUEST";
1602 break;
1603 case OFPT_FEATURES_REPLY:
1604 name = "FEATURES_REPLY";
1605 break;
1606 case OFPT_GET_CONFIG_REQUEST:
1607 name = "GET_CONFIG_REQUEST";
1608 break;
1609 case OFPT_GET_CONFIG_REPLY:
1610 name = "GET_CONFIG_REPLY";
1611 break;
1612 case OFPT_SET_CONFIG:
1613 name = "SET_CONFIG";
1614 break;
1615 case OFPT_PACKET_IN:
1616 name = "PACKET_IN";
1617 break;
1618 case OFPT_FLOW_REMOVED:
1619 name = "FLOW_REMOVED";
1620 break;
1621 case OFPT_PORT_STATUS:
1622 name = "PORT_STATUS";
1623 break;
1624 case OFPT_PACKET_OUT:
1625 name = "PACKET_OUT";
1626 break;
1627 case OFPT_FLOW_MOD:
1628 name = "FLOW_MOD";
1629 break;
1630 case OFPT_PORT_MOD:
1631 name = "PORT_MOD";
1632 break;
1633 case OFPT_STATS_REQUEST:
1634 name = "STATS_REQUEST";
1635 break;
1636 case OFPT_STATS_REPLY:
1637 name = "STATS_REPLY";
1638 break;
1639 case OFPT_BARRIER_REQUEST:
1640 name = "BARRIER_REQUEST";
1641 break;
1642 case OFPT_BARRIER_REPLY:
1643 name = "BARRIER_REPLY";
1644 break;
1645 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1646 name = "QUEUE_GET_CONFIG_REQUEST";
1647 break;
1648 case OFPT_QUEUE_GET_CONFIG_REPLY:
1649 name = "QUEUE_GET_CONFIG_REPLY";
1650 break;
1651 default:
1652 name = NULL;
1653 break;
064af421 1654 }
d1e2cf21
BP
1655
1656 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
064af421
BP
1657}
1658\f
1659static void
d295e8e9 1660print_and_free(FILE *stream, char *string)
064af421
BP
1661{
1662 fputs(string, stream);
1663 free(string);
1664}
1665
1666/* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1667 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1668 * numbers increase verbosity. */
1669void
1670ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1671{
1672 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1673}
1674
1675/* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1676 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1677 * the Ethernet frame (of which 'len' bytes were captured).
1678 *
1679 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1680void
1681ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1682{
1683 print_and_free(stream, ofp_packet_to_string(data, len, total_len));
1684}