nx-match: Make nxm_field_bytes(), nxm_field_bits() public.
[sliver-openvswitch.git] / lib / nx-match.h
1 /*
2  * Copyright (c) 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 #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
25 struct cls_rule;
26 struct ds;
27 struct flow;
28 struct ofpbuf;
29 struct nx_action_reg_load;
30 struct nx_action_reg_move;
31
32 /* Nicira Extended Match (NXM) flexible flow match helper functions.
33  *
34  * See include/openflow/nicira-ext.h for NXM specification.
35  */
36
37 int nx_pull_match(struct ofpbuf *, unsigned int match_len, uint16_t priority,
38                   struct cls_rule *);
39 int nx_put_match(struct ofpbuf *, const struct cls_rule *);
40
41 char *nx_match_to_string(const uint8_t *, unsigned int match_len);
42 int nx_match_from_string(const char *, struct ofpbuf *);
43
44 void nxm_parse_reg_move(struct nx_action_reg_move *, const char *);
45 void nxm_parse_reg_load(struct nx_action_reg_load *, const char *);
46
47 void nxm_format_reg_move(const struct nx_action_reg_move *, struct ds *);
48 void nxm_format_reg_load(const struct nx_action_reg_load *, struct ds *);
49
50 int nxm_check_reg_move(const struct nx_action_reg_move *, const struct flow *);
51 int nxm_check_reg_load(const struct nx_action_reg_load *, const struct flow *);
52
53 void nxm_execute_reg_move(const struct nx_action_reg_move *, struct flow *);
54 void nxm_execute_reg_load(const struct nx_action_reg_load *, struct flow *);
55
56 int nxm_field_bytes(uint32_t header);
57 int nxm_field_bits(uint32_t header);
58
59 /* Dealing with the 'ofs_nbits' members of struct nx_action_reg_load and struct
60  * nx_action_multipath. */
61
62 static inline ovs_be16
63 nxm_encode_ofs_nbits(int ofs, int n_bits)
64 {
65     return htons((ofs << 6) | (n_bits - 1));
66 }
67
68 static inline int
69 nxm_decode_ofs(ovs_be16 ofs_nbits)
70 {
71     return ntohs(ofs_nbits) >> 6;
72 }
73
74 static inline int
75 nxm_decode_n_bits(ovs_be16 ofs_nbits)
76 {
77     return (ntohs(ofs_nbits) & 0x3f) + 1;
78 }
79 \f
80 /* Upper bound on the length of an nx_match.  The longest nx_match (assuming
81  * we implement 4 registers) would be:
82  *
83  *                   header  value  mask  total
84  *                   ------  -----  ----  -----
85  *  NXM_OF_IN_PORT      4       2    --      6
86  *  NXM_OF_ETH_DST_W    4       6     6     16
87  *  NXM_OF_ETH_SRC      4       6    --     10
88  *  NXM_OF_ETH_TYPE     4       2    --      6
89  *  NXM_OF_VLAN_TCI     4       2     2      8
90  *  NXM_OF_IP_TOS       4       1    --      5
91  *  NXM_OF_IP_PROTO     4       2    --      6
92  *  NXM_OF_IP_SRC_W     4       4     4     12
93  *  NXM_OF_IP_DST_W     4       4     4     12
94  *  NXM_OF_TCP_SRC      4       2    --      6
95  *  NXM_OF_TCP_DST      4       2    --      6
96  *  NXM_NX_REG_W(0)     4       4     4     12
97  *  NXM_NX_REG_W(1)     4       4     4     12
98  *  NXM_NX_REG_W(2)     4       4     4     12
99  *  NXM_NX_REG_W(3)     4       4     4     12
100  *  NXM_NX_TUN_ID_W     4       8     8     20
101  *  -------------------------------------------
102  *  total                                  161
103  *
104  * So this value is conservative.
105  */
106 #define NXM_MAX_LEN 192
107
108 /* This is my guess at the length of a "typical" nx_match, for use in
109  * predicting space requirements. */
110 #define NXM_TYPICAL_LEN 64
111
112 #endif /* nx-match.h */