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