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