2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "byte-order.h"
25 #include "dynamic-string.h"
28 #include "openvswitch/tunnel.h"
34 odp_action_len(uint16_t type)
36 if (type > ODP_ACTION_ATTR_MAX) {
40 switch ((enum odp_action_type) type) {
41 case ODP_ACTION_ATTR_OUTPUT: return 4;
42 case ODP_ACTION_ATTR_CONTROLLER: return 8;
43 case ODP_ACTION_ATTR_SET_DL_TCI: return 2;
44 case ODP_ACTION_ATTR_STRIP_VLAN: return 0;
45 case ODP_ACTION_ATTR_SET_DL_SRC: return ETH_ADDR_LEN;
46 case ODP_ACTION_ATTR_SET_DL_DST: return ETH_ADDR_LEN;
47 case ODP_ACTION_ATTR_SET_NW_SRC: return 4;
48 case ODP_ACTION_ATTR_SET_NW_DST: return 4;
49 case ODP_ACTION_ATTR_SET_NW_TOS: return 1;
50 case ODP_ACTION_ATTR_SET_TP_SRC: return 2;
51 case ODP_ACTION_ATTR_SET_TP_DST: return 2;
52 case ODP_ACTION_ATTR_SET_TUNNEL: return 8;
53 case ODP_ACTION_ATTR_SET_PRIORITY: return 4;
54 case ODP_ACTION_ATTR_POP_PRIORITY: return 0;
55 case ODP_ACTION_ATTR_DROP_SPOOFED_ARP: return 0;
57 case ODP_ACTION_ATTR_UNSPEC:
58 case __ODP_ACTION_ATTR_MAX:
66 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
68 size_t len = nl_attr_get_size(a);
70 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
72 const uint8_t *unspec;
75 unspec = nl_attr_get(a);
76 for (i = 0; i < len; i++) {
77 ds_put_char(ds, i ? ' ': '(');
78 ds_put_format(ds, "%02x", unspec[i]);
85 format_odp_action(struct ds *ds, const struct nlattr *a)
90 if (nl_attr_get_size(a) != odp_action_len(nl_attr_type(a))) {
91 ds_put_format(ds, "bad length %zu, expected %d for: ",
92 nl_attr_get_size(a), odp_action_len(nl_attr_type(a)));
93 format_generic_odp_action(ds, a);
97 switch (nl_attr_type(a)) {
98 case ODP_ACTION_ATTR_OUTPUT:
99 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
101 case ODP_ACTION_ATTR_CONTROLLER:
102 ds_put_format(ds, "ctl(%"PRIu64")", nl_attr_get_u64(a));
104 case ODP_ACTION_ATTR_SET_TUNNEL:
105 ds_put_format(ds, "set_tunnel(%#"PRIx64")",
106 ntohll(nl_attr_get_be64(a)));
108 case ODP_ACTION_ATTR_SET_DL_TCI:
109 ds_put_format(ds, "set_tci(vid=%"PRIu16",pcp=%d)",
110 vlan_tci_to_vid(nl_attr_get_be16(a)),
111 vlan_tci_to_pcp(nl_attr_get_be16(a)));
113 case ODP_ACTION_ATTR_STRIP_VLAN:
114 ds_put_format(ds, "strip_vlan");
116 case ODP_ACTION_ATTR_SET_DL_SRC:
117 eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
118 ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
120 case ODP_ACTION_ATTR_SET_DL_DST:
121 eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
122 ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
124 case ODP_ACTION_ATTR_SET_NW_SRC:
125 ip = nl_attr_get_be32(a);
126 ds_put_format(ds, "set_nw_src("IP_FMT")", IP_ARGS(&ip));
128 case ODP_ACTION_ATTR_SET_NW_DST:
129 ip = nl_attr_get_be32(a);
130 ds_put_format(ds, "set_nw_dst("IP_FMT")", IP_ARGS(&ip));
132 case ODP_ACTION_ATTR_SET_NW_TOS:
133 ds_put_format(ds, "set_nw_tos(%"PRIu8")", nl_attr_get_u8(a));
135 case ODP_ACTION_ATTR_SET_TP_SRC:
136 ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
138 case ODP_ACTION_ATTR_SET_TP_DST:
139 ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
141 case ODP_ACTION_ATTR_SET_PRIORITY:
142 ds_put_format(ds, "set_priority(%#"PRIx32")", nl_attr_get_u32(a));
144 case ODP_ACTION_ATTR_POP_PRIORITY:
145 ds_put_cstr(ds, "pop_priority");
147 case ODP_ACTION_ATTR_DROP_SPOOFED_ARP:
148 ds_put_cstr(ds, "drop_spoofed_arp");
151 format_generic_odp_action(ds, a);
157 format_odp_actions(struct ds *ds, const struct nlattr *actions,
161 const struct nlattr *a;
164 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
166 ds_put_char(ds, ',');
168 format_odp_action(ds, a);
171 if (left == actions_len) {
172 ds_put_cstr(ds, "<empty>");
174 ds_put_format(ds, ",***%u leftover bytes***", left);
177 ds_put_cstr(ds, "drop");
181 /* Returns the correct length of the payload for a flow key attribute of the
182 * specified 'type', or -1 if 'type' is unknown. */
184 odp_flow_key_attr_len(uint16_t type)
186 if (type > ODP_KEY_ATTR_MAX) {
190 switch ((enum odp_key_type) type) {
191 case ODP_KEY_ATTR_TUN_ID: return 8;
192 case ODP_KEY_ATTR_IN_PORT: return 4;
193 case ODP_KEY_ATTR_ETHERNET: return sizeof(struct odp_key_ethernet);
194 case ODP_KEY_ATTR_8021Q: return sizeof(struct odp_key_8021q);
195 case ODP_KEY_ATTR_ETHERTYPE: return 2;
196 case ODP_KEY_ATTR_IPV4: return sizeof(struct odp_key_ipv4);
197 case ODP_KEY_ATTR_TCP: return sizeof(struct odp_key_tcp);
198 case ODP_KEY_ATTR_UDP: return sizeof(struct odp_key_udp);
199 case ODP_KEY_ATTR_ICMP: return sizeof(struct odp_key_icmp);
200 case ODP_KEY_ATTR_ARP: return sizeof(struct odp_key_arp);
202 case ODP_KEY_ATTR_UNSPEC:
203 case __ODP_KEY_ATTR_MAX:
212 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
214 size_t len = nl_attr_get_size(a);
216 ds_put_format(ds, "key%"PRId16, nl_attr_type(a));
218 const uint8_t *unspec;
221 unspec = nl_attr_get(a);
222 for (i = 0; i < len; i++) {
223 ds_put_char(ds, i ? ' ': '(');
224 ds_put_format(ds, "%02x", unspec[i]);
226 ds_put_char(ds, ')');
231 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
233 const struct odp_key_ethernet *eth_key;
234 const struct odp_key_8021q *q_key;
235 const struct odp_key_ipv4 *ipv4_key;
236 const struct odp_key_tcp *tcp_key;
237 const struct odp_key_udp *udp_key;
238 const struct odp_key_icmp *icmp_key;
239 const struct odp_key_arp *arp_key;
241 if (nl_attr_get_size(a) != odp_flow_key_attr_len(nl_attr_type(a))) {
242 ds_put_format(ds, "bad length %zu, expected %d for: ",
244 odp_flow_key_attr_len(nl_attr_type(a)));
245 format_generic_odp_key(a, ds);
249 switch (nl_attr_type(a)) {
250 case ODP_KEY_ATTR_TUN_ID:
251 ds_put_format(ds, "tun_id(%#"PRIx64")", nl_attr_get_be64(a));
254 case ODP_KEY_ATTR_IN_PORT:
255 ds_put_format(ds, "in_port(%"PRIu32")", nl_attr_get_u32(a));
258 case ODP_KEY_ATTR_ETHERNET:
259 eth_key = nl_attr_get(a);
260 ds_put_format(ds, "eth(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
261 ETH_ADDR_ARGS(eth_key->eth_src),
262 ETH_ADDR_ARGS(eth_key->eth_dst));
265 case ODP_KEY_ATTR_8021Q:
266 q_key = nl_attr_get(a);
267 ds_put_cstr(ds, "vlan(");
268 if (q_key->q_tpid != htons(ETH_TYPE_VLAN)) {
269 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(q_key->q_tpid));
271 ds_put_format(ds, "vid%"PRIu16",pcp%d)",
272 vlan_tci_to_vid(q_key->q_tci),
273 vlan_tci_to_pcp(q_key->q_tci));
276 case ODP_KEY_ATTR_ETHERTYPE:
277 ds_put_format(ds, "eth_type(0x%04"PRIx16")",
278 ntohs(nl_attr_get_be16(a)));
281 case ODP_KEY_ATTR_IPV4:
282 ipv4_key = nl_attr_get(a);
283 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT","
284 "proto=%"PRId8",tos=%"PRIu8")",
285 IP_ARGS(&ipv4_key->ipv4_src),
286 IP_ARGS(&ipv4_key->ipv4_dst),
287 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos);
290 case ODP_KEY_ATTR_TCP:
291 tcp_key = nl_attr_get(a);
292 ds_put_format(ds, "tcp(src=%"PRIu16",dst=%"PRIu16")",
293 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
296 case ODP_KEY_ATTR_UDP:
297 udp_key = nl_attr_get(a);
298 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16")",
299 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
302 case ODP_KEY_ATTR_ICMP:
303 icmp_key = nl_attr_get(a);
304 ds_put_format(ds, "icmp(type=%"PRIu8",code=%"PRIu8")",
305 icmp_key->icmp_type, icmp_key->icmp_code);
308 case ODP_KEY_ATTR_ARP:
309 arp_key = nl_attr_get(a);
310 ds_put_format(ds, "arp(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16")",
311 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
312 ntohs(arp_key->arp_op));
316 format_generic_odp_key(a, ds);
321 /* Appends to 'ds' a string representation of the 'key_len' bytes of
322 * ODP_KEY_ATTR_* attributes in 'key'. */
324 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
327 const struct nlattr *a;
330 NL_ATTR_FOR_EACH (a, left, key, key_len) {
332 ds_put_char(ds, ',');
334 format_odp_key_attr(a, ds);
337 if (left == key_len) {
338 ds_put_cstr(ds, "<empty>");
340 ds_put_format(ds, ",***%u leftover bytes***", left);
343 ds_put_cstr(ds, "<empty>");
347 /* Appends a representation of 'flow' as ODP_KEY_ATTR_* attributes to 'buf'. */
349 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
351 struct odp_key_ethernet *eth_key;
353 if (flow->tun_id != htonll(0)) {
354 nl_msg_put_be64(buf, ODP_KEY_ATTR_TUN_ID, flow->tun_id);
357 nl_msg_put_u32(buf, ODP_KEY_ATTR_IN_PORT, flow->in_port);
359 eth_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ETHERNET,
361 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
362 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
364 if (flow->vlan_tci != htons(0)) {
365 struct odp_key_8021q *q_key;
367 q_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_8021Q,
369 q_key->q_tpid = htons(ETH_TYPE_VLAN);
370 q_key->q_tci = flow->vlan_tci & ~htons(VLAN_CFI);
373 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
377 nl_msg_put_be16(buf, ODP_KEY_ATTR_ETHERTYPE, flow->dl_type);
379 if (flow->dl_type == htons(ETH_TYPE_IP)) {
380 struct odp_key_ipv4 *ipv4_key;
382 ipv4_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_IPV4,
384 ipv4_key->ipv4_src = flow->nw_src;
385 ipv4_key->ipv4_dst = flow->nw_dst;
386 ipv4_key->ipv4_proto = flow->nw_proto;
387 ipv4_key->ipv4_tos = flow->nw_tos;
389 if (flow->nw_proto == IP_TYPE_TCP) {
390 struct odp_key_tcp *tcp_key;
392 tcp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_TCP,
394 tcp_key->tcp_src = flow->tp_src;
395 tcp_key->tcp_dst = flow->tp_dst;
396 } else if (flow->nw_proto == IP_TYPE_UDP) {
397 struct odp_key_udp *udp_key;
399 udp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_UDP,
401 udp_key->udp_src = flow->tp_src;
402 udp_key->udp_dst = flow->tp_dst;
403 } else if (flow->nw_proto == IP_TYPE_ICMP) {
404 struct odp_key_icmp *icmp_key;
406 icmp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ICMP,
408 icmp_key->icmp_type = ntohs(flow->tp_src);
409 icmp_key->icmp_code = ntohs(flow->tp_dst);
411 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
412 struct odp_key_arp *arp_key;
414 arp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ARP,
416 arp_key->arp_sip = flow->nw_src;
417 arp_key->arp_tip = flow->nw_dst;
418 arp_key->arp_op = htons(flow->nw_proto);
422 /* Converts the 'key_len' bytes of ODP_KEY_ATTR_* attributes in 'key' to a flow
423 * structure in 'flow'. Returns 0 if successful, otherwise EINVAL. */
425 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
428 const struct nlattr *nla;
429 enum odp_key_type prev_type;
432 memset(flow, 0, sizeof *flow);
433 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
435 prev_type = ODP_KEY_ATTR_UNSPEC;
436 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
437 const struct odp_key_ethernet *eth_key;
438 const struct odp_key_8021q *q_key;
439 const struct odp_key_ipv4 *ipv4_key;
440 const struct odp_key_tcp *tcp_key;
441 const struct odp_key_udp *udp_key;
442 const struct odp_key_icmp *icmp_key;
443 const struct odp_key_arp *arp_key;
445 uint16_t type = nl_attr_type(nla);
446 int len = odp_flow_key_attr_len(type);
448 if (nl_attr_get_size(nla) != len && len != -1) {
452 #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
453 switch (TRANSITION(prev_type, type)) {
454 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_TUN_ID):
455 flow->tun_id = nl_attr_get_be64(nla);
458 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_IN_PORT):
459 case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_IN_PORT):
460 if (nl_attr_get_u32(nla) >= UINT16_MAX) {
463 flow->in_port = nl_attr_get_u32(nla);
466 case TRANSITION(ODP_KEY_ATTR_IN_PORT, ODP_KEY_ATTR_ETHERNET):
467 eth_key = nl_attr_get(nla);
468 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
469 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
472 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_8021Q):
473 q_key = nl_attr_get(nla);
474 if (q_key->q_tpid != htons(ETH_TYPE_VLAN)) {
475 /* Only standard 0x8100 VLANs currently supported. */
478 if (q_key->q_tci & htons(VLAN_CFI)) {
481 flow->vlan_tci = q_key->q_tci | htons(VLAN_CFI);
484 case TRANSITION(ODP_KEY_ATTR_8021Q, ODP_KEY_ATTR_ETHERTYPE):
485 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_ETHERTYPE):
486 flow->dl_type = nl_attr_get_be16(nla);
487 if (ntohs(flow->dl_type) < 1536) {
492 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV4):
493 if (flow->dl_type != htons(ETH_TYPE_IP)) {
496 ipv4_key = nl_attr_get(nla);
497 flow->nw_src = ipv4_key->ipv4_src;
498 flow->nw_dst = ipv4_key->ipv4_dst;
499 flow->nw_proto = ipv4_key->ipv4_proto;
500 flow->nw_tos = ipv4_key->ipv4_tos;
501 if (flow->nw_tos & IP_ECN_MASK) {
506 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP):
507 if (flow->nw_proto != IP_TYPE_TCP) {
510 tcp_key = nl_attr_get(nla);
511 flow->tp_src = tcp_key->tcp_src;
512 flow->tp_dst = tcp_key->tcp_dst;
515 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP):
516 if (flow->nw_proto != IP_TYPE_UDP) {
519 udp_key = nl_attr_get(nla);
520 flow->tp_src = udp_key->udp_src;
521 flow->tp_dst = udp_key->udp_dst;
524 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP):
525 if (flow->nw_proto != IP_TYPE_ICMP) {
528 icmp_key = nl_attr_get(nla);
529 flow->tp_src = htons(icmp_key->icmp_type);
530 flow->tp_dst = htons(icmp_key->icmp_code);
533 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_ARP):
534 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
537 arp_key = nl_attr_get(nla);
538 flow->nw_src = arp_key->arp_sip;
539 flow->nw_dst = arp_key->arp_tip;
540 if (arp_key->arp_op & htons(0xff00)) {
543 flow->nw_proto = ntohs(arp_key->arp_op);
547 if (type == ODP_KEY_ATTR_UNSPEC
548 || prev_type == ODP_KEY_ATTR_UNSPEC) {
561 case ODP_KEY_ATTR_UNSPEC:
564 case ODP_KEY_ATTR_TUN_ID:
565 case ODP_KEY_ATTR_IN_PORT:
568 case ODP_KEY_ATTR_ETHERNET:
569 case ODP_KEY_ATTR_8021Q:
572 case ODP_KEY_ATTR_ETHERTYPE:
573 if (flow->dl_type == htons(ETH_TYPE_IP)
574 || flow->dl_type == htons(ETH_TYPE_ARP)) {
579 case ODP_KEY_ATTR_IPV4:
580 if (flow->nw_proto == IP_TYPE_TCP
581 || flow->nw_proto == IP_TYPE_UDP
582 || flow->nw_proto == IP_TYPE_ICMP) {
587 case ODP_KEY_ATTR_TCP:
588 case ODP_KEY_ATTR_UDP:
589 case ODP_KEY_ATTR_ICMP:
590 case ODP_KEY_ATTR_ARP:
593 case __ODP_KEY_ATTR_MAX: