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