2 * Copyright (C) 2014 Red Hat, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Madhu Venugopal
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
12 import java.math.BigInteger;
13 import java.util.List;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.LinkedBlockingQueue;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.ClassifierProvider;
18 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
19 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
21 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
22 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
45 import com.google.common.collect.Lists;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider {
50 public final static long REG_VALUE_FROM_LOCAL = 0x1L;
51 public final static long REG_VALUE_FROM_REMOTE = 0x2L;
52 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
54 public ClassifierService() {
55 super(Service.CLASSIFIER);
58 public ClassifierService(Service service) {
62 private static final Logger logger = LoggerFactory.getLogger(ClassifierService.class);
65 * (Table:Classifier) Egress VM Traffic Towards TEP
66 * Match: Destination Ethernet Addr and OpenFlow InPort
67 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
68 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
69 * actions=set_field:5->tun_id,goto_table=<next-table>"
73 public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
74 String nodeName = OPENFLOW + dpidLong;
76 MatchBuilder matchBuilder = new MatchBuilder();
77 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
78 FlowBuilder flowBuilder = new FlowBuilder();
80 // Create the OF Match using MatchBuilder
81 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
82 // TODO Broken In_Port Match
83 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
85 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
86 // Add Flow Attributes
87 flowBuilder.setId(new FlowId(flowId));
88 FlowKey key = new FlowKey(new FlowId(flowId));
89 flowBuilder.setStrict(true);
90 flowBuilder.setBarrier(false);
91 flowBuilder.setTableId(getTable());
92 flowBuilder.setKey(key);
93 flowBuilder.setFlowName(flowId);
94 flowBuilder.setHardTimeout(0);
95 flowBuilder.setIdleTimeout(0);
98 // Instantiate the Builders for the OF Actions and Instructions
99 InstructionBuilder ib = new InstructionBuilder();
100 InstructionsBuilder isb = new InstructionsBuilder();
102 // Instructions List Stores Individual Instructions
103 List<Instruction> instructions = Lists.newArrayList();
105 // TODO Broken SetTunID
106 InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
107 ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
108 List<Action> actionList = aac.getApplyActions().getAction();
110 ActionBuilder ab = new ActionBuilder();
111 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
112 BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
114 ab.setKey(new ActionKey(1));
115 actionList.add(ab.build());
118 ib.setKey(new InstructionKey(0));
119 instructions.add(ib.build());
121 // Next service GOTO Instructions Need to be appended to the List
122 ib = this.getMutablePipelineInstructionBuilder();
124 ib.setKey(new InstructionKey(1));
125 instructions.add(ib.build());
127 // Add InstructionBuilder to the Instruction(s)Builder List
128 isb.setInstruction(instructions);
130 // Add InstructionsBuilder to FlowBuilder
131 flowBuilder.setInstructions(isb.build());
133 writeFlow(flowBuilder, nodeBuilder);
135 removeFlow(flowBuilder, nodeBuilder);
140 * (Table:Classifier) Egress VM Traffic Towards TEP
141 * Match: Source Ethernet Addr and OpenFlow InPort
142 * Instruction: Set VLANID and GOTO Table Egress (n)
143 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
144 * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
145 * table=0,in_port=1,vlan_tci=0x0000/0x1fff,dl_src=fa:16:3e:70:2f:c2 actions=push_vlan:0x8100,set_field:6097->vlan_vid,goto_table:20
149 public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
151 String nodeName = OPENFLOW + dpidLong;
153 MatchBuilder matchBuilder = new MatchBuilder();
154 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
155 FlowBuilder flowBuilder = new FlowBuilder();
157 // Create the OF Match using MatchBuilder
158 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
159 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
160 /* openflowplugin requires a vlan match to add a vlan */
161 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(0), false).build());
163 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
164 // Add Flow Attributes
165 flowBuilder.setId(new FlowId(flowId));
166 FlowKey key = new FlowKey(new FlowId(flowId));
167 flowBuilder.setStrict(true);
168 flowBuilder.setBarrier(false);
169 flowBuilder.setTableId(getTable());
170 flowBuilder.setKey(key);
171 flowBuilder.setFlowName(flowId);
172 flowBuilder.setHardTimeout(0);
173 flowBuilder.setIdleTimeout(0);
176 // Instantiate the Builders for the OF Actions and Instructions
177 InstructionBuilder ib = new InstructionBuilder();
178 InstructionsBuilder isb = new InstructionsBuilder();
180 // Instructions List Stores Individual Instructions
181 List<Instruction> instructions = Lists.newArrayList();
183 // Set VLAN ID Instruction
184 InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
186 ib.setKey(new InstructionKey(0));
187 instructions.add(ib.build());
189 // Next service GOTO Instructions Need to be appended to the List
190 ib = this.getMutablePipelineInstructionBuilder();
192 ib.setKey(new InstructionKey(1));
193 instructions.add(ib.build());
195 // Add InstructionBuilder to the Instruction(s)Builder List
196 isb.setInstruction(instructions);
198 // Add InstructionsBuilder to FlowBuilder
199 flowBuilder.setInstructions(isb.build());
201 writeFlow(flowBuilder, nodeBuilder);
203 removeFlow(flowBuilder, nodeBuilder);
208 * (Table:Classifier) Drop frames source from a VM that do not
209 * match the associated MAC address of the local VM.
210 * Match: Low priority anything not matching the VM SMAC
212 * table=0,priority=16384,in_port=1 actions=drop"
216 public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
218 String nodeName = OPENFLOW + dpidLong;
220 MatchBuilder matchBuilder = new MatchBuilder();
221 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
222 FlowBuilder flowBuilder = new FlowBuilder();
224 // Create the OF Match using MatchBuilder
225 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
228 // Instantiate the Builders for the OF Actions and Instructions
229 InstructionBuilder ib = new InstructionBuilder();
230 InstructionsBuilder isb = new InstructionsBuilder();
232 // Instructions List Stores Individual Instructions
233 List<Instruction> instructions = Lists.newArrayList();
235 // Call the InstructionBuilder Methods Containing Actions
236 InstructionUtils.createDropInstructions(ib);
238 ib.setKey(new InstructionKey(0));
239 instructions.add(ib.build());
241 // Add InstructionBuilder to the Instruction(s)Builder List
242 isb.setInstruction(instructions);
244 // Add InstructionsBuilder to FlowBuilder
245 flowBuilder.setInstructions(isb.build());
248 String flowId = "DropFilter_"+inPort;
249 // Add Flow Attributes
250 flowBuilder.setId(new FlowId(flowId));
251 FlowKey key = new FlowKey(new FlowId(flowId));
252 flowBuilder.setStrict(true);
253 flowBuilder.setBarrier(false);
254 flowBuilder.setTableId(getTable());
255 flowBuilder.setKey(key);
256 flowBuilder.setFlowName(flowId);
257 flowBuilder.setPriority(8192);
258 flowBuilder.setHardTimeout(0);
259 flowBuilder.setIdleTimeout(0);
261 writeFlow(flowBuilder, nodeBuilder);
263 removeFlow(flowBuilder, nodeBuilder);
267 * (Table:0) Ingress Tunnel Traffic
268 * Match: OpenFlow InPort and Tunnel ID
269 * Action: GOTO Local Table (10)
270 * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
274 public void programTunnelIn(Long dpidLong, String segmentationId,
275 Long ofPort, boolean write) {
277 String nodeName = OPENFLOW + dpidLong;
279 BigInteger tunnelId = new BigInteger(segmentationId);
280 MatchBuilder matchBuilder = new MatchBuilder();
281 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
282 FlowBuilder flowBuilder = new FlowBuilder();
284 // Create Match(es) and Set them in the FlowBuilder Object
285 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
286 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
289 // Create the OF Actions and Instructions
290 InstructionBuilder ib = new InstructionBuilder();
291 InstructionsBuilder isb = new InstructionsBuilder();
293 // Instructions List Stores Individual Instructions
294 List<Instruction> instructions = Lists.newArrayList();
296 List<Action> actionList = Lists.newArrayList();
297 ActionBuilder ab = new ActionBuilder();
298 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
299 BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
301 ab.setKey(new ActionKey(0));
302 actionList.add(ab.build());
304 ApplyActionsBuilder aab = new ApplyActionsBuilder();
305 aab.setAction(actionList);
306 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
308 // Call the InstructionBuilder Methods Containing Actions
310 ib.setKey(new InstructionKey(0));
311 instructions.add(ib.build());
313 // Append the default pipeline after the first classification
314 ib = this.getMutablePipelineInstructionBuilder();
316 ib.setKey(new InstructionKey(1));
317 instructions.add(ib.build());
319 // Add InstructionBuilder to the Instruction(s)Builder List
320 isb.setInstruction(instructions);
322 // Add InstructionsBuilder to FlowBuilder
323 flowBuilder.setInstructions(isb.build());
326 String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
327 // Add Flow Attributes
328 flowBuilder.setId(new FlowId(flowId));
329 FlowKey key = new FlowKey(new FlowId(flowId));
330 flowBuilder.setStrict(true);
331 flowBuilder.setBarrier(false);
332 flowBuilder.setTableId(getTable());
333 flowBuilder.setKey(key);
334 flowBuilder.setFlowName(flowId);
335 flowBuilder.setHardTimeout(0);
336 flowBuilder.setIdleTimeout(0);
339 writeFlow(flowBuilder, nodeBuilder);
341 removeFlow(flowBuilder, nodeBuilder);
346 * (Table:0) Ingress VLAN Traffic
347 * Match: OpenFlow InPort and vlan ID
348 * Action: GOTO Local Table (20)
349 * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
350 * table=0,in_port=2,dl_vlan=2001 actions=goto_table:20
354 public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
356 String nodeName = OPENFLOW + dpidLong;
358 MatchBuilder matchBuilder = new MatchBuilder();
359 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
360 FlowBuilder flowBuilder = new FlowBuilder();
362 // Create Match(es) and Set them in the FlowBuilder Object
363 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
364 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort).build());
367 // Create the OF Actions and Instructions
368 InstructionBuilder ib = new InstructionBuilder();
369 InstructionsBuilder isb = new InstructionsBuilder();
371 // Instructions List Stores Individual Instructions
372 List<Instruction> instructions = Lists.newArrayList();
374 // Append the default pipeline after the first classification
375 ib = this.getMutablePipelineInstructionBuilder();
377 ib.setKey(new InstructionKey(0));
378 instructions.add(ib.build());
380 // Add InstructionBuilder to the Instruction(s)Builder List
381 isb.setInstruction(instructions);
383 // Add InstructionsBuilder to FlowBuilder
384 flowBuilder.setInstructions(isb.build());
387 String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
388 // Add Flow Attributes
389 flowBuilder.setId(new FlowId(flowId));
390 FlowKey key = new FlowKey(new FlowId(flowId));
391 flowBuilder.setStrict(true);
392 flowBuilder.setBarrier(false);
393 flowBuilder.setTableId(getTable());
394 flowBuilder.setKey(key);
395 flowBuilder.setFlowName(flowId);
396 flowBuilder.setHardTimeout(0);
397 flowBuilder.setIdleTimeout(0);
399 writeFlow(flowBuilder, nodeBuilder);
401 removeFlow(flowBuilder, nodeBuilder);
406 * Create an LLDP Flow Rule to encapsulate into
407 * a packet_in that is sent to the controller
408 * for topology handling.
409 * Match: Ethertype 0x88CCL
410 * Action: Punt to Controller in a Packet_In msg
414 public void programLLDPPuntRule(Long dpidLong) {
416 String nodeName = OPENFLOW + dpidLong;
417 EtherType etherType = new EtherType(0x88CCL);
419 MatchBuilder matchBuilder = new MatchBuilder();
420 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
421 FlowBuilder flowBuilder = new FlowBuilder();
423 // Create Match(es) and Set them in the FlowBuilder Object
424 flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
426 // Create the OF Actions and Instructions
427 InstructionBuilder ib = new InstructionBuilder();
428 InstructionsBuilder isb = new InstructionsBuilder();
430 // Instructions List Stores Individual Instructions
431 List<Instruction> instructions = Lists.newArrayList();
433 // Call the InstructionBuilder Methods Containing Actions
434 InstructionUtils.createSendToControllerInstructions(nodeName, ib);
436 ib.setKey(new InstructionKey(0));
437 instructions.add(ib.build());
439 // Add InstructionBuilder to the Instruction(s)Builder List
440 isb.setInstruction(instructions);
442 // Add InstructionsBuilder to FlowBuilder
443 flowBuilder.setInstructions(isb.build());
445 String flowId = "LLDP";
446 flowBuilder.setId(new FlowId(flowId));
447 FlowKey key = new FlowKey(new FlowId(flowId));
448 flowBuilder.setBarrier(true);
449 flowBuilder.setTableId(getTable());
450 flowBuilder.setKey(key);
451 flowBuilder.setFlowName(flowId);
452 flowBuilder.setHardTimeout(0);
453 flowBuilder.setIdleTimeout(0);
454 writeFlow(flowBuilder, nodeBuilder);