a7baeb3525fd37f4f6fe9b5d166e7cca43be960f
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / ClassifierService.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Madhu Venugopal
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11
12 import java.math.BigInteger;
13 import java.util.List;
14
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;
44
45 import com.google.common.collect.Lists;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
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;
53
54     public ClassifierService() {
55         super(Service.CLASSIFIER);
56     }
57
58     public ClassifierService(Service service) {
59         super(service);
60     }
61
62     private static final Logger logger = LoggerFactory.getLogger(ClassifierService.class);
63
64     /*
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>"
70      */
71
72     @Override
73     public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
74         String nodeName = OPENFLOW + dpidLong;
75
76         MatchBuilder matchBuilder = new MatchBuilder();
77         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
78         FlowBuilder flowBuilder = new FlowBuilder();
79
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());
84
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);
96
97         if (write) {
98             // Instantiate the Builders for the OF Actions and Instructions
99             InstructionBuilder ib = new InstructionBuilder();
100             InstructionsBuilder isb = new InstructionsBuilder();
101
102             // Instructions List Stores Individual Instructions
103             List<Instruction> instructions = Lists.newArrayList();
104
105             // TODO Broken SetTunID
106             InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
107             ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
108             List<Action> actionList = aac.getApplyActions().getAction();
109
110             ActionBuilder ab = new ActionBuilder();
111             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
112                     BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
113             ab.setOrder(1);
114             ab.setKey(new ActionKey(1));
115             actionList.add(ab.build());
116
117             ib.setOrder(0);
118             ib.setKey(new InstructionKey(0));
119             instructions.add(ib.build());
120
121             // Next service GOTO Instructions Need to be appended to the List
122             ib = this.getMutablePipelineInstructionBuilder();
123             ib.setOrder(1);
124             ib.setKey(new InstructionKey(1));
125             instructions.add(ib.build());
126
127             // Add InstructionBuilder to the Instruction(s)Builder List
128             isb.setInstruction(instructions);
129
130             // Add InstructionsBuilder to FlowBuilder
131             flowBuilder.setInstructions(isb.build());
132
133             writeFlow(flowBuilder, nodeBuilder);
134         } else {
135             removeFlow(flowBuilder, nodeBuilder);
136         }
137     }
138
139     /*
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
146      */
147
148     @Override
149     public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
150
151         String nodeName = OPENFLOW + dpidLong;
152
153         MatchBuilder matchBuilder = new MatchBuilder();
154         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
155         FlowBuilder flowBuilder = new FlowBuilder();
156
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());
162
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);
174
175         if (write) {
176             // Instantiate the Builders for the OF Actions and Instructions
177             InstructionBuilder ib = new InstructionBuilder();
178             InstructionsBuilder isb = new InstructionsBuilder();
179
180             // Instructions List Stores Individual Instructions
181             List<Instruction> instructions = Lists.newArrayList();
182
183             // Set VLAN ID Instruction
184             InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
185             ib.setOrder(0);
186             ib.setKey(new InstructionKey(0));
187             instructions.add(ib.build());
188
189             // Next service GOTO Instructions Need to be appended to the List
190             ib = this.getMutablePipelineInstructionBuilder();
191             ib.setOrder(1);
192             ib.setKey(new InstructionKey(1));
193             instructions.add(ib.build());
194
195             // Add InstructionBuilder to the Instruction(s)Builder List
196             isb.setInstruction(instructions);
197
198             // Add InstructionsBuilder to FlowBuilder
199             flowBuilder.setInstructions(isb.build());
200
201             writeFlow(flowBuilder, nodeBuilder);
202         } else {
203             removeFlow(flowBuilder, nodeBuilder);
204         }
205     }
206
207     /*
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
211      * Instruction: Drop
212      * table=0,priority=16384,in_port=1 actions=drop"
213      */
214
215     @Override
216     public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
217
218         String nodeName = OPENFLOW + dpidLong;
219
220         MatchBuilder matchBuilder = new MatchBuilder();
221         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
222         FlowBuilder flowBuilder = new FlowBuilder();
223
224         // Create the OF Match using MatchBuilder
225         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
226
227         if (write) {
228             // Instantiate the Builders for the OF Actions and Instructions
229             InstructionBuilder ib = new InstructionBuilder();
230             InstructionsBuilder isb = new InstructionsBuilder();
231
232             // Instructions List Stores Individual Instructions
233             List<Instruction> instructions = Lists.newArrayList();
234
235             // Call the InstructionBuilder Methods Containing Actions
236             InstructionUtils.createDropInstructions(ib);
237             ib.setOrder(0);
238             ib.setKey(new InstructionKey(0));
239             instructions.add(ib.build());
240
241             // Add InstructionBuilder to the Instruction(s)Builder List
242             isb.setInstruction(instructions);
243
244             // Add InstructionsBuilder to FlowBuilder
245             flowBuilder.setInstructions(isb.build());
246         }
247
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);
260         if (write) {
261             writeFlow(flowBuilder, nodeBuilder);
262         } else {
263             removeFlow(flowBuilder, nodeBuilder);
264         }
265     }
266     /*
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
271      */
272
273     @Override
274     public void programTunnelIn(Long dpidLong, String segmentationId,
275             Long ofPort, boolean write) {
276
277         String nodeName = OPENFLOW + dpidLong;
278
279         BigInteger tunnelId = new BigInteger(segmentationId);
280         MatchBuilder matchBuilder = new MatchBuilder();
281         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
282         FlowBuilder flowBuilder = new FlowBuilder();
283
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());
287
288         if (write) {
289             // Create the OF Actions and Instructions
290             InstructionBuilder ib = new InstructionBuilder();
291             InstructionsBuilder isb = new InstructionsBuilder();
292
293             // Instructions List Stores Individual Instructions
294             List<Instruction> instructions = Lists.newArrayList();
295
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)));
300             ab.setOrder(0);
301             ab.setKey(new ActionKey(0));
302             actionList.add(ab.build());
303
304             ApplyActionsBuilder aab = new ApplyActionsBuilder();
305             aab.setAction(actionList);
306             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
307
308             // Call the InstructionBuilder Methods Containing Actions
309             ib.setOrder(0);
310             ib.setKey(new InstructionKey(0));
311             instructions.add(ib.build());
312
313             // Append the default pipeline after the first classification
314             ib = this.getMutablePipelineInstructionBuilder();
315             ib.setOrder(1);
316             ib.setKey(new InstructionKey(1));
317             instructions.add(ib.build());
318
319             // Add InstructionBuilder to the Instruction(s)Builder List
320             isb.setInstruction(instructions);
321
322             // Add InstructionsBuilder to FlowBuilder
323             flowBuilder.setInstructions(isb.build());
324         }
325
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);
337
338         if (write) {
339             writeFlow(flowBuilder, nodeBuilder);
340         } else {
341             removeFlow(flowBuilder, nodeBuilder);
342         }
343     }
344
345     /*
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
351      */
352
353     @Override
354     public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
355
356         String nodeName = OPENFLOW + dpidLong;
357
358         MatchBuilder matchBuilder = new MatchBuilder();
359         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
360         FlowBuilder flowBuilder = new FlowBuilder();
361
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());
365
366         if (write) {
367             // Create the OF Actions and Instructions
368             InstructionBuilder ib = new InstructionBuilder();
369             InstructionsBuilder isb = new InstructionsBuilder();
370
371             // Instructions List Stores Individual Instructions
372             List<Instruction> instructions = Lists.newArrayList();
373
374             // Append the default pipeline after the first classification
375             ib = this.getMutablePipelineInstructionBuilder();
376             ib.setOrder(0);
377             ib.setKey(new InstructionKey(0));
378             instructions.add(ib.build());
379
380             // Add InstructionBuilder to the Instruction(s)Builder List
381             isb.setInstruction(instructions);
382
383             // Add InstructionsBuilder to FlowBuilder
384             flowBuilder.setInstructions(isb.build());
385         }
386
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);
398         if (write) {
399             writeFlow(flowBuilder, nodeBuilder);
400         } else {
401             removeFlow(flowBuilder, nodeBuilder);
402         }
403     }
404
405     /*
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
411      */
412
413     @Override
414     public void programLLDPPuntRule(Long dpidLong) {
415
416         String nodeName = OPENFLOW + dpidLong;
417         EtherType etherType = new EtherType(0x88CCL);
418
419         MatchBuilder matchBuilder = new MatchBuilder();
420         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
421         FlowBuilder flowBuilder = new FlowBuilder();
422
423         // Create Match(es) and Set them in the FlowBuilder Object
424         flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
425
426         // Create the OF Actions and Instructions
427         InstructionBuilder ib = new InstructionBuilder();
428         InstructionsBuilder isb = new InstructionsBuilder();
429
430         // Instructions List Stores Individual Instructions
431         List<Instruction> instructions = Lists.newArrayList();
432
433         // Call the InstructionBuilder Methods Containing Actions
434         InstructionUtils.createSendToControllerInstructions(nodeName, ib);
435         ib.setOrder(0);
436         ib.setKey(new InstructionKey(0));
437         instructions.add(ib.build());
438
439         // Add InstructionBuilder to the Instruction(s)Builder List
440         isb.setInstruction(instructions);
441
442         // Add InstructionsBuilder to FlowBuilder
443         flowBuilder.setInstructions(isb.build());
444
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);
455     }
456 }