Bug 2004 - Fixing issue where local ports are not receivers for flood originating...
[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 org.opendaylight.ovsdb.openstack.netvirt.api.ClassifierProvider;
16 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
18 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
42
43 import com.google.common.collect.Lists;
44
45 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider {
46     public final static long REG_VALUE_FROM_LOCAL = 0x1L;
47     public final static long REG_VALUE_FROM_REMOTE = 0x2L;
48     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
49
50     public ClassifierService() {
51         super(Service.CLASSIFIER);
52     }
53
54     public ClassifierService(Service service) {
55         super(service);
56     }
57
58     @Override
59     public boolean isBridgeInPipeline (String nodeId) {
60         return true;
61     }
62
63     /*
64      * (Table:Classifier) Egress VM Traffic Towards TEP
65      * Match: Destination Ethernet Addr and OpenFlow InPort
66      * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
67      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
68      * actions=set_field:5->tun_id,goto_table=<next-table>"
69      */
70
71     @Override
72     public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
73         String nodeName = OPENFLOW + dpidLong;
74
75         MatchBuilder matchBuilder = new MatchBuilder();
76         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
77         FlowBuilder flowBuilder = new FlowBuilder();
78
79         // Create the OF Match using MatchBuilder
80         flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
81         // TODO Broken In_Port Match
82         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
83
84         String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
85         // Add Flow Attributes
86         flowBuilder.setId(new FlowId(flowId));
87         FlowKey key = new FlowKey(new FlowId(flowId));
88         flowBuilder.setStrict(true);
89         flowBuilder.setBarrier(false);
90         flowBuilder.setTableId(getTable());
91         flowBuilder.setKey(key);
92         flowBuilder.setFlowName(flowId);
93         flowBuilder.setHardTimeout(0);
94         flowBuilder.setIdleTimeout(0);
95
96         if (write) {
97             // Instantiate the Builders for the OF Actions and Instructions
98             InstructionBuilder ib = new InstructionBuilder();
99             InstructionsBuilder isb = new InstructionsBuilder();
100
101             // Instructions List Stores Individual Instructions
102             List<Instruction> instructions = Lists.newArrayList();
103
104             // TODO Broken SetTunID
105             InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
106             ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
107             List<Action> actionList = aac.getApplyActions().getAction();
108
109             ActionBuilder ab = new ActionBuilder();
110             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
111                     BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
112             ab.setOrder(1);
113             ab.setKey(new ActionKey(1));
114             actionList.add(ab.build());
115
116             ib.setOrder(0);
117             ib.setKey(new InstructionKey(0));
118             instructions.add(ib.build());
119
120             // Next service GOTO Instructions Need to be appended to the List
121             ib = this.getMutablePipelineInstructionBuilder();
122             ib.setOrder(1);
123             ib.setKey(new InstructionKey(1));
124             instructions.add(ib.build());
125
126             // Add InstructionBuilder to the Instruction(s)Builder List
127             isb.setInstruction(instructions);
128
129             // Add InstructionsBuilder to FlowBuilder
130             flowBuilder.setInstructions(isb.build());
131
132             writeFlow(flowBuilder, nodeBuilder);
133         } else {
134             removeFlow(flowBuilder, nodeBuilder);
135         }
136     }
137
138     /*
139      * (Table:Classifier) Egress VM Traffic Towards TEP
140      * Match: Source Ethernet Addr and OpenFlow InPort
141      * Instruction: Set VLANID and GOTO Table Egress (n)
142      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
143      * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
144      */
145
146     @Override
147     public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
148
149         String nodeName = OPENFLOW + dpidLong;
150
151         MatchBuilder matchBuilder = new MatchBuilder();
152         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
153         FlowBuilder flowBuilder = new FlowBuilder();
154
155         // Create the OF Match using MatchBuilder
156         flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
157         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
158
159         String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
160         // Add Flow Attributes
161         flowBuilder.setId(new FlowId(flowId));
162         FlowKey key = new FlowKey(new FlowId(flowId));
163         flowBuilder.setStrict(true);
164         flowBuilder.setBarrier(false);
165         flowBuilder.setTableId(getTable());
166         flowBuilder.setKey(key);
167         flowBuilder.setFlowName(flowId);
168         flowBuilder.setHardTimeout(0);
169         flowBuilder.setIdleTimeout(0);
170
171         if (write) {
172             // Instantiate the Builders for the OF Actions and Instructions
173             InstructionBuilder ib = new InstructionBuilder();
174             InstructionsBuilder isb = new InstructionsBuilder();
175
176             // Instructions List Stores Individual Instructions
177             List<Instruction> instructions = Lists.newArrayList();
178
179             // Set VLAN ID Instruction
180             InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
181             ib.setOrder(0);
182             ib.setKey(new InstructionKey(0));
183             instructions.add(ib.build());
184
185             // Next service GOTO Instructions Need to be appended to the List
186             ib = this.getMutablePipelineInstructionBuilder();
187             ib.setOrder(1);
188             ib.setKey(new InstructionKey(1));
189             instructions.add(ib.build());
190
191             // Add InstructionBuilder to the Instruction(s)Builder List
192             isb.setInstruction(instructions);
193
194             // Add InstructionsBuilder to FlowBuilder
195             flowBuilder.setInstructions(isb.build());
196
197             writeFlow(flowBuilder, nodeBuilder);
198         } else {
199             removeFlow(flowBuilder, nodeBuilder);
200         }
201     }
202
203     /*
204      * (Table:Classifier) Drop frames source from a VM that do not
205      * match the associated MAC address of the local VM.
206      * Match: Low priority anything not matching the VM SMAC
207      * Instruction: Drop
208      * table=0,priority=16384,in_port=1 actions=drop"
209      */
210
211     @Override
212     public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
213
214         String nodeName = OPENFLOW + dpidLong;
215
216         MatchBuilder matchBuilder = new MatchBuilder();
217         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
218         FlowBuilder flowBuilder = new FlowBuilder();
219
220         // Create the OF Match using MatchBuilder
221         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
222
223         if (write) {
224             // Instantiate the Builders for the OF Actions and Instructions
225             InstructionBuilder ib = new InstructionBuilder();
226             InstructionsBuilder isb = new InstructionsBuilder();
227
228             // Instructions List Stores Individual Instructions
229             List<Instruction> instructions = Lists.newArrayList();
230
231             // Call the InstructionBuilder Methods Containing Actions
232             InstructionUtils.createDropInstructions(ib);
233             ib.setOrder(0);
234             ib.setKey(new InstructionKey(0));
235             instructions.add(ib.build());
236
237             // Add InstructionBuilder to the Instruction(s)Builder List
238             isb.setInstruction(instructions);
239
240             // Add InstructionsBuilder to FlowBuilder
241             flowBuilder.setInstructions(isb.build());
242         }
243
244         String flowId = "DropFilter_"+inPort;
245         // Add Flow Attributes
246         flowBuilder.setId(new FlowId(flowId));
247         FlowKey key = new FlowKey(new FlowId(flowId));
248         flowBuilder.setStrict(true);
249         flowBuilder.setBarrier(false);
250         flowBuilder.setTableId(getTable());
251         flowBuilder.setKey(key);
252         flowBuilder.setFlowName(flowId);
253         flowBuilder.setPriority(8192);
254         flowBuilder.setHardTimeout(0);
255         flowBuilder.setIdleTimeout(0);
256         if (write) {
257             writeFlow(flowBuilder, nodeBuilder);
258         } else {
259             removeFlow(flowBuilder, nodeBuilder);
260         }
261     }
262     /*
263      * (Table:0) Ingress Tunnel Traffic
264      * Match: OpenFlow InPort and Tunnel ID
265      * Action: GOTO Local Table (10)
266      * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
267      */
268
269     @Override
270     public void programTunnelIn(Long dpidLong, String segmentationId,
271             Long ofPort, boolean write) {
272
273         String nodeName = OPENFLOW + dpidLong;
274
275         BigInteger tunnelId = new BigInteger(segmentationId);
276         MatchBuilder matchBuilder = new MatchBuilder();
277         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
278         FlowBuilder flowBuilder = new FlowBuilder();
279
280         // Create Match(es) and Set them in the FlowBuilder Object
281         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
282         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
283
284         if (write) {
285             // Create the OF Actions and Instructions
286             InstructionBuilder ib = new InstructionBuilder();
287             InstructionsBuilder isb = new InstructionsBuilder();
288
289             // Instructions List Stores Individual Instructions
290             List<Instruction> instructions = Lists.newArrayList();
291
292             List<Action> actionList = Lists.newArrayList();
293             ActionBuilder ab = new ActionBuilder();
294             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
295                     BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
296             ab.setOrder(0);
297             ab.setKey(new ActionKey(0));
298             actionList.add(ab.build());
299
300             ApplyActionsBuilder aab = new ApplyActionsBuilder();
301             aab.setAction(actionList);
302             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
303
304             // Call the InstructionBuilder Methods Containing Actions
305             ib.setOrder(0);
306             ib.setKey(new InstructionKey(0));
307             instructions.add(ib.build());
308
309             // Append the default pipeline after the first classification
310             ib = this.getMutablePipelineInstructionBuilder();
311             ib.setOrder(1);
312             ib.setKey(new InstructionKey(1));
313             instructions.add(ib.build());
314
315             // Add InstructionBuilder to the Instruction(s)Builder List
316             isb.setInstruction(instructions);
317
318             // Add InstructionsBuilder to FlowBuilder
319             flowBuilder.setInstructions(isb.build());
320         }
321
322         String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
323         // Add Flow Attributes
324         flowBuilder.setId(new FlowId(flowId));
325         FlowKey key = new FlowKey(new FlowId(flowId));
326         flowBuilder.setStrict(true);
327         flowBuilder.setBarrier(false);
328         flowBuilder.setTableId(getTable());
329         flowBuilder.setKey(key);
330         flowBuilder.setFlowName(flowId);
331         flowBuilder.setHardTimeout(0);
332         flowBuilder.setIdleTimeout(0);
333
334         if (write) {
335             writeFlow(flowBuilder, nodeBuilder);
336         } else {
337             removeFlow(flowBuilder, nodeBuilder);
338         }
339     }
340
341     /*
342      * (Table:0) Ingress VLAN Traffic
343      * Match: OpenFlow InPort and vlan ID
344      * Action: GOTO Local Table (20)
345      * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
346      */
347
348     @Override
349     public void programVlanIn(Long dpidLong, String segmentationId,  Long ethPort, boolean write) {
350
351         String nodeName = OPENFLOW + dpidLong;
352
353         MatchBuilder matchBuilder = new MatchBuilder();
354         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
355         FlowBuilder flowBuilder = new FlowBuilder();
356
357         // Create Match(es) and Set them in the FlowBuilder Object
358         flowBuilder.setMatch(
359                 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true)
360                 .build())
361                 .setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort)
362                         .build());
363
364         if (write) {
365             // Create the OF Actions and Instructions
366             InstructionBuilder ib = new InstructionBuilder();
367             InstructionsBuilder isb = new InstructionsBuilder();
368
369             // Instructions List Stores Individual Instructions
370             List<Instruction> instructions = Lists.newArrayList();
371
372             // Call the InstructionBuilder Methods Containing Actions
373             ib = this.getMutablePipelineInstructionBuilder();
374             ib.setOrder(0);
375             ib.setKey(new InstructionKey(0));
376             instructions.add(ib.build());
377
378             // Add InstructionBuilder to the Instruction(s)Builder List
379             isb.setInstruction(instructions);
380
381             // Add InstructionsBuilder to FlowBuilder
382             flowBuilder.setInstructions(isb.build());
383         }
384
385         String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
386         // Add Flow Attributes
387         flowBuilder.setId(new FlowId(flowId));
388         FlowKey key = new FlowKey(new FlowId(flowId));
389         flowBuilder.setStrict(true);
390         flowBuilder.setBarrier(false);
391         flowBuilder.setTableId(getTable());
392         flowBuilder.setKey(key);
393         flowBuilder.setFlowName(flowId);
394         flowBuilder.setHardTimeout(0);
395         flowBuilder.setIdleTimeout(0);
396         if (write) {
397             writeFlow(flowBuilder, nodeBuilder);
398         } else {
399             removeFlow(flowBuilder, nodeBuilder);
400         }
401     }
402
403     /*
404      * Create an LLDP Flow Rule to encapsulate into
405      * a packet_in that is sent to the controller
406      * for topology handling.
407      * Match: Ethertype 0x88CCL
408      * Action: Punt to Controller in a Packet_In msg
409      */
410
411     @Override
412     public void programLLDPPuntRule(Long dpidLong) {
413
414         String nodeName = OPENFLOW + dpidLong;
415         EtherType etherType = new EtherType(0x88CCL);
416
417         MatchBuilder matchBuilder = new MatchBuilder();
418         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
419         FlowBuilder flowBuilder = new FlowBuilder();
420
421         // Create Match(es) and Set them in the FlowBuilder Object
422         flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
423
424         // Create the OF Actions and Instructions
425         InstructionBuilder ib = new InstructionBuilder();
426         InstructionsBuilder isb = new InstructionsBuilder();
427
428         // Instructions List Stores Individual Instructions
429         List<Instruction> instructions = Lists.newArrayList();
430
431         // Call the InstructionBuilder Methods Containing Actions
432         InstructionUtils.createSendToControllerInstructions(ib);
433         ib.setOrder(0);
434         ib.setKey(new InstructionKey(0));
435         instructions.add(ib.build());
436
437         // Add InstructionBuilder to the Instruction(s)Builder List
438         isb.setInstruction(instructions);
439
440         // Add InstructionsBuilder to FlowBuilder
441         flowBuilder.setInstructions(isb.build());
442
443         String flowId = "LLDP";
444         flowBuilder.setId(new FlowId(flowId));
445         FlowKey key = new FlowKey(new FlowId(flowId));
446         flowBuilder.setBarrier(true);
447         flowBuilder.setTableId(getTable());
448         flowBuilder.setKey(key);
449         flowBuilder.setFlowName(flowId);
450         flowBuilder.setHardTimeout(0);
451         flowBuilder.setIdleTimeout(0);
452         writeFlow(flowBuilder, nodeBuilder);
453     }
454 }