tunneling: Add support for tunnel ID.
[sliver-openvswitch.git] / lib / odp-util.c
1 /*
2  * Copyright (c) 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 "odp-util.h"
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "coverage.h"
23 #include "dynamic-string.h"
24 #include "flow.h"
25 #include "packets.h"
26 #include "timeval.h"
27 #include "util.h"
28
29 union odp_action *
30 odp_actions_add(struct odp_actions *actions, uint16_t type)
31 {
32     union odp_action *a;
33     if (actions->n_actions < MAX_ODP_ACTIONS) {
34         a = &actions->actions[actions->n_actions++];
35     } else {
36         COVERAGE_INC(odp_overflow);
37         actions->n_actions = MAX_ODP_ACTIONS + 1;
38         a = &actions->actions[MAX_ODP_ACTIONS - 1];
39     }
40     memset(a, 0, sizeof *a);
41     a->type = type;
42     return a;
43 }
44
45 void
46 format_odp_action(struct ds *ds, const union odp_action *a)
47 {
48     switch (a->type) {
49     case ODPAT_OUTPUT:
50         ds_put_format(ds, "%"PRIu16, a->output.port);
51         break;
52     case ODPAT_OUTPUT_GROUP:
53         ds_put_format(ds, "g%"PRIu16, a->output_group.group);
54         break;
55     case ODPAT_CONTROLLER:
56         ds_put_format(ds, "ctl(%"PRIu32")", a->controller.arg);
57         break;
58     case ODPAT_SET_TUNNEL:
59         ds_put_format(ds, "set_tunnel(0x%08"PRIx32")", ntohl(a->tunnel.tun_id));
60         break;
61     case ODPAT_SET_VLAN_VID:
62         ds_put_format(ds, "set_vlan(%"PRIu16")", ntohs(a->vlan_vid.vlan_vid));
63         break;
64     case ODPAT_SET_VLAN_PCP:
65         ds_put_format(ds, "set_vlan_pcp(%"PRIu8")", a->vlan_pcp.vlan_pcp);
66         break;
67     case ODPAT_STRIP_VLAN:
68         ds_put_format(ds, "strip_vlan");
69         break;
70     case ODPAT_SET_DL_SRC:
71         ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")",
72                ETH_ADDR_ARGS(a->dl_addr.dl_addr));
73         break;
74     case ODPAT_SET_DL_DST:
75         ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")",
76                ETH_ADDR_ARGS(a->dl_addr.dl_addr));
77         break;
78     case ODPAT_SET_NW_SRC:
79         ds_put_format(ds, "set_nw_src("IP_FMT")",
80                       IP_ARGS(&a->nw_addr.nw_addr));
81         break;
82     case ODPAT_SET_NW_DST:
83         ds_put_format(ds, "set_nw_dst("IP_FMT")",
84                       IP_ARGS(&a->nw_addr.nw_addr));
85         break;
86     case ODPAT_SET_NW_TOS:
87         ds_put_format(ds, "set_nw_tos(%"PRIu8")", a->nw_tos.nw_tos);
88         break;
89     case ODPAT_SET_TP_SRC:
90         ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(a->tp_port.tp_port));
91         break;
92     case ODPAT_SET_TP_DST:
93         ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(a->tp_port.tp_port));
94         break;
95     default:
96         ds_put_format(ds, "***bad action 0x%"PRIx16"***", a->type);
97         break;
98     }
99 }
100
101 void
102 format_odp_actions(struct ds *ds, const union odp_action *actions,
103                    size_t n_actions)
104 {
105     size_t i;
106     for (i = 0; i < n_actions; i++) {
107         if (i) {
108             ds_put_char(ds, ',');
109         }
110         format_odp_action(ds, &actions[i]);
111     }
112     if (!n_actions) {
113         ds_put_cstr(ds, "drop");
114     }
115 }
116
117 void
118 format_odp_flow_stats(struct ds *ds, const struct odp_flow_stats *s)
119 {
120     ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
121                   (unsigned long long int) s->n_packets,
122                   (unsigned long long int) s->n_bytes);
123     if (s->used_sec) {
124         long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
125         ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
126     } else {
127         ds_put_format(ds, "never");
128     }
129 }
130
131 void
132 format_odp_flow(struct ds *ds, const struct odp_flow *f)
133 {
134     flow_format(ds, &f->key);
135     ds_put_cstr(ds, ", ");
136     format_odp_flow_stats(ds, &f->stats);
137     ds_put_cstr(ds, ", actions:");
138     format_odp_actions(ds, f->actions, f->n_actions);
139 }
140