ofp-actions: helper functions for of12 set-field action
[sliver-openvswitch.git] / lib / nx-match.h
1 /*
2  * Copyright (c) 2010, 2011, 2012 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 "flow.h"
24 #include "ofp-errors.h"
25 #include "openvswitch/types.h"
26
27 struct ds;
28 struct match;
29 struct mf_subfield;
30 struct ofpact_reg_move;
31 struct ofpact_reg_load;
32 struct ofpbuf;
33 struct nx_action_reg_load;
34 struct nx_action_reg_move;
35
36 /* Nicira Extended Match (NXM) flexible flow match helper functions.
37  *
38  * See include/openflow/nicira-ext.h for NXM specification.
39  */
40
41 enum ofperr nx_pull_match(struct ofpbuf *, unsigned int match_len,
42                           struct match *,
43                           ovs_be64 *cookie, ovs_be64 *cookie_mask);
44 enum ofperr nx_pull_match_loose(struct ofpbuf *, unsigned int match_len,
45                                 struct match *, ovs_be64 *cookie,
46                                 ovs_be64 *cookie_mask);
47 enum ofperr oxm_pull_match(struct ofpbuf *, struct match *);
48 enum ofperr oxm_pull_match_loose(struct ofpbuf *, struct match *);
49 int nx_put_match(struct ofpbuf *, const struct match *,
50                  ovs_be64 cookie, ovs_be64 cookie_mask);
51 int oxm_put_match(struct ofpbuf *, const struct match *);
52
53 char *nx_match_to_string(const uint8_t *, unsigned int match_len);
54 char *oxm_match_to_string(const uint8_t *, unsigned int match_len);
55 int nx_match_from_string(const char *, struct ofpbuf *);
56 int oxm_match_from_string(const char *, struct ofpbuf *);
57
58 void nxm_parse_reg_move(struct ofpact_reg_move *, const char *);
59 void nxm_parse_reg_load(struct ofpact_reg_load *, const char *);
60
61 void nxm_format_reg_move(const struct ofpact_reg_move *, struct ds *);
62 void nxm_format_reg_load(const struct ofpact_reg_load *, struct ds *);
63
64 enum ofperr nxm_reg_move_from_openflow(const struct nx_action_reg_move *,
65                                        struct ofpbuf *ofpacts);
66 enum ofperr nxm_reg_load_from_openflow(const struct nx_action_reg_load *,
67                                        struct ofpbuf *ofpacts);
68 enum ofperr nxm_reg_load_from_openflow12_set_field(
69     const struct ofp12_action_set_field * oasf, struct ofpbuf *ofpacts);
70
71 enum ofperr nxm_reg_move_check(const struct ofpact_reg_move *,
72                                const struct flow *);
73 enum ofperr nxm_reg_load_check(const struct ofpact_reg_load *,
74                                const struct flow *);
75
76 void nxm_reg_move_to_nxast(const struct ofpact_reg_move *,
77                            struct ofpbuf *openflow);
78 void nxm_reg_load_to_nxast(const struct ofpact_reg_load *,
79                            struct ofpbuf *openflow);
80
81 void nxm_execute_reg_move(const struct ofpact_reg_move *, struct flow *);
82 void nxm_execute_reg_load(const struct ofpact_reg_load *, struct flow *);
83 void nxm_reg_load(const struct mf_subfield *, uint64_t src_data,
84                   struct flow *);
85
86 int nxm_field_bytes(uint32_t header);
87 int nxm_field_bits(uint32_t header);
88
89 /* Dealing with the 'ofs_nbits' members in several Nicira extensions. */
90
91 static inline ovs_be16
92 nxm_encode_ofs_nbits(int ofs, int n_bits)
93 {
94     return htons((ofs << 6) | (n_bits - 1));
95 }
96
97 static inline int
98 nxm_decode_ofs(ovs_be16 ofs_nbits)
99 {
100     return ntohs(ofs_nbits) >> 6;
101 }
102
103 static inline int
104 nxm_decode_n_bits(ovs_be16 ofs_nbits)
105 {
106     return (ntohs(ofs_nbits) & 0x3f) + 1;
107 }
108 \f
109 /* This is my guess at the length of a "typical" nx_match, for use in
110  * predicting space requirements. */
111 #define NXM_TYPICAL_LEN 64
112
113 #endif /* nx-match.h */