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 org.opendaylight.ovsdb.openstack.netvirt.api.L2ForwardingProvider;
16 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
18 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
21 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
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.action.OutputActionCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
47 import org.osgi.framework.BundleContext;
48 import org.osgi.framework.ServiceReference;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.google.common.collect.Lists;
54 public class L2ForwardingService extends AbstractServiceInstance implements ConfigInterface, L2ForwardingProvider {
55 private static final Logger LOG = LoggerFactory.getLogger(L2ForwardingService.class);
56 public L2ForwardingService() {
57 super(Service.L2_FORWARDING);
60 public L2ForwardingService(Service service) {
65 * (Table:L2Forwarding) Local Unicast
66 * Match: Tunnel ID and dMAC
68 * table=2,tun_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2 goto:<next-table>
71 public void programLocalUcastOut(Long dpidLong, String segmentationId,
72 Long localPort, 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.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
82 flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
84 String flowId = "UcastOut_"+segmentationId+"_"+localPort+"_"+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);
97 // Instantiate the Builders for the OF Actions and Instructions
98 InstructionBuilder ib = new InstructionBuilder();
99 InstructionsBuilder isb = new InstructionsBuilder();
101 // Instructions List Stores Individual Instructions
102 List<Instruction> instructions = Lists.newArrayList();
104 // Set the Output Port/Iface
105 InstructionUtils.createOutputPortInstructions(ib, dpidLong, localPort);
107 ib.setKey(new InstructionKey(0));
108 instructions.add(ib.build());
110 // Add InstructionBuilder to the Instruction(s)Builder List
111 isb.setInstruction(instructions);
113 // Add InstructionsBuilder to FlowBuilder
114 flowBuilder.setInstructions(isb.build());
115 writeFlow(flowBuilder, nodeBuilder);
117 removeFlow(flowBuilder, nodeBuilder);
121 * (Table:2) Local VLAN unicast
122 * Match: VLAN ID and dMAC
123 * Action: Output Port
124 * table=2,vlan_id=0x5,dl_dst=00:00:00:00:00:01 actions=output:2
125 * table=110,dl_vlan=2001,dl_dst=fa:16:3e:a3:3b:cc actions=pop_vlan,output:1
129 public void programLocalVlanUcastOut (Long dpidLong, String segmentationId, Long localPort, String attachedMac, boolean write) {
131 String nodeName = OPENFLOW + dpidLong;
133 MatchBuilder matchBuilder = new MatchBuilder();
134 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
135 FlowBuilder flowBuilder = new FlowBuilder();
137 // Create the OF Match using MatchBuilder
138 flowBuilder.setMatch(
139 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
140 flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
142 String flowId = "VlanUcastOut_"+segmentationId+"_"+localPort+"_"+attachedMac;
143 // Add Flow Attributes
144 flowBuilder.setId(new FlowId(flowId));
145 FlowKey key = new FlowKey(new FlowId(flowId));
146 flowBuilder.setStrict(true);
147 flowBuilder.setBarrier(false);
148 flowBuilder.setTableId(getTable());
149 flowBuilder.setKey(key);
150 flowBuilder.setFlowName(flowId);
151 flowBuilder.setHardTimeout(0);
152 flowBuilder.setIdleTimeout(0);
155 // Instantiate the Builders for the OF Actions and Instructions
156 InstructionBuilder ib = new InstructionBuilder();
157 InstructionsBuilder isb = new InstructionsBuilder();
159 // Instructions List Stores Individual Instructions
160 List<Instruction> instructions = Lists.newArrayList();
161 List<Instruction> instructions_tmp = Lists.newArrayList();
163 /* Strip vlan and store to tmp instruction space*/
164 InstructionUtils.createPopVlanInstructions(ib);
166 ib.setKey(new InstructionKey(0));
167 instructions_tmp.add(ib.build());
169 // Set the Output Port/Iface
170 ib = new InstructionBuilder();
171 InstructionUtils.addOutputPortInstructions(ib, dpidLong, localPort, instructions_tmp);
173 ib.setKey(new InstructionKey(0));
174 instructions.add(ib.build());
176 // Add InstructionBuilder to the Instruction(s)Builder List
177 isb.setInstruction(instructions);
179 // Add InstructionsBuilder to FlowBuilder
180 flowBuilder.setInstructions(isb.build());
181 writeFlow(flowBuilder, nodeBuilder);
183 removeFlow(flowBuilder, nodeBuilder);
189 * Utility function used by the flooding logic to allow a flow to be resubmitted
190 * to the local port flooding rule, after being outputed to all available tunnel
191 * or VLAN egress ports.
193 private void appendResubmitLocalFlood(InstructionBuilder ib) {
195 //Update the ApplyActions instructions
196 ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
197 List<Action> actionList = aac.getApplyActions().getAction();
199 int index = actionList.size();
200 ActionBuilder ab = new ActionBuilder();
201 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(ClassifierService.REG_FIELD).build(),
202 BigInteger.valueOf(ClassifierService.REG_VALUE_FROM_REMOTE)));
204 ab.setKey(new ActionKey(index));
205 actionList.add(ab.build());
208 ab = new ActionBuilder();
209 ab.setAction(ActionUtils.nxResubmitAction(null, this.getTable()));
211 ab.setKey(new ActionKey(index));
212 actionList.add(ab.build());
216 * (Table:2) Local Broadcast Flood
217 * Match: Tunnel ID and dMAC (::::FF:FF)
218 * table=2,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
219 * actions=output:2,3,4,5
223 public void programLocalBcastOut(Long dpidLong, String segmentationId, Long localPort, boolean write) {
225 String nodeName = OPENFLOW + dpidLong;
227 MatchBuilder matchBuilder = new MatchBuilder();
228 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
229 FlowBuilder flowBuilder = new FlowBuilder();
231 // Create the OF Match using MatchBuilder
232 MatchUtils.addNxRegMatch(matchBuilder, new MatchUtils.RegMatch(ClassifierService.REG_FIELD, ClassifierService.REG_VALUE_FROM_REMOTE));
233 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
234 flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
235 new MacAddress("01:00:00:00:00:00")).build());
237 String flowId = "BcastOut_"+segmentationId;
238 // Add Flow Attributes
239 flowBuilder.setId(new FlowId(flowId));
240 FlowKey key = new FlowKey(new FlowId(flowId));
241 flowBuilder.setStrict(true);
242 flowBuilder.setBarrier(false);
243 flowBuilder.setTableId(getTable());
244 flowBuilder.setKey(key);
245 flowBuilder.setPriority(16384);
246 flowBuilder.setFlowName(flowId);
247 flowBuilder.setHardTimeout(0);
248 flowBuilder.setIdleTimeout(0);
249 Flow flow = this.getFlow(flowBuilder, nodeBuilder);
250 // Instantiate the Builders for the OF Actions and Instructions
251 InstructionBuilder ib = new InstructionBuilder();
252 InstructionsBuilder isb = new InstructionsBuilder();
253 List<Instruction> instructions = Lists.newArrayList();
254 List<Instruction> existingInstructions = null;
256 Instructions ins = flow.getInstructions();
258 existingInstructions = ins.getInstruction();
263 // Create output port list
264 createOutputPortInstructions(ib, dpidLong, localPort, existingInstructions);
266 ib.setKey(new InstructionKey(0));
268 /* Alternative method to address Bug 2004 is to make a call
269 * here to appendResubmitLocalFlood(ib) so that we send the
270 * flow back to the local flood rule.
272 instructions.add(ib.build());
274 // Add InstructionBuilder to the Instruction(s)Builder List
275 isb.setInstruction(instructions);
277 // Add InstructionsBuilder to FlowBuilder
278 flowBuilder.setInstructions(isb.build());
280 writeFlow(flowBuilder, nodeBuilder);
282 boolean flowRemove = InstructionUtils.removeOutputPortFromInstructions(ib, dpidLong, localPort,
283 existingInstructions);
285 /* if all ports are removed, remove flow */
286 removeFlow(flowBuilder, nodeBuilder);
288 /* Install instruction with new output port list*/
290 ib.setKey(new InstructionKey(0));
291 instructions.add(ib.build());
293 // Add InstructionBuilder to the Instruction(s)Builder List
294 isb.setInstruction(instructions);
296 // Add InstructionsBuilder to FlowBuilder
297 flowBuilder.setInstructions(isb.build());
298 writeFlow(flowBuilder, nodeBuilder);
304 * (Table:2) Local VLAN Broadcast Flood
305 * Match: vlan ID and dMAC (::::FF:FF)
306 * table=2,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
307 * actions=strip_vlan, output:2,3,4,5
308 * table=110,dl_vlan=2001,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 actions=output:2,pop_vlan,output:1,output:3,output:4
312 public void programLocalVlanBcastOut(Long dpidLong, String segmentationId,
313 Long localPort, Long ethPort, boolean write) {
315 String nodeName = OPENFLOW + dpidLong;
317 MatchBuilder matchBuilder = new MatchBuilder();
318 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
319 FlowBuilder flowBuilder = new FlowBuilder();
321 // Create the OF Match using MatchBuilder
322 flowBuilder.setMatch(
323 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
324 flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
325 new MacAddress("01:00:00:00:00:00")).build());
327 String flowId = "VlanBcastOut_"+segmentationId+"_"+ethPort;
328 // Add Flow Attributes
329 flowBuilder.setId(new FlowId(flowId));
330 FlowKey key = new FlowKey(new FlowId(flowId));
331 flowBuilder.setStrict(true);
332 flowBuilder.setBarrier(false);
333 flowBuilder.setTableId(getTable());
334 flowBuilder.setKey(key);
335 flowBuilder.setPriority(16384);
336 flowBuilder.setFlowName(flowId);
337 flowBuilder.setHardTimeout(0);
338 flowBuilder.setIdleTimeout(0);
339 Flow flow = this.getFlow(flowBuilder, nodeBuilder);
340 // Instantiate the Builders for the OF Actions and Instructions
341 List<Instruction> existingInstructions = null;
343 Instructions ins = flow.getInstructions();
345 existingInstructions = ins.getInstruction();
349 List<Instruction> instructions = Lists.newArrayList();
350 InstructionBuilder ib = new InstructionBuilder();
351 List<Action> actionList;
353 if (existingInstructions == null) {
354 /* First time called there should be no instructions.
355 * We can simply add the output:ethPort first, followed by
356 * popVlan and then the local port. The next calls will append
357 * the rest of the local ports.
359 ActionBuilder ab = new ActionBuilder();
360 actionList = Lists.newArrayList();
362 ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":" + ethPort)));
364 ab.setKey(new ActionKey(0));
365 actionList.add(ab.build());
367 ab.setAction(ActionUtils.popVlanAction());
369 ab.setKey(new ActionKey(1));
370 actionList.add(ab.build());
372 ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":" + localPort)));
374 ab.setKey(new ActionKey(2));
375 actionList.add(ab.build());
377 /* Subsequent calls require appending any new local ports for this tenant. */
378 Instruction in = existingInstructions.get(0);
379 actionList = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
381 NodeConnectorId ncid = new NodeConnectorId(nodeName + ":" + localPort);
382 boolean addNew = true;
384 /* Check if the port is already in the output list */
385 for (Action action : actionList) {
386 if (action.getAction() instanceof OutputActionCase) {
387 OutputActionCase opAction = (OutputActionCase) action.getAction();
388 if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
396 ActionBuilder ab = new ActionBuilder();
398 ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":" + localPort)));
399 ab.setOrder(actionList.size());
400 ab.setKey(new ActionKey(actionList.size()));
401 actionList.add(ab.build());
405 ApplyActionsBuilder aab = new ApplyActionsBuilder();
406 aab.setAction(actionList);
407 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
409 ib.setKey(new InstructionKey(0));
410 instructions.add(ib.build());
412 // Add InstructionBuilder to the Instruction(s)Builder List
413 InstructionsBuilder isb = new InstructionsBuilder();
414 isb.setInstruction(instructions);
416 // Add InstructionsBuilder to FlowBuilder
417 flowBuilder.setInstructions(isb.build());
418 writeFlow(flowBuilder, nodeBuilder);
420 //boolean flowRemove = removeOutputPortFromGroup(nodeBuilder, ib, dpidLong,
421 // localPort, existingInstructions);
422 boolean flowRemove = removeOutputPortFromInstructions(ib, dpidLong, localPort, ethPort,
423 existingInstructions);
425 /* if all ports are removed, remove flow */
426 removeFlow(flowBuilder, nodeBuilder);
428 /* Install instruction with new output port list*/
430 ib.setKey(new InstructionKey(0));
431 instructions.add(ib.build());
433 // Add InstructionBuilder to the Instruction(s)Builder List
434 InstructionsBuilder isb = new InstructionsBuilder();
435 isb.setInstruction(instructions);
437 // Add InstructionsBuilder to FlowBuilder
438 flowBuilder.setInstructions(isb.build());
439 writeFlow(flowBuilder, nodeBuilder);
444 private boolean removeOutputPortFromInstructions(InstructionBuilder ib, Long dpidLong, Long localPort,
445 Long ethPort, List<Instruction> instructions) {
446 List<Action> actionList = Lists.newArrayList();
447 boolean removeFlow = true;
449 if (instructions != null) {
450 Instruction in = instructions.get(0);
451 List<Action> oldActionList = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
452 NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + localPort);
453 NodeConnectorId ncidEth = new NodeConnectorId(OPENFLOW + dpidLong + ":" + ethPort);
455 // Remove the port from the output list
456 ActionBuilder ab = new ActionBuilder();
458 //for (ListIterator<Action> it = oldActionList.listIterator(oldActionList.size()); it.hasPrevious();) {
459 // Action action = it.previous();
460 for (Action action : oldActionList) {
461 if (action.getAction() instanceof OutputActionCase) {
462 OutputActionCase opAction = (OutputActionCase) action.getAction();
463 if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncidEth))) {
464 actionList.add(action);
465 } else if (!opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
466 ab.setAction(action.getAction());
468 ab.setKey(new ActionKey(index));
469 actionList.add(ab.build());
473 actionList.add(action);
476 ApplyActionsBuilder aab = new ApplyActionsBuilder();
477 aab.setAction(actionList);
478 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
481 if (actionList.size() > 2) {
482 // Add InstructionBuilder to the Instruction(s)Builder List
483 InstructionsBuilder isb = new InstructionsBuilder();
484 isb.setInstruction(instructions);
492 * (Table:1) Local Table Miss
493 * Match: Any Remaining Flows w/a TunID
494 * Action: Drop w/ a low priority
495 * table=2,priority=8192,tun_id=0x5 actions=drop
499 public void programLocalTableMiss(Long dpidLong, String segmentationId, boolean write) {
501 String nodeName = OPENFLOW + dpidLong;
503 MatchBuilder matchBuilder = new MatchBuilder();
504 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
505 FlowBuilder flowBuilder = new FlowBuilder();
507 // Create Match(es) and Set them in the FlowBuilder Object
508 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
511 // Create the OF Actions and Instructions
512 InstructionBuilder ib = new InstructionBuilder();
513 InstructionsBuilder isb = new InstructionsBuilder();
515 // Instructions List Stores Individual Instructions
516 List<Instruction> instructions = Lists.newArrayList();
518 // Call the InstructionBuilder Methods Containing Actions
519 InstructionUtils.createDropInstructions(ib);
521 ib.setKey(new InstructionKey(0));
522 instructions.add(ib.build());
524 // Add InstructionBuilder to the Instruction(s)Builder List
525 isb.setInstruction(instructions);
527 // Add InstructionsBuilder to FlowBuilder
528 flowBuilder.setInstructions(isb.build());
531 String flowId = "LocalTableMiss_"+segmentationId;
532 // Add Flow Attributes
533 flowBuilder.setId(new FlowId(flowId));
534 FlowKey key = new FlowKey(new FlowId(flowId));
535 flowBuilder.setStrict(true);
536 flowBuilder.setBarrier(false);
537 flowBuilder.setTableId(getTable());
538 flowBuilder.setKey(key);
539 flowBuilder.setPriority(8192);
540 flowBuilder.setFlowName(flowId);
541 flowBuilder.setHardTimeout(0);
542 flowBuilder.setIdleTimeout(0);
544 writeFlow(flowBuilder, nodeBuilder);
546 removeFlow(flowBuilder, nodeBuilder);
551 * (Table:1) Local Table Miss
552 * Match: Any Remaining Flows w/a VLAN ID
553 * Action: Drop w/ a low priority
554 * table=2,priority=8192,vlan_id=0x5 actions=drop
558 public void programLocalVlanTableMiss(Long dpidLong, String segmentationId, boolean write) {
560 String nodeName = OPENFLOW + dpidLong;
562 MatchBuilder matchBuilder = new MatchBuilder();
563 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
564 FlowBuilder flowBuilder = new FlowBuilder();
566 // Create Match(es) and Set them in the FlowBuilder Object
567 flowBuilder.setMatch(
568 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
571 // Create the OF Actions and Instructions
572 InstructionBuilder ib = new InstructionBuilder();
573 InstructionsBuilder isb = new InstructionsBuilder();
575 // Instructions List Stores Individual Instructions
576 List<Instruction> instructions = Lists.newArrayList();
578 // Call the InstructionBuilder Methods Containing Actions
579 InstructionUtils.createDropInstructions(ib);
581 ib.setKey(new InstructionKey(0));
582 instructions.add(ib.build());
584 // Add InstructionBuilder to the Instruction(s)Builder List
585 isb.setInstruction(instructions);
587 // Add InstructionsBuilder to FlowBuilder
588 flowBuilder.setInstructions(isb.build());
591 String flowId = "LocalTableMiss_"+segmentationId;
592 // Add Flow Attributes
593 flowBuilder.setId(new FlowId(flowId));
594 FlowKey key = new FlowKey(new FlowId(flowId));
595 flowBuilder.setStrict(true);
596 flowBuilder.setBarrier(false);
597 flowBuilder.setTableId(getTable());
598 flowBuilder.setKey(key);
599 flowBuilder.setPriority(8192);
600 flowBuilder.setFlowName(flowId);
601 flowBuilder.setHardTimeout(0);
602 flowBuilder.setIdleTimeout(0);
604 writeFlow(flowBuilder, nodeBuilder);
606 removeFlow(flowBuilder, nodeBuilder);
612 * (Table:1) Egress Tunnel Traffic
613 * Match: Destination Ethernet Addr and Local InPort
614 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
615 * table=1,tun_id=0x5,dl_dst=00:00:00:00:00:08 \
616 * actions=output:10,goto_table:2"
618 // TODO : Check on the reason why the original handleTunnelOut was chaining the traffic to table 2
620 public void programTunnelOut(Long dpidLong, String segmentationId, Long OFPortOut, String attachedMac, boolean write) {
622 String nodeName = OPENFLOW + dpidLong;
624 MatchBuilder matchBuilder = new MatchBuilder();
625 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
626 FlowBuilder flowBuilder = new FlowBuilder();
628 // Create the OF Match using MatchBuilder
629 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
630 flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
632 String flowId = "TunnelOut_"+segmentationId+"_"+OFPortOut+"_"+attachedMac;
633 // Add Flow Attributes
634 flowBuilder.setId(new FlowId(flowId));
635 FlowKey key = new FlowKey(new FlowId(flowId));
636 flowBuilder.setStrict(true);
637 flowBuilder.setBarrier(false);
638 flowBuilder.setTableId(getTable());
639 flowBuilder.setKey(key);
640 flowBuilder.setFlowName(flowId);
641 flowBuilder.setHardTimeout(0);
642 flowBuilder.setIdleTimeout(0);
645 // Instantiate the Builders for the OF Actions and Instructions
646 InstructionBuilder ib = new InstructionBuilder();
647 InstructionsBuilder isb = new InstructionsBuilder();
649 // Instructions List Stores Individual Instructions
650 List<Instruction> instructions = Lists.newArrayList();
652 // Set the Output Port/Iface
653 InstructionUtils.createOutputPortInstructions(ib, dpidLong, OFPortOut);
655 ib.setKey(new InstructionKey(1));
656 instructions.add(ib.build());
658 // Add InstructionBuilder to the Instruction(s)Builder List
659 isb.setInstruction(instructions);
661 // Add InstructionsBuilder to FlowBuilder
662 flowBuilder.setInstructions(isb.build());
664 writeFlow(flowBuilder, nodeBuilder);
666 removeFlow(flowBuilder, nodeBuilder);
671 * (Table:1) Egress VLAN Traffic
672 * Match: Destination Ethernet Addr and VLAN id
673 * Instruction: GOTO Table Table 2
674 * table=1,vlan_id=0x5,dl_dst=00:00:00:00:00:08 \
675 * actions= goto_table:2"
677 // TODO : Check on the reason why the original handleTunnelOut was chaining the traffic to table 2
679 public void programVlanOut(Long dpidLong, String segmentationId, Long ethPort, String attachedMac, boolean write) {
681 String nodeName = OPENFLOW + dpidLong;
683 MatchBuilder matchBuilder = new MatchBuilder();
684 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
685 FlowBuilder flowBuilder = new FlowBuilder();
687 // Create the OF Match using MatchBuilder
688 flowBuilder.setMatch(
689 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
690 flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(attachedMac), null).build());
692 String flowId = "VlanOut_"+segmentationId+"_"+ethPort+"_"+attachedMac;
693 // Add Flow Attributes
694 flowBuilder.setId(new FlowId(flowId));
695 FlowKey key = new FlowKey(new FlowId(flowId));
696 flowBuilder.setStrict(true);
697 flowBuilder.setBarrier(false);
698 flowBuilder.setTableId(getTable());
699 flowBuilder.setKey(key);
700 flowBuilder.setFlowName(flowId);
701 flowBuilder.setHardTimeout(0);
702 flowBuilder.setIdleTimeout(0);
705 // Instantiate the Builders for the OF Actions and Instructions
706 InstructionBuilder ib = new InstructionBuilder();
707 InstructionsBuilder isb = new InstructionsBuilder();
709 // Instructions List Stores Individual Instructions
710 List<Instruction> instructions = Lists.newArrayList();
711 InstructionUtils.createOutputPortInstructions(ib, dpidLong, ethPort);
713 ib.setKey(new InstructionKey(1));
714 instructions.add(ib.build());
716 // Add InstructionBuilder to the Instruction(s)Builder List
717 isb.setInstruction(instructions);
719 // Add InstructionsBuilder to FlowBuilder
720 flowBuilder.setInstructions(isb.build());
722 writeFlow(flowBuilder, nodeBuilder);
724 removeFlow(flowBuilder, nodeBuilder);
729 * (Table:1) Egress Tunnel Traffic
730 * Match: Destination Ethernet Addr and Local InPort
731 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
732 * table=1,priority=16384,tun_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
733 * actions=output:10,output:11,goto_table:2
736 public void programTunnelFloodOut(Long dpidLong, String segmentationId, Long OFPortOut, boolean write) {
738 String nodeName = OPENFLOW + dpidLong;
740 MatchBuilder matchBuilder = new MatchBuilder();
741 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
742 FlowBuilder flowBuilder = new FlowBuilder();
744 // Create the OF Match using MatchBuilder
746 MatchUtils.addNxRegMatch(matchBuilder, new MatchUtils.RegMatch(ClassifierService.REG_FIELD, ClassifierService.REG_VALUE_FROM_LOCAL));
747 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
749 flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
750 new MacAddress("01:00:00:00:00:00")).build());
752 String flowId = "TunnelFloodOut_"+segmentationId;
753 // Add Flow Attributes
754 flowBuilder.setId(new FlowId(flowId));
755 FlowKey key = new FlowKey(new FlowId(flowId));
756 flowBuilder.setBarrier(true);
757 flowBuilder.setTableId(getTable());
758 flowBuilder.setKey(key);
759 flowBuilder.setPriority(16383); // FIXME: change it back to 16384 once bug 3005 is fixed.
760 flowBuilder.setFlowName(flowId);
761 flowBuilder.setHardTimeout(0);
762 flowBuilder.setIdleTimeout(0);
764 Flow flow = this.getFlow(flowBuilder, nodeBuilder);
765 // Instantiate the Builders for the OF Actions and Instructions
766 InstructionBuilder ib = new InstructionBuilder();
767 InstructionsBuilder isb = new InstructionsBuilder();
768 List<Instruction> instructions = Lists.newArrayList();
769 List<Instruction> existingInstructions = null;
771 Instructions ins = flow.getInstructions();
773 existingInstructions = ins.getInstruction();
778 // Set the Output Port/Iface
779 //createOutputGroupInstructions(nodeBuilder, ib, dpidLong, OFPortOut, existingInstructions);
780 createOutputPortInstructions(ib, dpidLong, OFPortOut, existingInstructions);
782 ib.setKey(new InstructionKey(0));
783 instructions.add(ib.build());
785 // Add InstructionBuilder to the Instruction(s)Builder List
786 isb.setInstruction(instructions);
788 // Add InstructionsBuilder to FlowBuilder
789 flowBuilder.setInstructions(isb.build());
791 writeFlow(flowBuilder, nodeBuilder);
793 /* remove port from action list */
794 boolean flowRemove = InstructionUtils.removeOutputPortFromInstructions(ib, dpidLong,
795 OFPortOut, existingInstructions);
797 /* if all port are removed, remove the flow too. */
798 removeFlow(flowBuilder, nodeBuilder);
800 /* Install instruction with new output port list*/
802 ib.setKey(new InstructionKey(0));
803 instructions.add(ib.build());
805 // Add InstructionBuilder to the Instruction(s)Builder List
806 isb.setInstruction(instructions);
808 // Add InstructionsBuilder to FlowBuilder
809 flowBuilder.setInstructions(isb.build());
810 writeFlow(flowBuilder, nodeBuilder);
816 * (Table:1) Egress VLAN Traffic
817 * Match: Destination Ethernet Addr and VLAN id
818 * Instruction: GOTO table 2 and Output port eth interface
819 * Example: table=1,priority=16384,vlan_id=0x5,dl_dst=ff:ff:ff:ff:ff:ff \
820 * actions=output:eth1,goto_table:2
824 public void programVlanFloodOut(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
826 String nodeName = OPENFLOW + dpidLong;
828 MatchBuilder matchBuilder = new MatchBuilder();
829 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
830 FlowBuilder flowBuilder = new FlowBuilder();
832 // Create the OF Match using MatchBuilder
834 flowBuilder.setMatch(
835 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
837 flowBuilder.setMatch(MatchUtils.createDestEthMatch(matchBuilder, new MacAddress("01:00:00:00:00:00"),
838 new MacAddress("01:00:00:00:00:00")).build());
840 String flowId = "VlanFloodOut_"+segmentationId+"_"+ethPort;
841 // Add Flow Attributes
842 flowBuilder.setId(new FlowId(flowId));
843 FlowKey key = new FlowKey(new FlowId(flowId));
844 flowBuilder.setBarrier(true);
845 flowBuilder.setTableId(getTable());
846 flowBuilder.setKey(key);
847 flowBuilder.setPriority(16384);
848 flowBuilder.setFlowName(flowId);
849 flowBuilder.setHardTimeout(0);
850 flowBuilder.setIdleTimeout(0);
852 //ToDo: Is there something to be done with result of the call to getFlow?
854 Flow flow = this.getFlow(flowBuilder, nodeBuilder);
855 // Instantiate the Builders for the OF Actions and Instructions
856 InstructionBuilder ib = new InstructionBuilder();
857 InstructionsBuilder isb = new InstructionsBuilder();
858 List<Instruction> instructions = Lists.newArrayList();
861 // Set the Output Port/Iface
862 InstructionUtils.createOutputPortInstructions(ib, dpidLong, ethPort);
864 ib.setKey(new InstructionKey(0));
865 instructions.add(ib.build());
867 // Add InstructionBuilder to the Instruction(s)Builder List
868 isb.setInstruction(instructions);
870 // Add InstructionsBuilder to FlowBuilder
871 flowBuilder.setInstructions(isb.build());
873 writeFlow(flowBuilder, nodeBuilder);
875 removeFlow(flowBuilder, nodeBuilder);
880 * (Table:1) Table Drain w/ Catch All
882 * Action: GOTO Local Table (10)
883 * table=2,priority=8192,tun_id=0x5 actions=drop
886 public void programTunnelMiss(Long dpidLong, String segmentationId, boolean write) {
888 String nodeName = OPENFLOW + dpidLong;
890 MatchBuilder matchBuilder = new MatchBuilder();
891 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
892 FlowBuilder flowBuilder = new FlowBuilder();
894 // Create Match(es) and Set them in the FlowBuilder Object
895 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId)).build());
898 // Create the OF Actions and Instructions
899 InstructionsBuilder isb = new InstructionsBuilder();
901 // Instructions List Stores Individual Instructions
902 List<Instruction> instructions = Lists.newArrayList();
904 // Call the InstructionBuilder Methods Containing Actions
905 InstructionBuilder ib = this.getMutablePipelineInstructionBuilder();
907 ib.setKey(new InstructionKey(0));
908 instructions.add(ib.build());
910 // Add InstructionBuilder to the Instruction(s)Builder List
911 isb.setInstruction(instructions);
913 // Add InstructionsBuilder to FlowBuilder
914 flowBuilder.setInstructions(isb.build());
917 String flowId = "TunnelMiss_"+segmentationId;
918 // Add Flow Attributes
919 flowBuilder.setId(new FlowId(flowId));
920 FlowKey key = new FlowKey(new FlowId(flowId));
921 flowBuilder.setStrict(true);
922 flowBuilder.setBarrier(false);
923 flowBuilder.setTableId(getTable());
924 flowBuilder.setKey(key);
925 flowBuilder.setPriority(8192);
926 flowBuilder.setFlowName(flowId);
927 flowBuilder.setHardTimeout(0);
928 flowBuilder.setIdleTimeout(0);
930 writeFlow(flowBuilder, nodeBuilder);
932 removeFlow(flowBuilder, nodeBuilder);
937 * (Table:1) Table Drain w/ Catch All
939 * Action: Output port eth interface
940 * table=1,priority=8192,vlan_id=0x5 actions= output port:eth1
941 * table=110,priority=8192,dl_vlan=2001 actions=output:2
945 public void programVlanMiss(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
947 String nodeName = OPENFLOW + dpidLong;
949 MatchBuilder matchBuilder = new MatchBuilder();
950 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
951 FlowBuilder flowBuilder = new FlowBuilder();
953 // Create Match(es) and Set them in the FlowBuilder Object
954 flowBuilder.setMatch(
955 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
958 // Create the OF Actions and Instructions
959 InstructionBuilder ib = new InstructionBuilder();
960 InstructionsBuilder isb = new InstructionsBuilder();
962 // Instructions List Stores Individual Instructions
963 List<Instruction> instructions = Lists.newArrayList();
965 // Set the Output Port/Iface
966 InstructionUtils.createOutputPortInstructions(ib, dpidLong, ethPort);
968 ib.setKey(new InstructionKey(1));
969 instructions.add(ib.build());
971 // Add InstructionBuilder to the Instruction(s)Builder List
972 isb.setInstruction(instructions);
974 // Add InstructionsBuilder to FlowBuilder
975 flowBuilder.setInstructions(isb.build());
978 String flowId = "VlanMiss_"+segmentationId;
979 // Add Flow Attributes
980 flowBuilder.setId(new FlowId(flowId));
981 FlowKey key = new FlowKey(new FlowId(flowId));
982 flowBuilder.setStrict(true);
983 flowBuilder.setBarrier(false);
984 flowBuilder.setTableId(getTable());
985 flowBuilder.setKey(key);
986 flowBuilder.setPriority(8192);
987 flowBuilder.setFlowName(flowId);
988 flowBuilder.setHardTimeout(0);
989 flowBuilder.setIdleTimeout(0);
991 writeFlow(flowBuilder, nodeBuilder);
993 removeFlow(flowBuilder, nodeBuilder);
998 * Create Output Port Group Instruction
1000 * @param ib Map InstructionBuilder without any instructions
1001 * @param dpidLong Long the datapath ID of a switch/node
1002 * @param port Long representing a port on a switch/node
1003 * @return ib InstructionBuilder Map with instructions
1005 protected InstructionBuilder createOutputPortInstructions(InstructionBuilder ib,
1006 Long dpidLong, Long port ,
1007 List<Instruction> instructions) {
1008 NodeConnectorId ncid = new NodeConnectorId(OPENFLOW + dpidLong + ":" + port);
1009 LOG.debug("createOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}", dpidLong, port, instructions);
1011 List<Action> actionList = Lists.newArrayList();
1012 ActionBuilder ab = new ActionBuilder();
1014 List<Action> existingActions;
1015 if (instructions != null && instructions.size() > 0) {
1017 * First instruction is the one containing the output ports.
1018 * So, only extract the actions from that.
1020 Instruction in = instructions.get(0);
1021 if (in.getInstruction() instanceof ApplyActionsCase) {
1022 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
1023 // Only include output actions
1024 for (Action action : existingActions) {
1025 if (action.getAction() instanceof OutputActionCase) {
1026 actionList.add(action);
1031 /* Create output action for this port*/
1032 OutputActionBuilder oab = new OutputActionBuilder();
1033 oab.setOutputNodeConnector(ncid);
1034 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
1035 boolean addNew = true;
1037 /* Find the group action and get the group */
1038 for (Action action : actionList) {
1039 OutputActionCase opAction = (OutputActionCase)action.getAction();
1040 /* If output port action already in the action list of one of the buckets, skip */
1041 if (opAction.getOutputAction().getOutputNodeConnector().equals(new Uri(ncid))) {
1047 ab.setOrder(actionList.size());
1048 ab.setKey(new ActionKey(actionList.size()));
1049 actionList.add(ab.build());
1051 // Create an Apply Action
1052 ApplyActionsBuilder aab = new ApplyActionsBuilder();
1053 aab.setAction(actionList);
1054 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1055 LOG.debug("createOutputPortInstructions() : applyAction {}", aab.build());
1060 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
1061 super.setDependencies(bundleContext.getServiceReference(L2ForwardingProvider.class.getName()), this);
1065 public void setDependencies(Object impl) {