2 * Copyright (C) 2014 SDN Hub, LLC.
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 : Srini Seetharaman, Madhu Venugopal
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
12 import java.math.BigInteger;
13 import java.util.List;
16 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerConfiguration;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerConfiguration.LoadBalancerPoolMember;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerProvider;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
25 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
26 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
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.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.OfjNxHashFields;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.OfjNxMpAlgorithm;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 import com.google.common.collect.Lists;
59 public class LoadBalancerService extends AbstractServiceInstance implements LoadBalancerProvider {
61 private static final Logger logger = LoggerFactory.getLogger(LoadBalancerProvider.class);
62 private static final int DEFAULT_FLOW_PRIORITY = 32768;
63 private static final Long FIRST_PASS_REGA_MATCH_VALUE = 0L;
64 private static final Long SECOND_PASS_REGA_MATCH_VALUE = 1L;
66 private static final Class<? extends NxmNxReg> REG_FIELD_A = NxmNxReg1.class;
67 private static final Class<? extends NxmNxReg> REG_FIELD_B = NxmNxReg2.class;
69 public LoadBalancerService() {
70 super(Service.LOAD_BALANCER);
73 public LoadBalancerService(Service service) {
78 * When this method is called, we do the following for minimizing flow updates:
79 * 1. Overwrite the solo multipath rule that applies to all members
80 * 2. Append second pass rule for the header rewriting specific to this member
81 * 3. Append reverse rules specific to this member
84 public Status programLoadBalancerPoolMemberRules(Node node,
85 LoadBalancerConfiguration lbConfig, LoadBalancerPoolMember member,
86 org.opendaylight.ovsdb.openstack.netvirt.api.Action action) {
87 if (lbConfig == null || member == null) {
88 logger.error("Null value for LB config {} or Member {}", lbConfig, member);
89 return new Status(StatusCode.BADREQUEST);
91 if (!lbConfig.isValid()) {
92 logger.error("LB config is invalid: {}", lbConfig);
93 return new Status(StatusCode.BADREQUEST);
95 logger.debug("Performing {} rules for member {} with index {} on LB with VIP {} and total members {}",
96 action, member.getIP(), member.getIndex(), lbConfig.getVip(), lbConfig.getMembers().size());
98 NodeBuilder nodeBuilder = new NodeBuilder();
99 nodeBuilder.setId(new NodeId(Constants.OPENFLOW_NODE_PREFIX + node.getNodeId().getValue()));
100 nodeBuilder.setKey(new NodeKey(nodeBuilder.getId()));
102 //Update the multipath rule
103 manageLoadBalancerVIPRulesFirstPass(nodeBuilder, lbConfig, true);
105 if (action.equals(org.opendaylight.ovsdb.openstack.netvirt.api.Action.ADD)) {
106 manageLoadBalancerMemberVIPRulesSecondPass(nodeBuilder, lbConfig, member, true);
107 manageLoadBalancerMemberReverseRules(nodeBuilder, lbConfig, member, true);
108 return new Status(StatusCode.SUCCESS);
110 /* TODO: Delete single member.
111 * For now, removing a member requires deleting the full LB instance and re-adding
113 return new Status(StatusCode.NOTIMPLEMENTED);
117 * When this method is called, we perform the following:
118 * 1. Write the solo multipath rule that applies to all members
119 * 2. Append second pass rules for the header rewriting for all members
120 * 3. Append reverse rules for all the members, specific to the protocol/port
123 public Status programLoadBalancerRules(Node node, LoadBalancerConfiguration lbConfig,
124 org.opendaylight.ovsdb.openstack.netvirt.api.Action action) {
125 if (lbConfig == null) {
126 logger.error("LB config is invalid: {}", lbConfig);
127 return new Status(StatusCode.BADREQUEST);
129 if (!lbConfig.isValid()) {
130 logger.error("LB config is invalid: {}", lbConfig);
131 return new Status(StatusCode.BADREQUEST);
133 logger.debug("Performing {} rules for VIP {} and {} members", action, lbConfig.getVip(), lbConfig.getMembers().size());
135 NodeBuilder nodeBuilder = new NodeBuilder();
136 nodeBuilder.setId(new NodeId(Constants.OPENFLOW_NODE_PREFIX + node.getNodeId().getValue()));
137 nodeBuilder.setKey(new NodeKey(nodeBuilder.getId()));
139 if (action.equals(org.opendaylight.ovsdb.openstack.netvirt.api.Action.ADD)) {
140 manageLoadBalancerVIPRulesFirstPass(nodeBuilder, lbConfig, true);
141 manageLoadBalancerVIPRulesSecondPass(nodeBuilder, lbConfig, true);
142 manageLoadBalancerReverseRules(nodeBuilder, lbConfig, true);
143 return new Status(StatusCode.SUCCESS);
145 else if (action.equals(org.opendaylight.ovsdb.openstack.netvirt.api.Action.DELETE)) {
146 manageLoadBalancerVIPRulesFirstPass(nodeBuilder, lbConfig, false);
147 manageLoadBalancerVIPRulesSecondPass(nodeBuilder, lbConfig, false);
148 manageLoadBalancerReverseRules(nodeBuilder, lbConfig, false);
149 return new Status(StatusCode.SUCCESS);
152 return new Status(StatusCode.NOTIMPLEMENTED);
156 * Method to insert/remove default rule for traffic destined to the VIP and no
157 * server selection performed yet
158 * @param nodeBuilder NodeBuilder
159 * @param lbConfig LoadBalancerConfiguration
160 * @param write Boolean to indicate of the flow is to be inserted or removed
162 private void manageLoadBalancerVIPRulesFirstPass(NodeBuilder nodeBuilder, LoadBalancerConfiguration lbConfig, boolean write) {
163 MatchBuilder matchBuilder = new MatchBuilder();
164 FlowBuilder flowBuilder = new FlowBuilder();
166 // Match Tunnel-ID, VIP, and Reg0==0
167 if (lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
168 lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE))
169 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(lbConfig.getProviderSegmentationId()));
170 else if (lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN))
171 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(lbConfig.getProviderSegmentationId())), true);
173 return; //Should not get here. TODO: Other types
175 MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(lbConfig.getVip()));
176 MatchUtils.addNxRegMatch(matchBuilder, new MatchUtils.RegMatch(REG_FIELD_A, FIRST_PASS_REGA_MATCH_VALUE));
178 String flowId = "LOADBALANCER_FORWARD_FLOW1_" + lbConfig.getVip();
179 flowBuilder.setId(new FlowId(flowId));
180 FlowKey key = new FlowKey(new FlowId(flowId));
181 flowBuilder.setMatch(matchBuilder.build());
182 flowBuilder.setPriority(DEFAULT_FLOW_PRIORITY);
183 flowBuilder.setBarrier(true);
184 flowBuilder.setTableId(this.getTable());
185 flowBuilder.setKey(key);
186 flowBuilder.setFlowName(flowId);
187 flowBuilder.setHardTimeout(0);
188 flowBuilder.setIdleTimeout(0);
191 // Create the OF Actions and Instructions
192 InstructionsBuilder isb = new InstructionsBuilder();
194 // Instructions List Stores Individual Instructions
195 List<Instruction> instructions = Lists.newArrayList();
197 List<Action> actionList = Lists.newArrayList();
199 ActionBuilder ab = new ActionBuilder();
200 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD_A).build(),
201 BigInteger.valueOf(SECOND_PASS_REGA_MATCH_VALUE)));
203 ab.setKey(new ActionKey(0));
204 actionList.add(ab.build());
206 ab = new ActionBuilder();
207 ab.setAction(ActionUtils.nxMultipathAction(OfjNxHashFields.NXHASHFIELDSSYMMETRICL4,
208 0, OfjNxMpAlgorithm.NXMPALGMODULON,
209 lbConfig.getMembers().size()-1, //By Nicira-Ext spec, this field is max_link minus 1
210 0L, new DstNxRegCaseBuilder().setNxReg(REG_FIELD_B).build(),
213 ab.setKey(new ActionKey(1));
214 actionList.add(ab.build());
216 ab = new ActionBuilder();
217 ab.setAction(ActionUtils.nxResubmitAction(null, this.getTable()));
219 ab.setKey(new ActionKey(2));
220 actionList.add(ab.build());
222 // Create an Apply Action
223 ApplyActionsBuilder aab = new ApplyActionsBuilder();
224 aab.setAction(actionList);
225 InstructionBuilder ib = new InstructionBuilder();
226 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
228 // Call the InstructionBuilder Methods Containing Actions
230 ib.setKey(new InstructionKey(0));
231 instructions.add(ib.build());
233 // Add InstructionBuilder to the Instruction(s)Builder List
234 isb.setInstruction(instructions);
236 // Add InstructionsBuilder to FlowBuilder
237 flowBuilder.setInstructions(isb.build());
239 writeFlow(flowBuilder, nodeBuilder);
242 removeFlow(flowBuilder, nodeBuilder);
247 * Method to program each rule that matches on Reg0 and Reg1 to insert appropriate header rewriting
248 * rules for all members. This function calls manageLoadBalancerMemberVIPRulesSecondPass in turn.
249 * @param nodeBuilder Node to insert rule to
250 * @param lbConfig Configuration for this LoadBalancer instance
251 * @param write Boolean to indicate of the flow is to be inserted or removed
253 private void manageLoadBalancerVIPRulesSecondPass(NodeBuilder nodeBuilder, LoadBalancerConfiguration lbConfig, boolean write) {
254 for(Map.Entry<String, LoadBalancerPoolMember> entry : lbConfig.getMembers().entrySet()){
255 manageLoadBalancerMemberVIPRulesSecondPass(nodeBuilder, lbConfig, entry.getValue(), write);
259 private void manageLoadBalancerMemberVIPRulesSecondPass(NodeBuilder nodeBuilder, LoadBalancerConfiguration lbConfig, LoadBalancerPoolMember member, boolean write) {
260 String vip = lbConfig.getVip();
262 MatchBuilder matchBuilder = new MatchBuilder();
263 FlowBuilder flowBuilder = new FlowBuilder();
265 // Match Tunnel-ID, VIP, Reg0==1 and Reg1==Index of member
266 if (lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
267 lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE))
268 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(lbConfig.getProviderSegmentationId()));
269 else if (lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN))
270 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(lbConfig.getProviderSegmentationId())), true);
272 return; //Should not get here. TODO: Other types
274 MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(vip));
275 MatchUtils.addNxRegMatch(matchBuilder, new MatchUtils.RegMatch(REG_FIELD_A, SECOND_PASS_REGA_MATCH_VALUE),
276 new MatchUtils.RegMatch(REG_FIELD_B, (long)member.getIndex()));
278 String flowId = "LOADBALANCER_FORWARD_FLOW2_" + vip + "_" + member.getIP();
279 flowBuilder.setId(new FlowId(flowId));
280 FlowKey key = new FlowKey(new FlowId(flowId));
281 flowBuilder.setMatch(matchBuilder.build());
282 flowBuilder.setPriority(DEFAULT_FLOW_PRIORITY+1);
283 flowBuilder.setBarrier(true);
284 flowBuilder.setTableId(this.getTable());
285 flowBuilder.setKey(key);
286 flowBuilder.setFlowName(flowId);
287 flowBuilder.setHardTimeout(0);
288 flowBuilder.setIdleTimeout(0);
291 // Create the OF Actions and Instructions
292 InstructionsBuilder isb = new InstructionsBuilder();
294 // Instructions List Stores Individual Instructions
295 List<Instruction> instructions = Lists.newArrayList();
297 List<Action> actionList = Lists.newArrayList();
298 ActionBuilder ab = new ActionBuilder();
299 ab.setAction(ActionUtils.setDlDstAction(new MacAddress(member.getMAC())));
301 ab.setKey(new ActionKey(0));
302 actionList.add(ab.build());
304 ab = new ActionBuilder();
305 Ipv4Builder ipb = new Ipv4Builder().setIpv4Address(MatchUtils.iPv4PrefixFromIPv4Address(member.getIP()));
306 ab.setAction(ActionUtils.setNwDstAction(ipb.build()));
308 ab.setKey(new ActionKey(1));
309 actionList.add(ab.build());
311 // Create an Apply Action
312 ApplyActionsBuilder aab = new ApplyActionsBuilder();
313 aab.setAction(actionList);
315 // Call the InstructionBuilder Methods Containing Actions
316 InstructionBuilder ib = new InstructionBuilder();
317 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
319 ib.setKey(new InstructionKey(0));
320 instructions.add(ib.build());
322 // Call the InstructionBuilder Methods Containing Actions
323 ib = this.getMutablePipelineInstructionBuilder();
325 ib.setKey(new InstructionKey(1));
326 instructions.add(ib.build());
328 // Add InstructionBuilder to the Instruction(s)Builder List
329 isb.setInstruction(instructions);
331 // Add InstructionsBuilder to FlowBuilder
332 flowBuilder.setInstructions(isb.build());
334 writeFlow(flowBuilder, nodeBuilder);
337 removeFlow(flowBuilder, nodeBuilder);
342 * Method to program all reverse rules that matches member {IP, Protocol, Port} for all members.
343 * This function calls manageLoadBalancerMemberReverseRules in turn.
344 * @param nodeBuilder Node to insert rule to
345 * @param lbConfig Configuration for this LoadBalancer instance
347 private void manageLoadBalancerReverseRules(NodeBuilder nodeBuilder, LoadBalancerConfiguration lbConfig, boolean write) {
348 for(Map.Entry<String, LoadBalancerPoolMember> entry : lbConfig.getMembers().entrySet()){
349 manageLoadBalancerMemberReverseRules(nodeBuilder, lbConfig, entry.getValue(), write);
353 private void manageLoadBalancerMemberReverseRules(NodeBuilder nodeBuilder, LoadBalancerConfiguration lbConfig,
354 LoadBalancerPoolMember member, boolean write) {
356 String vip = lbConfig.getVip();
357 String vmac = lbConfig.getVmac();
359 MatchBuilder matchBuilder = new MatchBuilder();
360 FlowBuilder flowBuilder = new FlowBuilder();
362 // Match Tunnel-ID, MemberIP, and Protocol/Port
363 if (lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
364 lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE))
365 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(lbConfig.getProviderSegmentationId()));
366 else if (lbConfig.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN))
367 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(lbConfig.getProviderSegmentationId())), true);
369 return; //Should not get here. TODO: Other types
371 MatchUtils.createSrcL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(member.getIP()));
372 MatchUtils.createSetSrcTcpMatch(matchBuilder, new PortNumber(member.getPort()));
374 String flowId = "LOADBALANCER_REVERSE_FLOW_" + vip + "_" + member.getIP();
375 flowBuilder.setId(new FlowId(flowId));
376 FlowKey key = new FlowKey(new FlowId(flowId));
377 flowBuilder.setMatch(matchBuilder.build());
378 flowBuilder.setPriority(DEFAULT_FLOW_PRIORITY);
379 flowBuilder.setBarrier(true);
380 flowBuilder.setTableId(this.getTable());
381 flowBuilder.setKey(key);
382 flowBuilder.setFlowName(flowId);
383 flowBuilder.setHardTimeout(0);
384 flowBuilder.setIdleTimeout(0);
387 // Create the OF Actions and Instructions
388 InstructionsBuilder isb = new InstructionsBuilder();
390 // Instructions List Stores Individual Instructions
391 List<Instruction> instructions = Lists.newArrayList();
393 List<Action> actionList = Lists.newArrayList();
394 ActionBuilder ab = new ActionBuilder();
395 Ipv4Builder ipb = new Ipv4Builder().setIpv4Address(MatchUtils.iPv4PrefixFromIPv4Address(vip));
396 ab.setAction(ActionUtils.setNwSrcAction(ipb.build()));
398 ab.setKey(new ActionKey(0));
399 actionList.add(ab.build());
401 /* If a dummy MAC is assigned to the VIP, we use that as the
402 * source MAC for the reverse traffic.
405 ab = new ActionBuilder();
406 ab.setAction(ActionUtils.setDlDstAction(new MacAddress(vmac)));
408 ab.setKey(new ActionKey(1));
409 actionList.add(ab.build());
412 // Create an Apply Action
413 ApplyActionsBuilder aab = new ApplyActionsBuilder();
414 aab.setAction(actionList);
416 // Call the InstructionBuilder Methods Containing Actions
417 InstructionBuilder ib = new InstructionBuilder();
418 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
420 ib.setKey(new InstructionKey(0));
421 instructions.add(ib.build());
423 // Call the InstructionBuilder Methods Containing Actions
424 ib = this.getMutablePipelineInstructionBuilder();
426 ib.setKey(new InstructionKey(1));
427 instructions.add(ib.build());
429 // Add InstructionBuilder to the Instruction(s)Builder List
430 isb.setInstruction(instructions);
432 // Add InstructionsBuilder to FlowBuilder
433 flowBuilder.setInstructions(isb.build());
435 writeFlow(flowBuilder, nodeBuilder);
438 removeFlow(flowBuilder, nodeBuilder);