Better abstract OpenFlow error codes.
[sliver-openvswitch.git] / lib / nx-match.h
1 /*
2  * Copyright (c) 2010, 2011, 2012 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 #ifndef NX_MATCH_H
18 #define NX_MATCH_H 1
19
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include "openvswitch/types.h"
24 #include "ofp-errors.h"
25
26 struct cls_rule;
27 struct ds;
28 struct flow;
29 struct ofpbuf;
30 struct nx_action_reg_load;
31 struct nx_action_reg_move;
32
33 /* Nicira Extended Match (NXM) flexible flow match helper functions.
34  *
35  * See include/openflow/nicira-ext.h for NXM specification.
36  */
37
38 enum ofperr nx_pull_match(struct ofpbuf *, unsigned int match_len,
39                           uint16_t priority, struct cls_rule *,
40                           ovs_be64 *cookie, ovs_be64 *cookie_mask);
41 enum ofperr nx_pull_match_loose(struct ofpbuf *, unsigned int match_len,
42                                 uint16_t priority, struct cls_rule *,
43                                 ovs_be64 *cookie, ovs_be64 *cookie_mask);
44 int nx_put_match(struct ofpbuf *, const struct cls_rule *,
45                  ovs_be64 cookie, ovs_be64 cookie_mask);
46
47 char *nx_match_to_string(const uint8_t *, unsigned int match_len);
48 int nx_match_from_string(const char *, struct ofpbuf *);
49
50 uint64_t nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
51                              const struct flow *);
52
53 void nxm_parse_reg_move(struct nx_action_reg_move *, const char *);
54 void nxm_parse_reg_load(struct nx_action_reg_load *, const char *);
55
56 void nxm_format_reg_move(const struct nx_action_reg_move *, struct ds *);
57 void nxm_format_reg_load(const struct nx_action_reg_load *, struct ds *);
58
59 int nxm_check_reg_move(const struct nx_action_reg_move *, const struct flow *);
60 enum ofperr nxm_check_reg_load(const struct nx_action_reg_load *,
61                                const struct flow *);
62 enum ofperr nxm_src_check(ovs_be32 src, unsigned int ofs, unsigned int n_bits,
63                           const struct flow *);
64 enum ofperr nxm_dst_check(ovs_be32 dst, unsigned int ofs, unsigned int n_bits,
65                           const struct flow *);
66
67 void nxm_execute_reg_move(const struct nx_action_reg_move *, struct flow *);
68 void nxm_execute_reg_load(const struct nx_action_reg_load *, struct flow *);
69 void nxm_reg_load(ovs_be32 dst, ovs_be16 ofs_nbits, uint64_t src_data,
70                   struct flow *);
71
72 int nxm_field_bytes(uint32_t header);
73 int nxm_field_bits(uint32_t header);
74
75 const char *nxm_parse_field_bits(const char *s,
76                                  uint32_t *headerp, int *ofsp, int *n_bitsp);
77 void nxm_format_field_bits(struct ds *, uint32_t header, int ofs, int n_bits);
78
79 /* Dealing with the 'ofs_nbits' members of struct nx_action_reg_load and struct
80  * nx_action_multipath. */
81
82 static inline ovs_be16
83 nxm_encode_ofs_nbits(int ofs, int n_bits)
84 {
85     return htons((ofs << 6) | (n_bits - 1));
86 }
87
88 static inline int
89 nxm_decode_ofs(ovs_be16 ofs_nbits)
90 {
91     return ntohs(ofs_nbits) >> 6;
92 }
93
94 static inline int
95 nxm_decode_n_bits(ovs_be16 ofs_nbits)
96 {
97     return (ntohs(ofs_nbits) & 0x3f) + 1;
98 }
99 \f
100 /* Upper bound on the length of an nx_match.  The longest nx_match (an
101  * IPV6 neighbor discovery message using 5 registers) would be:
102  *
103  *                   header  value  mask  total
104  *                   ------  -----  ----  -----
105  *  NXM_OF_IN_PORT      4       2    --      6
106  *  NXM_OF_ETH_DST_W    4       6     6     16
107  *  NXM_OF_ETH_SRC      4       6    --     10
108  *  NXM_OF_ETH_TYPE     4       2    --      6
109  *  NXM_OF_VLAN_TCI     4       2     2      8
110  *  NXM_OF_IP_TOS       4       1    --      5
111  *  NXM_NX_IP_ECN       4       1    --      5
112  *  NXM_OF_IP_TTL       4       1    --      5
113  *  NXM_NX_IP_FRAG      4       1     1      8
114  *  NXM_OF_IP_PROTO     4       2    --      6
115  *  NXM_OF_IPV6_SRC_W   4      16    16     36
116  *  NXM_OF_IPV6_DST_W   4      16    16     36
117  *  NXM_OF_IPV6_LABEL   4       4    --      8
118  *  NXM_OF_ICMP_TYPE    4       1    --      5
119  *  NXM_OF_ICMP_CODE    4       1    --      5
120  *  NXM_NX_ND_TARGET    4      16    --     20
121  *  NXM_NX_ND_SLL       4       6    --     10
122  *  NXM_NX_REG_W(0)     4       4     4     12
123  *  NXM_NX_REG_W(1)     4       4     4     12
124  *  NXM_NX_REG_W(2)     4       4     4     12
125  *  NXM_NX_REG_W(3)     4       4     4     12
126  *  NXM_NX_REG_W(4)     4       4     4     12
127  *  NXM_NX_TUN_ID_W     4       8     8     20
128  *  -------------------------------------------
129  *  total                                  275
130  *
131  * So this value is conservative.
132  */
133 #define NXM_MAX_LEN 384
134
135 /* This is my guess at the length of a "typical" nx_match, for use in
136  * predicting space requirements. */
137 #define NXM_TYPICAL_LEN 64
138
139 #endif /* nx-match.h */