flow: Separate "flow_t" from "struct odp_flow_key".
[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     size_t idx;
34
35     idx = actions->n_actions++ & (MAX_ODP_ACTIONS - 1);
36     a = &actions->actions[idx];
37     memset(a, 0, sizeof *a);
38     a->type = type;
39     return a;
40 }
41
42 void
43 format_odp_flow_key(struct ds *ds, const struct odp_flow_key *key)
44 {
45     ds_put_format(ds, "in_port%04x:vlan%d:pcp%d mac"ETH_ADDR_FMT
46                   "->"ETH_ADDR_FMT" type%04x proto%"PRId8" tos%"PRIu8
47                   " ip"IP_FMT"->"IP_FMT" port%d->%d",
48                   key->in_port, ntohs(key->dl_vlan), key->dl_vlan_pcp,
49                   ETH_ADDR_ARGS(key->dl_src), ETH_ADDR_ARGS(key->dl_dst),
50                   ntohs(key->dl_type), key->nw_proto, key->nw_tos,
51                   IP_ARGS(&key->nw_src), IP_ARGS(&key->nw_dst),
52                   ntohs(key->tp_src), ntohs(key->tp_dst));
53 }
54
55 void
56 format_odp_action(struct ds *ds, const union odp_action *a)
57 {
58     switch (a->type) {
59     case ODPAT_OUTPUT:
60         ds_put_format(ds, "%"PRIu16, a->output.port);
61         break;
62     case ODPAT_CONTROLLER:
63         ds_put_format(ds, "ctl(%"PRIu32")", a->controller.arg);
64         break;
65     case ODPAT_SET_TUNNEL:
66         ds_put_format(ds, "set_tunnel(0x%08"PRIx32")", ntohl(a->tunnel.tun_id));
67         break;
68     case ODPAT_SET_VLAN_VID:
69         ds_put_format(ds, "set_vlan(%"PRIu16")", ntohs(a->vlan_vid.vlan_vid));
70         break;
71     case ODPAT_SET_VLAN_PCP:
72         ds_put_format(ds, "set_vlan_pcp(%"PRIu8")", a->vlan_pcp.vlan_pcp);
73         break;
74     case ODPAT_STRIP_VLAN:
75         ds_put_format(ds, "strip_vlan");
76         break;
77     case ODPAT_SET_DL_SRC:
78         ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")",
79                ETH_ADDR_ARGS(a->dl_addr.dl_addr));
80         break;
81     case ODPAT_SET_DL_DST:
82         ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")",
83                ETH_ADDR_ARGS(a->dl_addr.dl_addr));
84         break;
85     case ODPAT_SET_NW_SRC:
86         ds_put_format(ds, "set_nw_src("IP_FMT")",
87                       IP_ARGS(&a->nw_addr.nw_addr));
88         break;
89     case ODPAT_SET_NW_DST:
90         ds_put_format(ds, "set_nw_dst("IP_FMT")",
91                       IP_ARGS(&a->nw_addr.nw_addr));
92         break;
93     case ODPAT_SET_NW_TOS:
94         ds_put_format(ds, "set_nw_tos(%"PRIu8")", a->nw_tos.nw_tos);
95         break;
96     case ODPAT_SET_TP_SRC:
97         ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(a->tp_port.tp_port));
98         break;
99     case ODPAT_SET_TP_DST:
100         ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(a->tp_port.tp_port));
101         break;
102     case ODPAT_SET_PRIORITY:
103         ds_put_format(ds, "set_priority(0x%"PRIx32")", a->priority.priority);
104         break;
105     case ODPAT_POP_PRIORITY:
106         ds_put_cstr(ds, "pop_priority");
107         break;
108     case ODPAT_DROP_SPOOFED_ARP:
109         ds_put_cstr(ds, "drop_spoofed_arp");
110         break;
111     default:
112         ds_put_format(ds, "***bad action 0x%"PRIx16"***", a->type);
113         break;
114     }
115 }
116
117 void
118 format_odp_actions(struct ds *ds, const union odp_action *actions,
119                    size_t n_actions)
120 {
121     size_t i;
122     for (i = 0; i < n_actions; i++) {
123         if (i) {
124             ds_put_char(ds, ',');
125         }
126         format_odp_action(ds, &actions[i]);
127     }
128     if (!n_actions) {
129         ds_put_cstr(ds, "drop");
130     }
131 }
132
133 void
134 format_odp_flow_stats(struct ds *ds, const struct odp_flow_stats *s)
135 {
136     ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
137                   (unsigned long long int) s->n_packets,
138                   (unsigned long long int) s->n_bytes);
139     if (s->used_sec) {
140         long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
141         ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
142     } else {
143         ds_put_format(ds, "never");
144     }
145 }
146
147 void
148 format_odp_flow(struct ds *ds, const struct odp_flow *f)
149 {
150     format_odp_flow_key(ds, &f->key);
151     ds_put_cstr(ds, ", ");
152     format_odp_flow_stats(ds, &f->stats);
153     ds_put_cstr(ds, ", actions:");
154     format_odp_actions(ds, f->actions, f->n_actions);
155 }
156 \f
157 void
158 odp_flow_key_from_flow(struct odp_flow_key *key, const struct flow *flow)
159 {
160     key->tun_id = flow->tun_id;
161     key->nw_src = flow->nw_src;
162     key->nw_dst = flow->nw_dst;
163     key->in_port = flow->in_port;
164     key->dl_vlan = flow->dl_vlan;
165     key->dl_type = flow->dl_type;
166     key->tp_src = flow->tp_src;
167     key->tp_dst = flow->tp_dst;
168     memcpy(key->dl_src, flow->dl_src, ETH_ADDR_LEN);
169     memcpy(key->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
170     key->nw_proto = flow->nw_proto;
171     key->dl_vlan_pcp = flow->dl_vlan_pcp;
172     key->nw_tos = flow->nw_tos;
173     memset(key->reserved, 0, sizeof key->reserved);
174 }
175
176 void
177 odp_flow_key_to_flow(const struct odp_flow_key *key, struct flow *flow)
178 {
179     flow->tun_id = key->tun_id;
180     flow->nw_src = key->nw_src;
181     flow->nw_dst = key->nw_dst;
182     flow->in_port = key->in_port;
183     flow->dl_vlan = key->dl_vlan;
184     flow->dl_type = key->dl_type;
185     flow->tp_src = key->tp_src;
186     flow->tp_dst = key->tp_dst;
187     memcpy(flow->dl_src, key->dl_src, ETH_ADDR_LEN);
188     memcpy(flow->dl_dst, key->dl_dst, ETH_ADDR_LEN);
189     flow->nw_proto = key->nw_proto;
190     flow->dl_vlan_pcp = key->dl_vlan_pcp;
191     flow->nw_tos = key->nw_tos;
192 }