blob: f005c5f9ca84145b244c7ba0b0caa3d0239d95de [file] [log] [blame]
Jian Li543fe852021-02-04 17:25:01 +09001/*
2 * Copyright 2021-present Open Networking Foundation
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 */
16package org.onosproject.kubevirtnetworking.util;
17
18import org.onlab.packet.Ip4Address;
19import org.onosproject.net.Device;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
22import org.onosproject.net.device.DeviceService;
23import org.onosproject.net.flow.instructions.ExtensionPropertyException;
24import org.onosproject.net.flow.instructions.ExtensionTreatment;
25import org.slf4j.Logger;
26
27import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_LOAD;
28import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA;
29import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA;
30import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST;
31import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST;
32import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST;
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Provides common methods to help populating flow rules for SONA applications.
37 */
38public final class RulePopulatorUtil {
39
40 private static final Logger log = getLogger(RulePopulatorUtil.class);
41
42 private static final int OFF_SET_BIT = 0;
43 private static final int REMAINDER_BIT = 8;
44
45 private static final String OFF_SET_N_BITS = "ofsNbits";
46 private static final String DESTINATION = "dst";
47 private static final String VALUE = "value";
48 private static final String TUNNEL_DST = "tunnelDst";
49
50 // layer 3 nicira fields
51 private static final int NXM_OF_IP_SRC = 0x00000e04;
52 private static final int NXM_OF_IP_DST = 0x00001004;
53 private static final int NXM_OF_IP_PROT = 0x00000c01;
54
55 public static final int NXM_NX_IP_TTL = 0x00013a01;
56 public static final int NXM_NX_IP_FRAG = 0x00013401;
57 public static final int NXM_OF_ARP_OP = 0x00001e02;
58 public static final int NXM_OF_ARP_SPA = 0x00002004;
59 public static final int NXM_OF_ARP_TPA = 0x00002204;
60 public static final int NXM_NX_ARP_SHA = 0x00012206;
61 public static final int NXM_NX_ARP_THA = 0x00012406;
62
63 // layer 4 nicira fields
64 public static final int NXM_OF_TCP_SRC = 0x00001202;
65 public static final int NXM_OF_TCP_DST = 0x00001402;
66 public static final int NXM_NX_TCP_FLAGS = 0x00014402;
67 public static final int NXM_OF_UDP_SRC = 0x00001602;
68 public static final int NXM_OF_UDP_DST = 0x00001802;
69
70 public static final int NXM_OF_ICMP_TYPE = 0x00001a01;
71 public static final int NXM_OF_ICMP_CODE = 0x00001c01;
72
73 private RulePopulatorUtil() {
74 }
75
76 /**
77 * Returns the nicira load extension treatment.
78 *
79 * @param device device instance
80 * @param field field code
81 * @param value value to load
82 * @return load extension treatment
83 */
84 public static ExtensionTreatment buildLoadExtension(Device device,
85 long field,
86 long value) {
87 if (!checkTreatmentResolver(device)) {
88 return null;
89 }
90
91 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
92 ExtensionTreatment treatment =
93 resolver.getExtensionInstruction(NICIRA_LOAD.type());
94
95 int ofsNbits = OFF_SET_BIT << 6 | (REMAINDER_BIT - 1);
96
97 try {
98 treatment.setPropertyValue(OFF_SET_N_BITS, ofsNbits);
99 treatment.setPropertyValue(DESTINATION, field);
100 treatment.setPropertyValue(VALUE, value);
101 return treatment;
102 } catch (ExtensionPropertyException e) {
103 log.error("Failed to set nicira load extension treatment for {}",
104 device.id());
105 return null;
106 }
107 }
108
109 /**
110 * Returns the nicira move source MAC to destination MAC extension treatment.
111 *
112 * @param device device instance
113 * @return move extension treatment
114 */
115 public static ExtensionTreatment buildMoveEthSrcToDstExtension(Device device) {
116 if (!checkTreatmentResolver(device)) {
117 return null;
118 }
119
120 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
121 return resolver.getExtensionInstruction(NICIRA_MOV_ETH_SRC_TO_DST.type());
122 }
123
124 /**
125 * Returns the nicira move source IP to destination IP extension treatment.
126 *
127 * @param device device instance
128 * @return move extension treatment
129 */
130 public static ExtensionTreatment buildMoveIpSrcToDstExtension(Device device) {
131 if (!checkTreatmentResolver(device)) {
132 return null;
133 }
134
135 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
136 return resolver.getExtensionInstruction(NICIRA_MOV_IP_SRC_TO_DST.type());
137 }
138
139 /**
140 * Returns the nicira move ARP SHA to THA extension treatment.
141 *
142 * @param device device instance
143 * @return move extension treatment
144 */
145 public static ExtensionTreatment buildMoveArpShaToThaExtension(Device device) {
146 if (!checkTreatmentResolver(device)) {
147 return null;
148 }
149
150 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
151 return resolver.getExtensionInstruction(NICIRA_MOV_ARP_SHA_TO_THA.type());
152 }
153
154 /**
155 * Returns the nicira move ARP SPA to TPA extension treatment.
156 *
157 * @param device device instance
158 * @return move extension treatment
159 */
160 public static ExtensionTreatment buildMoveArpSpaToTpaExtension(Device device) {
161 if (!checkTreatmentResolver(device)) {
162 return null;
163 }
164
165 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
166 return resolver.getExtensionInstruction(NICIRA_MOV_ARP_SPA_TO_TPA.type());
167 }
168
169 private static boolean checkTreatmentResolver(Device device) {
170 if (device == null || !device.is(ExtensionTreatmentResolver.class)) {
171 log.warn("Nicira extension treatment is not supported");
172 return false;
173 }
174
175 return true;
176 }
177
178 /**
179 * Returns tunnel destination extension treatment object.
180 *
181 * @param deviceService driver service
182 * @param deviceId device id to apply this treatment
183 * @param remoteIp tunnel destination ip address
184 * @return extension treatment
185 */
186 public static ExtensionTreatment buildExtension(DeviceService deviceService,
187 DeviceId deviceId,
188 Ip4Address remoteIp) {
189 Device device = deviceService.getDevice(deviceId);
190 if (!checkTreatmentResolver(device)) {
191 return null;
192 }
193
194 if (device == null) {
195 return null;
196 }
197
198 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
199 ExtensionTreatment treatment =
200 resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type());
201 try {
202 treatment.setPropertyValue(TUNNEL_DST, remoteIp);
203 return treatment;
204 } catch (ExtensionPropertyException e) {
205 log.warn("Failed to get tunnelDst extension treatment for {} " +
206 "because of {}", deviceId, e);
207 return null;
208 }
209 }
210}