Merge branch 'mainstream'
[sliver-openvswitch.git] / lib / nx-match.h
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
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 "compiler.h"
24 #include "flow.h"
25 #include "ofp-errors.h"
26 #include "openvswitch/types.h"
27
28 struct ds;
29 struct match;
30 struct mf_subfield;
31 struct ofpact_reg_move;
32 struct ofpact_reg_load;
33 struct ofpact_stack;
34 struct ofpbuf;
35 struct nx_action_reg_load;
36 struct nx_action_reg_move;
37
38
39 /* Nicira Extended Match (NXM) flexible flow match helper functions.
40  *
41  * See include/openflow/nicira-ext.h for NXM specification.
42  */
43
44 enum ofperr nx_pull_match(struct ofpbuf *, unsigned int match_len,
45                           struct match *,
46                           ovs_be64 *cookie, ovs_be64 *cookie_mask);
47 enum ofperr nx_pull_match_loose(struct ofpbuf *, unsigned int match_len,
48                                 struct match *, ovs_be64 *cookie,
49                                 ovs_be64 *cookie_mask);
50 enum ofperr oxm_pull_match(struct ofpbuf *, struct match *);
51 enum ofperr oxm_pull_match_loose(struct ofpbuf *, struct match *);
52 int nx_put_match(struct ofpbuf *, const struct match *,
53                  ovs_be64 cookie, ovs_be64 cookie_mask);
54 int oxm_put_match(struct ofpbuf *, const struct match *);
55
56 char *nx_match_to_string(const uint8_t *, unsigned int match_len);
57 char *oxm_match_to_string(const struct ofpbuf *, unsigned int match_len);
58 int nx_match_from_string(const char *, struct ofpbuf *);
59 int oxm_match_from_string(const char *, struct ofpbuf *);
60
61 char *nxm_parse_reg_move(struct ofpact_reg_move *, const char *)
62     WARN_UNUSED_RESULT;
63 char *nxm_parse_reg_load(struct ofpact_reg_load *, const char *)
64     WARN_UNUSED_RESULT;
65
66 void nxm_format_reg_move(const struct ofpact_reg_move *, struct ds *);
67 void nxm_format_reg_load(const struct ofpact_reg_load *, struct ds *);
68
69 enum ofperr nxm_reg_move_from_openflow(const struct nx_action_reg_move *,
70                                        struct ofpbuf *ofpacts);
71 enum ofperr nxm_reg_load_from_openflow(const struct nx_action_reg_load *,
72                                        struct ofpbuf *ofpacts);
73 enum ofperr nxm_reg_load_from_openflow12_set_field(
74     const struct ofp12_action_set_field * oasf, struct ofpbuf *ofpacts);
75
76 enum ofperr nxm_reg_move_check(const struct ofpact_reg_move *,
77                                const struct flow *);
78 enum ofperr nxm_reg_load_check(const struct ofpact_reg_load *,
79                                const struct flow *);
80
81 void nxm_reg_move_to_nxast(const struct ofpact_reg_move *,
82                            struct ofpbuf *openflow);
83 void nxm_reg_load_to_nxast(const struct ofpact_reg_load *,
84                            struct ofpbuf *openflow);
85
86 void nxm_execute_reg_move(const struct ofpact_reg_move *, struct flow *,
87                           struct flow_wildcards *);
88 void nxm_execute_reg_load(const struct ofpact_reg_load *, struct flow *,
89                           struct flow_wildcards *);
90 void nxm_reg_load(const struct mf_subfield *, uint64_t src_data,
91                   struct flow *, struct flow_wildcards *);
92
93 char *nxm_parse_stack_action(struct ofpact_stack *, const char *)
94     WARN_UNUSED_RESULT;
95
96 void nxm_format_stack_push(const struct ofpact_stack *, struct ds *);
97 void nxm_format_stack_pop(const struct ofpact_stack *, struct ds *);
98
99 enum ofperr nxm_stack_push_from_openflow(const struct nx_action_stack *,
100                                        struct ofpbuf *ofpacts);
101 enum ofperr nxm_stack_pop_from_openflow(const struct nx_action_stack *,
102                                        struct ofpbuf *ofpacts);
103 enum ofperr nxm_stack_push_check(const struct ofpact_stack *,
104                                  const  struct flow *);
105 enum ofperr nxm_stack_pop_check(const struct ofpact_stack *,
106                                const struct flow *);
107
108 void nxm_stack_push_to_nxast(const struct ofpact_stack *,
109                            struct ofpbuf *openflow);
110 void nxm_stack_pop_to_nxast(const struct ofpact_stack *,
111                            struct ofpbuf *openflow);
112
113 void nxm_execute_stack_push(const struct ofpact_stack *,
114                             const struct flow *, struct flow_wildcards *,
115                             struct ofpbuf *);
116 void nxm_execute_stack_pop(const struct ofpact_stack *,
117                             struct flow *, struct flow_wildcards *,
118                             struct ofpbuf *);
119
120 int nxm_field_bytes(uint32_t header);
121 int nxm_field_bits(uint32_t header);
122
123 /* Dealing with the 'ofs_nbits' members in several Nicira extensions. */
124
125 static inline ovs_be16
126 nxm_encode_ofs_nbits(int ofs, int n_bits)
127 {
128     return htons((ofs << 6) | (n_bits - 1));
129 }
130
131 static inline int
132 nxm_decode_ofs(ovs_be16 ofs_nbits)
133 {
134     return ntohs(ofs_nbits) >> 6;
135 }
136
137 static inline int
138 nxm_decode_n_bits(ovs_be16 ofs_nbits)
139 {
140     return (ntohs(ofs_nbits) & 0x3f) + 1;
141 }
142 \f
143 /* This is my guess at the length of a "typical" nx_match, for use in
144  * predicting space requirements. */
145 #define NXM_TYPICAL_LEN 64
146
147 #endif /* nx-match.h */