blob: 67513b97b677b18c6ceb703b260bcefc23fcebbe [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.protocol.match;
2
3import org.projectfloodlight.openflow.protocol.OFObject;
4import org.projectfloodlight.openflow.types.Masked;
5import org.projectfloodlight.openflow.types.OFValueType;
6
7/**
8 * Generic interface for version-agnostic immutable Match structure.
9 * The Match structure is defined in the OpenFlow protocol, and it contains information on
10 * the fields to be matched in a specific flow record.
11 * This interface does not assume anything on the fields in the Match structure. If in
12 * some version, the match structure cannot handle a certain field, it may return <code>false</code>
13 * for <code>supports(...)</code> calls, and throw <code>UnsupportedOperationException</code> from all
14 * other methods in such cases.
15 * <br><br>
16 * On wildcards and masks:<br>
17 * This interface defines the following masking notations for fields:
18 * <ul>
19 * <li><b>Exact</b>: field is matched exactly against a single, fixed value (no mask, or mask is all ones).
20 * <li><b>Wildcarded</b>: field is not being matched. It is fully masked (mask=0) and any value of it
21 * will match the flow record having this match.
22 * <li><b>Partially masked</b>: field is matched using a specified mask which is neither 0 nor all ones. Mask can
23 * be either arbitrary or require some specific structure.
24 * </ul>
25 * Implementing classes may or may not support all types of these masking types. They may also support
26 * them in part. For example, OF1.0 supports exact match and (full) wildcarding for all fields, but it
27 * does only supports partial masking for IP source/destination fields, and this partial masking must be
28 * in the CIDR prefix format. Thus, OF1.0 implementation may throw <code>UnsupportedOperationException</code> if given
Rob Vaterlaus934b4ad2013-11-12 10:08:59 -080029 * in <code>setMasked</code> an IP mask of, for example, 255.0.255.0, or if <code>setMasked</code> is called for any field
Yotam Harcholf3f11152013-09-05 16:47:16 -070030 * which is not IP source/destination address.
31 * <br><br>
32 * On prerequisites:<br>
33 * From the OF1.1 spec, page 28, the OF1.0 spec failed to explicitly specify this, but it
34 * is the behavior of OF1.0 switches:
35 * "Protocol-specific fields within ofp_match will be ignored within a single table when
36 * the corresponding protocol is not specified in the match. The MPLS match fields will
37 * be ignored unless the Ethertype is specified as MPLS. Likewise, the IP header and
38 * transport header fields will be ignored unless the Ethertype is specified as either
39 * IPv4 or ARP. The tp_src and tp_dst fields will be ignored unless the network protocol
Rob Vaterlaus934b4ad2013-11-12 10:08:59 -080040 * specified is as TCP, UDP or SCTP. Fields that are ignored don't need to be wildcarded
Yotam Harcholf3f11152013-09-05 16:47:16 -070041 * and should be set to 0."
42 * <br><br>
43 * This interface uses generics to assure type safety in users code. However, implementing classes may have to suppress
44 * 'unchecked cast' warnings while making sure they correctly cast base on their implementation details.
45 *
46 * @author Yotam Harchol (yotam.harchol@bigswitch.com)
47 */
48public interface Match extends OFObject {
49
50 /**
51 * Returns a value for the given field if:
52 * <ul>
53 * <li>Field is supported
54 * <li>Field is not fully wildcarded
55 * <li>Prerequisites are ok
56 * </ul>
57 * If one of the above conditions does not hold, returns null. Value is returned masked if partially wildcarded.
58 *
59 * @param field Match field to retrieve
60 * @return Value of match field (may be masked), or <code>null</code> if field is one of the conditions above does not hold.
61 * @throws UnsupportedOperationException If field is not supported.
62 */
63 public <F extends OFValueType<F>> F get(MatchField<F> field) throws UnsupportedOperationException;
64
65 /**
66 * Returns the masked value for the given field from this match, along with the mask itself.
67 * Prerequisite: field is partially masked.
68 * If prerequisite is not met, a <code>null</code> is returned.
69 *
70 * @param field Match field to retrieve.
71 * @return Masked value of match field or null if no mask is set.
72 * @throws UnsupportedOperationException If field is not supported.
73 */
74 public <F extends OFValueType<F>> Masked<F> getMasked(MatchField<F> field) throws UnsupportedOperationException;
75
76 /**
77 * Returns true if and only if this match object supports the given match field.
78 *
79 * @param field Match field
80 * @return true if field is supported, false otherwise.
81 */
82 public boolean supports(MatchField<?> field);
83
84 /**
85 * Returns true if and only if this match object supports partially bitmasking of the given field.
86 * (note: not all possible values of this bitmask have to be acceptable)
87 *
88 * @param field Match field.
89 * @return true if field can be partially masked, false otherwise.
90 * @throws UnsupportedOperationException If field is not supported.
91 */
92 public boolean supportsMasked(MatchField<?> field) throws UnsupportedOperationException;
93
94 /**
95 * Returns true if and only if this field is currently specified in the match with an exact value and
96 * no mask. I.e., the specified match will only select packets that match the exact value of getValue(field).
97 *
98 * @param field Match field.
99 * @return true if field has a specific exact value, false if not.
100 * @throws UnsupportedOperationException If field is not supported.
101 */
102 public boolean isExact(MatchField<?> field) throws UnsupportedOperationException;
103
104 /**
105 * True if and only if this field is currently logically unspecified in the match, i.e, the
106 * value returned by getValue(f) has no impact on whether a packet will be selected
107 * by the match or not.
108 *
109 * @param field Match field.
110 * @return true if field is fully wildcarded, false if not.
111 * @throws UnsupportedOperationException If field is not supported.
112 */
113 public boolean isFullyWildcarded(MatchField<?> field) throws UnsupportedOperationException;
114
115 /**
116 * True if and only if this field is currently partially specified in the match, i.e, the
kjwon157bc85402015-02-12 15:07:42 +0900117 * match will only select packets that match (p.value &amp; getMask(field)) == getValue(field),
Yotam Harcholf3f11152013-09-05 16:47:16 -0700118 * and getMask(field) != 0.
119 *
120 * @param field Match field.
121 * @return true if field is partially masked, false if not.
122 * @throws UnsupportedOperationException If field is not supported.
123 */
124 public boolean isPartiallyMasked(MatchField<?> field) throws UnsupportedOperationException;
125
126 /**
Rob Vaterlaus934b4ad2013-11-12 10:08:59 -0800127 * Get an Iterable over the match fields that have been specified for the
128 * match. This includes the match fields that are exact or masked match
129 * (but not fully wildcarded).
130 *
131 * @return
132 */
133 public Iterable<MatchField<?>> getMatchFields();
134
135 /**
Yotam Harcholf3f11152013-09-05 16:47:16 -0700136 * Returns a builder to build new instances of this type of match object.
137 * @return Match builder
138 */
139 public Builder createBuilder();
140
141 /**
142 * Builder interface for Match objects.
143 * Builder is used to create new Match objects and it creates the match according to the version it
144 * corresponds to. The builder uses the same notation of wildcards and masks, and can also throw
145 * <code>UnsupportedOperationException</code> if it is asked to create some matching that is not supported in
146 * the version it represents.
147 *
148 * While used, MatchBuilder may not be consistent in terms of field prerequisites. However, user must
149 * solve these before using the generated Match object as these prerequisites should be enforced in the
150 * getters.
151 *
152 * @author Yotam Harchol (yotam.harchol@bigswitch.com)
153 */
154 interface Builder {
155 public <F extends OFValueType<F>> F get(MatchField<F> field) throws UnsupportedOperationException;
156
157 public <F extends OFValueType<F>> Masked<F> getMasked(MatchField<F> field) throws UnsupportedOperationException;
158
159 public boolean supports(MatchField<?> field);
160
161 public boolean supportsMasked(MatchField<?> field) throws UnsupportedOperationException;
162
163 public boolean isExact(MatchField<?> field) throws UnsupportedOperationException;
164
165 public boolean isFullyWildcarded(MatchField<?> field) throws UnsupportedOperationException;
166
167 public boolean isPartiallyMasked(MatchField<?> field) throws UnsupportedOperationException;
168
169 /**
170 * Sets a specific exact value for a field.
171 *
172 * @param field Match field to set.
173 * @param value Value of match field.
174 * @return the Builder instance used.
175 * @throws UnsupportedOperationException If field is not supported.
176 */
177 public <F extends OFValueType<F>> Builder setExact(MatchField<F> field, F value) throws UnsupportedOperationException;
178
179 /**
180 * Sets a masked value for a field.
181 *
182 * @param field Match field to set.
183 * @param value Value of field.
184 * @param mask Mask value.
185 * @return the Builder instance used.
186 * @throws UnsupportedOperationException If field is not supported, if field is supported but does not support masking, or if mask structure is not supported.
187 */
188 public <F extends OFValueType<F>> Builder setMasked(MatchField<F> field, F value, F mask) throws UnsupportedOperationException;
189
190 /**
191 * Sets a masked value for a field.
192 *
193 * @param field Match field to set.
194 * @param valueWithMask Compound Masked object contains the value and the mask.
195 * @return the Builder instance used.
196 * @throws UnsupportedOperationException If field is not supported, if field is supported but does not support masking, or if mask structure is not supported.
197 */
198 public <F extends OFValueType<F>> Builder setMasked(MatchField<F> field, Masked<F> valueWithMask) throws UnsupportedOperationException;
199
200 /**
201 * Unsets any value given for the field and wildcards it so that it matches any value.
202 *
203 * @param field Match field to unset.
204 * @return the Builder instance used.
205 * @throws UnsupportedOperationException If field is not supported.
206 */
207 public <F extends OFValueType<F>> Builder wildcard(MatchField<F> field) throws UnsupportedOperationException;
208
209 /**
210 * Returns the match created by this builder.
211 *
212 * @return a Match object.
213 */
214 public Match build();
215 }
216}