blob: 9b68fa8978ea99792cb80866b6c768bc6f0466d7 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.openflow.drivers;
tom7ef8ff92014-09-17 13:08:06 -070017
Jonathan Hart47f2dde2015-01-14 17:45:06 -080018import java.io.IOException;
19import java.util.ArrayList;
20import java.util.Collections;
21import java.util.List;
22import java.util.Map;
23import java.util.concurrent.ConcurrentHashMap;
24import java.util.concurrent.atomic.AtomicBoolean;
25
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.openflow.controller.Dpid;
27import org.onosproject.openflow.controller.RoleState;
28import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch;
29import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted;
30import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted;
31import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted;
tom7ef8ff92014-09-17 13:08:06 -070032import org.projectfloodlight.openflow.protocol.OFAsyncGetReply;
33import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
34import org.projectfloodlight.openflow.protocol.OFBucket;
35import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
36import org.projectfloodlight.openflow.protocol.OFFactory;
37import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
38import org.projectfloodlight.openflow.protocol.OFGroupFeaturesStatsReply;
39import org.projectfloodlight.openflow.protocol.OFGroupType;
40import org.projectfloodlight.openflow.protocol.OFMatchV3;
41import org.projectfloodlight.openflow.protocol.OFMessage;
42import org.projectfloodlight.openflow.protocol.OFOxmList;
43import org.projectfloodlight.openflow.protocol.OFPortDesc;
44import org.projectfloodlight.openflow.protocol.OFStatsReply;
45import org.projectfloodlight.openflow.protocol.action.OFAction;
46import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
47import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst;
48import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthSrc;
49import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthType;
50import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
51import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked;
52import org.projectfloodlight.openflow.protocol.oxm.OFOxmMetadataMasked;
53import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
54import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
55import org.projectfloodlight.openflow.types.EthType;
56import org.projectfloodlight.openflow.types.IPv4Address;
57import org.projectfloodlight.openflow.types.MacAddress;
58import org.projectfloodlight.openflow.types.OFBufferId;
59import org.projectfloodlight.openflow.types.OFGroup;
60import org.projectfloodlight.openflow.types.OFMetadata;
61import org.projectfloodlight.openflow.types.OFPort;
62import org.projectfloodlight.openflow.types.OFVlanVidMatch;
63import org.projectfloodlight.openflow.types.TableId;
64import org.projectfloodlight.openflow.types.U32;
65import org.projectfloodlight.openflow.types.U64;
66import org.projectfloodlight.openflow.util.HexString;
tom7ef8ff92014-09-17 13:08:06 -070067
68/**
69 * OFDescriptionStatistics Vendor (Manufacturer Desc.): Stanford University,
70 * Ericsson Research and CPqD Research. Make (Hardware Desc.) : OpenFlow 1.3
71 * Reference Userspace Switch Model (Datapath Desc.) : None Software : Serial :
72 * None
73 */
74public class OFSwitchImplCPqD13 extends AbstractOpenFlowSwitch {
75
tom7ef8ff92014-09-17 13:08:06 -070076 private static final int VLAN_ID_OFFSET = 16;
77 private final AtomicBoolean driverHandshakeComplete;
78 private OFFactory factory;
79 private static final int OFPCML_NO_BUFFER = 0xffff;
80 // Configuration of asynch messages to controller. We need different
81 // asynch messages depending on role-equal or role-master.
82 // We don't want to get anything if we are slave.
83 private static final long SET_FLOW_REMOVED_MASK_MASTER = 0xf;
84 private static final long SET_PACKET_IN_MASK_MASTER = 0x7;
85 private static final long SET_PORT_STATUS_MASK_MASTER = 0x7;
86 private static final long SET_FLOW_REMOVED_MASK_EQUAL = 0x0;
87 private static final long SET_PACKET_IN_MASK_EQUAL = 0x0;
88 private static final long SET_PORT_STATUS_MASK_EQUAL = 0x7;
89 private static final long SET_ALL_SLAVE = 0x0;
90
91 private static final long TEST_FLOW_REMOVED_MASK = 0xf;
92 private static final long TEST_PACKET_IN_MASK = 0x7;
93 private static final long TEST_PORT_STATUS_MASK = 0x7;
94 private long barrierXidToWaitFor = -1;
95
96 private static final int TABLE_VLAN = 0;
97 private static final int TABLE_TMAC = 1;
98 private static final int TABLE_IPV4_UNICAST = 2;
99 private static final int TABLE_MPLS = 3;
100 private static final int TABLE_META = 4;
101 private static final int TABLE_ACL = 5;
102
103 private static final short MAX_PRIORITY = (short) 0xffff;
104 private static final short SLASH_24_PRIORITY = (short) 0xfff0;
105 private static final short MIN_PRIORITY = 0x0;
106 private static final U64 METADATA_MASK = U64.of(Long.MAX_VALUE << 1 | 0x1);
107
108 private final Map<Integer, OFGroup> l2groups;
109
Jonathan Hart47f2dde2015-01-14 17:45:06 -0800110 public OFSwitchImplCPqD13(Dpid dpid, OFDescStatsReply desc) {
tom7ef8ff92014-09-17 13:08:06 -0700111 super(dpid);
112 driverHandshakeComplete = new AtomicBoolean(false);
113 l2groups = new ConcurrentHashMap<Integer, OFGroup>();
114 setSwitchDescription(desc);
tom7ef8ff92014-09-17 13:08:06 -0700115 }
116
117 /* (non-Javadoc)
118 * @see java.lang.Object#toString()
119 */
120 @Override
121 public String toString() {
122 return "OFSwitchImplCPqD13 [" + ((channel != null)
123 ? channel.getRemoteAddress() : "?")
124 + " DPID[" + ((this.getStringId() != null) ? this.getStringId() : "?") + "]]";
125 }
126
127 @Override
128 public void startDriverHandshake() {
129 log.debug("Starting driver handshake for sw {}", getStringId());
130 if (startDriverHandshakeCalled) {
131 throw new SwitchDriverSubHandshakeAlreadyStarted();
132 }
133 startDriverHandshakeCalled = true;
134 factory = this.factory();
Jonathan Hart47f2dde2015-01-14 17:45:06 -0800135
tom7ef8ff92014-09-17 13:08:06 -0700136 sendBarrier(true);
137 }
138
139 @Override
140 public boolean isDriverHandshakeComplete() {
141 if (!startDriverHandshakeCalled) {
142 throw new SwitchDriverSubHandshakeNotStarted();
143 }
144 return driverHandshakeComplete.get();
145 }
146
147 @Override
148 public void processDriverHandshakeMessage(OFMessage m) {
Ray Milkey241b96a2014-11-17 13:08:20 -0800149 if (!startDriverHandshakeCalled) {
tom7ef8ff92014-09-17 13:08:06 -0700150 throw new SwitchDriverSubHandshakeNotStarted();
151 }
152 if (driverHandshakeComplete.get()) {
153 throw new SwitchDriverSubHandshakeCompleted(m);
154 }
155
156 switch (m.getType()) {
157 case BARRIER_REPLY:
158 if (m.getXid() == barrierXidToWaitFor) {
159 driverHandshakeComplete.set(true);
160 }
161 break;
162
163 case ERROR:
164 log.error("Switch {} Error {}", getStringId(), m);
165 break;
166
167 case GET_ASYNC_REPLY:
168 OFAsyncGetReply asrep = (OFAsyncGetReply) m;
169 decodeAsyncGetReply(asrep);
170 break;
171 case STATS_REPLY:
172 processStatsReply((OFStatsReply) m);
173 break;
174 case PACKET_IN:
175 case PORT_STATUS:
176 case QUEUE_GET_CONFIG_REPLY:
177 case ROLE_REPLY:
178 case FEATURES_REPLY:
179 case FLOW_REMOVED:
180 break;
181
182 default:
183 log.debug("Received message {} during switch-driver subhandshake "
184 + "from switch {} ... Ignoring message", m, getStringId());
185
186 }
187 }
188
189 private void configureSwitch() throws IOException {
190 // setAsyncConfig();
191 // getTableFeatures();
192 sendGroupFeaturesRequest();
193 setL2Groups();
194 sendBarrier(false);
195 setL3Groups();
196 setL25Groups();
197 sendGroupDescRequest();
198 populateTableVlan();
199 populateTableTMac();
200 populateIpTable();
201 populateMplsTable();
202 populateTableMissEntry(TABLE_ACL, false, false, false, -1);
203 sendBarrier(true);
204 }
205
206 private void setAsyncConfig() throws IOException {
207 List<OFMessage> msglist = new ArrayList<OFMessage>(3);
208 OFMessage setAC = null;
209
210 if (role == RoleState.MASTER) {
211 setAC = factory.buildAsyncSet()
212 .setFlowRemovedMaskEqualMaster(SET_FLOW_REMOVED_MASK_MASTER)
213 .setPacketInMaskEqualMaster(SET_PACKET_IN_MASK_MASTER)
214 .setPortStatusMaskEqualMaster(SET_PORT_STATUS_MASK_MASTER)
215 .setFlowRemovedMaskSlave(SET_ALL_SLAVE)
216 .setPacketInMaskSlave(SET_ALL_SLAVE)
217 .setPortStatusMaskSlave(SET_ALL_SLAVE)
218 .setXid(getNextTransactionId())
219 .build();
220 } else if (role == RoleState.EQUAL) {
221 setAC = factory.buildAsyncSet()
222 .setFlowRemovedMaskEqualMaster(SET_FLOW_REMOVED_MASK_EQUAL)
223 .setPacketInMaskEqualMaster(SET_PACKET_IN_MASK_EQUAL)
224 .setPortStatusMaskEqualMaster(SET_PORT_STATUS_MASK_EQUAL)
225 .setFlowRemovedMaskSlave(SET_ALL_SLAVE)
226 .setPacketInMaskSlave(SET_ALL_SLAVE)
227 .setPortStatusMaskSlave(SET_ALL_SLAVE)
228 .setXid(getNextTransactionId())
229 .build();
230 }
231 msglist.add(setAC);
232
233 OFMessage br = factory.buildBarrierRequest()
234 .setXid(getNextTransactionId())
235 .build();
236 msglist.add(br);
237
238 OFMessage getAC = factory.buildAsyncGetRequest()
239 .setXid(getNextTransactionId())
240 .build();
241 msglist.add(getAC);
242
243 sendMsg(msglist);
244 }
245
246 private void decodeAsyncGetReply(OFAsyncGetReply rep) {
247 long frm = rep.getFlowRemovedMaskEqualMaster();
248 //long frs = rep.getFlowRemovedMaskSlave();
249 long pim = rep.getPacketInMaskEqualMaster();
250 //long pis = rep.getPacketInMaskSlave();
251 long psm = rep.getPortStatusMaskEqualMaster();
252 //long pss = rep.getPortStatusMaskSlave();
253
254 if (role == RoleState.MASTER || role == RoleState.EQUAL) { // should separate
255 log.info("FRM:{}", HexString.toHexString((frm & TEST_FLOW_REMOVED_MASK)));
256 log.info("PIM:{}", HexString.toHexString((pim & TEST_PACKET_IN_MASK)));
257 log.info("PSM:{}", HexString.toHexString((psm & TEST_PORT_STATUS_MASK)));
258 }
259
260 }
261
262 private void getTableFeatures() throws IOException {
263 OFMessage gtf = factory.buildTableFeaturesStatsRequest()
264 .setXid(getNextTransactionId())
265 .build();
266 sendMsg(gtf);
267 }
268
269 private void sendGroupFeaturesRequest() throws IOException {
270 OFMessage gfr = factory.buildGroupFeaturesStatsRequest()
271 .setXid(getNextTransactionId())
272 .build();
273 sendMsg(gfr);
274 }
275
276 private void sendGroupDescRequest() throws IOException {
277 OFMessage gdr = factory.buildGroupDescStatsRequest()
278 .setXid(getNextTransactionId())
279 .build();
280 sendMsg(gdr);
281 }
282
283 /*Create L2 interface groups for all physical ports
284 Naming convention followed is the same as OF-DPA spec
285 eg. port 1 with allowed vlan 10, is enveloped in group with id,
286 0x0 00a 0001, where the uppermost 4 bits identify an L2 interface,
287 the next 12 bits identify the vlan-id, and the lowermost 16 bits
288 identify the port number.*/
289 private void setL2Groups() throws IOException {
290 List<OFMessage> msglist = new ArrayList<OFMessage>();
291 for (OFPortDesc p : getPorts()) {
292 int pnum = p.getPortNo().getPortNumber();
293 int portVlan = getVlanConfig(pnum);
294 if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) {
295 OFGroup gl2 = OFGroup.of(pnum | (portVlan << VLAN_ID_OFFSET));
296 OFAction out = factory.actions().buildOutput()
297 .setPort(p.getPortNo()).build();
298 OFAction popVlan = factory.actions().popVlan();
299 List<OFAction> actions = new ArrayList<OFAction>();
300 actions.add(popVlan);
301 actions.add(out);
302 OFBucket bucket = factory.buildBucket()
303 .setActions(actions).build();
304 List<OFBucket> buckets = Collections.singletonList(bucket);
305 OFMessage gmAdd = factory.buildGroupAdd()
306 .setGroup(gl2)
307 .setBuckets(buckets)
308 .setGroupType(OFGroupType.INDIRECT)
309 .setXid(getNextTransactionId())
310 .build();
311 msglist.add(gmAdd);
312 l2groups.put(pnum, gl2);
313 }
314 }
315 log.debug("Creating {} L2 groups in sw {}", msglist.size(), getStringId());
316 sendMsg(msglist);
317 }
318
319 private int getVlanConfig(int portnum) {
320 int portVlan = 10 * portnum;
321 if ((getId() == 0x1 && portnum == 6) ||
322 (getId() == 0x2) ||
323 (getId() == 0x3 && portnum == 2)) {
324 portVlan = 192; // 0xc0
325 }
326 return portVlan;
327 }
328
329 private MacAddress getRouterMacAddr() {
330 if (getId() == 0x3) {
331 return MacAddress.of("00:00:07:07:07:80"); // router mac
332 }
333 if (getId() == 0x1) {
334 return MacAddress.of("00:00:01:01:01:80");
335 }
336 // switch 0x2
337 return MacAddress.of("00:00:02:02:02:80");
338 }
339
340 // only for ports connected to other routers
341 private OFAction getDestAction(int portnum) {
342 OFAction setDA = null;
343 MacAddress dAddr = null;
344 if (getId() == 0x1 && portnum == 6) { // connected to switch 2
345 dAddr = MacAddress.of("00:00:02:02:02:80");
346 }
347 if (getId() == 0x2) {
348 if (portnum == 1) { // connected to sw 1
349 dAddr = MacAddress.of("00:00:01:01:01:80");
350 } else if (portnum == 2) { // connected to sw 3
351 dAddr = MacAddress.of("00:00:07:07:07:80");
352 }
353 }
354 if (getId() == 0x3) {
355 if (portnum == 2) { // connected to switch 2
356 dAddr = MacAddress.of("00:00:02:02:02:80");
357 }
358 }
359
360 if (dAddr != null) {
361 OFOxmEthDst dstAddr = factory.oxms().ethDst(dAddr);
362 setDA = factory.actions().buildSetField()
363 .setField(dstAddr).build();
364 }
365 return setDA;
366 }
367
368 /*
369 * L3 groups are created for all router ports and they all point to corresponding
370 * L2 groups. Only the ports that connect to other routers will have the
371 * DA set.
372 */
373 private void setL3Groups() throws IOException {
374 List<OFMessage> msglist = new ArrayList<OFMessage>();
375 for (OFGroup gl2 : l2groups.values()) {
376 int gnum = gl2.getGroupNumber();
377 int portnum = gnum & 0x0000ffff;
378 int vlanid = ((gnum & 0x0fff0000) >> VLAN_ID_OFFSET);
379 MacAddress sAddr = getRouterMacAddr();
380
381 OFGroup gl3 = OFGroup.of(0x20000000 | portnum);
382 OFAction group = factory.actions().buildGroup()
383 .setGroup(gl2).build();
384 OFOxmEthSrc srcAddr = factory.oxms().ethSrc(sAddr);
385 OFAction setSA = factory.actions().buildSetField()
386 .setField(srcAddr).build();
387 OFOxmVlanVid vid = factory.oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanid));
388 OFAction setVlan = factory.actions().buildSetField()
389 .setField(vid).build();
390 OFAction decTtl = factory.actions().decNwTtl();
391
392 List<OFAction> actions = new ArrayList<OFAction>();
393 actions.add(decTtl); // decrement the IP TTL/do-checksum/check TTL
394 // and MTU
395 actions.add(setVlan); // set the vlan-id of the exit-port (and
396 // l2group)
397 actions.add(setSA); // set this routers mac address
398 // make L3Unicast group setDA for known (configured) ports
399 // that connect to other routers
400 OFAction setDA = getDestAction(portnum);
401 if (setDA != null) {
402 actions.add(setDA);
403 }
404 actions.add(group);
405
406 OFBucket bucket = factory.buildBucket()
407 .setActions(actions).build();
408 List<OFBucket> buckets = Collections.singletonList(bucket);
409 OFMessage gmAdd = factory.buildGroupAdd()
410 .setGroup(gl3)
411 .setBuckets(buckets)
412 .setGroupType(OFGroupType.INDIRECT)
413 .setXid(getNextTransactionId())
414 .build();
415 msglist.add(gmAdd);
416 }
417 sendMsg(msglist);
418 log.debug("Creating {} L3 groups in sw {}", msglist.size(), getStringId());
419 }
420
421 /*
422 * L2.5 or mpls-unicast groups are only created for those router ports
423 * connected to other router ports. They differ from the corresponding
424 * L3-unicast group only by the fact that they decrement the MPLS TTL
425 * instead of the IP ttl
426 */
427 private void setL25Groups() throws IOException {
428 List<OFMessage> msglist = new ArrayList<OFMessage>();
429 for (OFGroup gl2 : l2groups.values()) {
430 int gnum = gl2.getGroupNumber();
431 int portnum = gnum & 0x0000ffff;
432 int vlanid = ((gnum & 0x0fff0000) >> VLAN_ID_OFFSET);
433 MacAddress sAddr = getRouterMacAddr();
434 OFAction setDA = getDestAction(portnum);
435 // setDA will only be non-null for ports connected to routers
436 if (setDA != null) {
437 OFGroup gl3 = OFGroup.of(0xa0000000 | portnum); // different id
438 // for mpls
439 // group
440 OFAction group = factory.actions().buildGroup()
441 .setGroup(gl2).build();
442 OFOxmEthSrc srcAddr = factory.oxms().ethSrc(sAddr);
443 OFAction setSA = factory.actions().buildSetField()
444 .setField(srcAddr).build();
445 OFOxmVlanVid vid = factory.oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanid));
446 OFAction setVlan = factory.actions().buildSetField()
447 .setField(vid).build();
448 OFAction decMplsTtl = factory.actions().decMplsTtl();
449 List<OFAction> actions = new ArrayList<OFAction>();
450 actions.add(decMplsTtl); // decrement the MPLS
451 // TTL/do-checksum/check TTL and MTU
452 actions.add(setVlan); // set the vlan-id of the exit-port (and
453 // l2group)
454 actions.add(setSA); // set this routers mac address
455 actions.add(setDA);
456 actions.add(group);
457 OFBucket bucket = factory.buildBucket()
458 .setActions(actions).build();
459 List<OFBucket> buckets = Collections.singletonList(bucket);
460 OFMessage gmAdd = factory.buildGroupAdd()
461 .setGroup(gl3)
462 .setBuckets(buckets)
463 .setGroupType(OFGroupType.INDIRECT)
464 .setXid(getNextTransactionId())
465 .build();
466 msglist.add(gmAdd);
467 }
468 }
469 sendMsg(msglist);
470 log.debug("Creating {} MPLS groups in sw {}", msglist.size(), getStringId());
471 }
472
473 /* Using ECMP groups
474 *
475 * OFGroup group47 = OFGroup.of(47);
476 OFAction outgroup1 = factory.actions()
477 .buildGroup()
478 .setGroup(group61)
479 .build();
480 OFBucket buc47_1 = factory.buildBucket()
481 .setWeight(1)
482 .setActions(Collections.singletonList(outgroup1))
483 .build();
484 OFAction outgroup2 = factory.actions()
485 .buildGroup()
486 .setGroup(group62)
487 .build();
488 OFBucket buc47_2 = factory.buildBucket()
489 .setWeight(1)
490 .setActions(Collections.singletonList(outgroup2))
491 .build();
492 List<OFBucket> buckets47 = new ArrayList<OFBucket>();
493 buckets47.add(buc47_1);
494 buckets47.add(buc47_2);
495 OFMessage gmS12 = factory.buildGroupAdd()
496 .setGroup(group47)
497 .setBuckets(buckets47)
498 .setGroupType(OFGroupType.SELECT)
499 .setXid(getNextTransactionId())
500 .build();
501 write(gmS12, null); */
502
503 private void processStatsReply(OFStatsReply sr) {
504 switch (sr.getStatsType()) {
505 case AGGREGATE:
506 break;
507 case DESC:
508 break;
509 case EXPERIMENTER:
510 break;
511 case FLOW:
512 break;
513 case GROUP_DESC:
514 processGroupDesc((OFGroupDescStatsReply) sr);
515 break;
516 case GROUP_FEATURES:
517 processGroupFeatures((OFGroupFeaturesStatsReply) sr);
518 break;
519 case METER_CONFIG:
520 break;
521 case METER_FEATURES:
522 break;
523 case PORT_DESC:
524 break;
525 case TABLE_FEATURES:
526 break;
527 default:
528 break;
529
530 }
531 }
532
533 private void processGroupFeatures(OFGroupFeaturesStatsReply gfsr) {
534 log.info("Sw: {} Group Features {}", getStringId(), gfsr);
535 }
536
537 private void processGroupDesc(OFGroupDescStatsReply gdsr) {
538 log.info("Sw: {} Group Desc {}", getStringId(), gdsr);
539 }
540
541 private void populateTableVlan() throws IOException {
542 // for all incoming ports assign configured port-vlans
543 // currently assign portnum*10 -> vlanid to access ports
544 // and vlan 192 to router to router ports
545 List<OFMessage> msglist = new ArrayList<OFMessage>();
546 for (OFPortDesc p : getPorts()) {
547 int pnum = p.getPortNo().getPortNumber();
548 if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) {
549 int vlanid = getVlanConfig(pnum);
550 OFOxmInPort oxp = factory.oxms().inPort(p.getPortNo());
551 OFOxmVlanVid oxv = factory.oxms()
552 .vlanVid(OFVlanVidMatch.UNTAGGED);
553 OFOxmList oxmList = OFOxmList.of(oxp, oxv);
554 OFMatchV3 match = factory.buildMatchV3()
555 .setOxmList(oxmList).build();
556 OFOxmVlanVid vidToSet = factory.oxms()
557 .vlanVid(OFVlanVidMatch.ofVlan(vlanid));
558 OFAction pushVlan = factory.actions().pushVlan(EthType.VLAN_FRAME);
559 OFAction setVlan = factory.actions().setField(vidToSet);
560 List<OFAction> actionlist = new ArrayList<OFAction>();
561 actionlist.add(pushVlan);
562 actionlist.add(setVlan);
563 OFInstruction appAction = factory.instructions().buildApplyActions()
564 .setActions(actionlist).build();
565 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
566 .setTableId(TableId.of(TABLE_TMAC)).build();
567 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
568 instructions.add(appAction);
569 instructions.add(gotoTbl);
570 OFMessage flowEntry = factory.buildFlowAdd()
571 .setTableId(TableId.of(TABLE_VLAN))
572 .setMatch(match)
573 .setInstructions(instructions)
574 .setPriority(1000) // does not matter - all rules
575 // exclusive
576 .setBufferId(OFBufferId.NO_BUFFER)
577 .setIdleTimeout(0)
578 .setHardTimeout(0)
579 .setXid(getNextTransactionId())
580 .build();
581 msglist.add(flowEntry);
582 }
583 }
584 // table-vlan has no table-miss entry, and so packets that miss are
585 // essentially dropped
586 sendMsg(msglist);
587 log.debug("Adding {} vlan-rules in sw {}", msglist.size(), getStringId());
588 }
589
590 private void populateTableTMac() throws IOException {
591 // match for ip packets
592 OFOxmEthType oxe = factory.oxms().ethType(EthType.IPv4);
593 OFOxmList oxmListIp = OFOxmList.of(oxe);
594 OFMatchV3 matchIp = factory.buildMatchV3()
595 .setOxmList(oxmListIp).build();
596 OFInstruction gotoTblIp = factory.instructions().buildGotoTable()
597 .setTableId(TableId.of(TABLE_IPV4_UNICAST)).build();
598 List<OFInstruction> instructionsIp = Collections.singletonList(gotoTblIp);
599 OFMessage ipEntry = factory.buildFlowAdd()
600 .setTableId(TableId.of(TABLE_TMAC))
601 .setMatch(matchIp)
602 .setInstructions(instructionsIp)
603 .setPriority(1000) // strict priority required lower than
604 // multicastMac
605 .setBufferId(OFBufferId.NO_BUFFER)
606 .setIdleTimeout(0)
607 .setHardTimeout(0)
608 .setXid(getNextTransactionId())
609 .build();
610
611 // match for mpls packets
612 OFOxmEthType oxmpls = factory.oxms().ethType(EthType.MPLS_UNICAST);
613 OFOxmList oxmListMpls = OFOxmList.of(oxmpls);
614 OFMatchV3 matchMpls = factory.buildMatchV3()
615 .setOxmList(oxmListMpls).build();
616 OFInstruction gotoTblMpls = factory.instructions().buildGotoTable()
617 .setTableId(TableId.of(TABLE_MPLS)).build();
618 List<OFInstruction> instructionsMpls = Collections.singletonList(gotoTblMpls);
619 OFMessage mplsEntry = factory.buildFlowAdd()
620 .setTableId(TableId.of(TABLE_TMAC))
621 .setMatch(matchMpls)
622 .setInstructions(instructionsMpls)
623 .setPriority(1001) // strict priority required lower than
624 // multicastMac
625 .setBufferId(OFBufferId.NO_BUFFER)
626 .setIdleTimeout(0)
627 .setHardTimeout(0)
628 .setXid(getNextTransactionId())
629 .build();
630
631 // match for everything else to send to controller. Essentially
632 // the table miss flow entry
633 populateTableMissEntry(TABLE_TMAC, true, false, false, -1);
634 log.debug("Adding termination-mac-rules in sw {}", getStringId());
635 List<OFMessage> msglist = new ArrayList<OFMessage>(2);
636 msglist.add(ipEntry);
637 msglist.add(mplsEntry);
638 sendMsg(msglist);
639 }
640
641 private List<String> getMyIps() { // send to controller
642 List<String> myIps = new ArrayList<String>();
643 if (getId() == 0x1) {
644 myIps.add("10.0.2.128");
645 myIps.add("10.0.3.128");
646 myIps.add("10.0.1.128");
647 myIps.add("192.168.0.1");
648 }
649 if (getId() == 0x2) {
650 myIps.add("192.168.0.2");
651 }
652 if (getId() == 0x3) {
653 myIps.add("192.168.0.3");
654 myIps.add("7.7.7.128");
655 }
656 return myIps;
657 }
658
659 private List<String> getMySubnetIps() { // send to controller
660 List<String> subnetIps = new ArrayList<String>();
661 if (getId() == 0x1) {
662 subnetIps.add("10.0.2.0");
663 subnetIps.add("10.0.3.0");
664 subnetIps.add("10.0.1.0");
665 }
alshabib4785eec2014-12-04 16:45:45 -0800666
tom7ef8ff92014-09-17 13:08:06 -0700667 if (getId() == 0x3) {
668 subnetIps.add("7.7.7.0");
669 }
670 return subnetIps;
671 }
tom7ef8ff92014-09-17 13:08:06 -0700672 private static class RouteEntry {
673 String prefix;
674 String mask;
675 int nextHopPort;
676 String dstMac;
677 int label;
678
679 public RouteEntry(String prefix, String mask, int nextHopPort, int label) {
680 this.prefix = prefix;
681 this.mask = mask;
682 this.nextHopPort = nextHopPort;
683 this.label = label;
684 }
685
686 public RouteEntry(String prefix, int nextHopPort, String dstMac) {
687 this.prefix = prefix;
688 this.nextHopPort = nextHopPort;
689 this.dstMac = dstMac;
690 }
691 }
692
693 // send out of mpls-group where the next-hop mac-da is already set
694 private List<RouteEntry> getRouterNextHopIps() {
695 List<RouteEntry> routerNextHopIps = new ArrayList<RouteEntry>();
696 if (getId() == 0x1) {
697 routerNextHopIps
698 .add(new RouteEntry("192.168.0.2", "255.255.255.255", 6, 102));
699 routerNextHopIps
700 .add(new RouteEntry("192.168.0.3", "255.255.255.255", 6, 103));
701 routerNextHopIps.add(new RouteEntry("7.7.7.0", "255.255.255.0", 6, 103));
702 }
703 //if (getId() == 0x2) {
704 /* These are required for normal IP routing without labels.
705 routerNextHopIps.add(new RouteEntry("192.168.0.1","255.255.255.255",1));
706 routerNextHopIps.add(new RouteEntry("192.168.0.3","255.255.255.255",2));
707 routerNextHopIps.add(new RouteEntry("10.0.1.0","255.255.255.0",1));
708 routerNextHopIps.add(new RouteEntry("10.0.2.0","255.255.255.0",1));
709 routerNextHopIps.add(new RouteEntry("10.0.3.0","255.255.255.0",1));
710 routerNextHopIps.add(new RouteEntry("7.7.7.0","255.255.255.0",2));*/
711 //}
712 if (getId() == 0x3) {
713 routerNextHopIps
714 .add(new RouteEntry("192.168.0.2", "255.255.255.255", 2, 102));
715 routerNextHopIps
716 .add(new RouteEntry("192.168.0.1", "255.255.255.255", 2, 101));
717 routerNextHopIps.add(new RouteEntry("10.0.1.0", "255.255.255.0", 2, 101));
718 routerNextHopIps.add(new RouteEntry("10.0.2.0", "255.255.255.0", 2, 101));
719 routerNextHopIps.add(new RouteEntry("10.0.3.0", "255.255.255.0", 2, 101));
720 }
721 return routerNextHopIps;
722 }
723
724 // known host mac-addr, setDA/send out of l3group
725 private List<RouteEntry> getHostNextHopIps() {
726 List<RouteEntry> hostNextHopIps = new ArrayList<RouteEntry>();
727 if (getId() == 0x1) {
728 hostNextHopIps.add(new RouteEntry("10.0.2.1", 4, "00:00:00:00:02:01"));
729 hostNextHopIps.add(new RouteEntry("10.0.3.1", 5, "00:00:00:00:03:01"));
730 }
tom7ef8ff92014-09-17 13:08:06 -0700731 if (getId() == 0x3) {
732 hostNextHopIps.add(new RouteEntry("7.7.7.7", 1, "00:00:07:07:07:07"));
733 }
734 return hostNextHopIps;
735 }
736
737 private void populateIpTable() throws IOException {
738 populateMyIps();
739 populateMySubnets();
740 populateRoutes();
741 populateHostRoutes();
742
743 // match for everything else to send to ACL table. Essentially
744 // the table miss flow entry
745 populateTableMissEntry(TABLE_IPV4_UNICAST, false, true,
746 true, TABLE_ACL);
747 }
748
749 private void populateMyIps() throws IOException {
750 List<OFMessage> msglist = new ArrayList<OFMessage>();
751 // first all my ip's as exact-matches
752 // write-action instruction to send to controller
753 List<String> myIps = getMyIps();
754 for (int i = 0; i < myIps.size(); i++) {
755 OFOxmEthType ethTypeIp = factory.oxms()
756 .ethType(EthType.IPv4);
757 OFOxmIpv4DstMasked ipPrefix = factory.oxms()
758 .ipv4DstMasked(IPv4Address.of(myIps.get(i)), IPv4Address.NO_MASK);
759 OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix);
760 OFMatchV3 match = factory.buildMatchV3()
761 .setOxmList(oxmListSlash32).build();
762 OFAction outc = factory.actions().buildOutput()
763 .setPort(OFPort.CONTROLLER).setMaxLen(OFPCML_NO_BUFFER)
764 .build();
765 OFInstruction writeInstr = factory.instructions().buildWriteActions()
766 .setActions(Collections.singletonList(outc)).build();
767 OFInstruction gotoInstr = factory.instructions().buildGotoTable()
768 .setTableId(TableId.of(TABLE_ACL)).build();
769 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
770 instructions.add(writeInstr);
771 instructions.add(gotoInstr);
772 OFMessage myIpEntry = factory.buildFlowAdd()
773 .setTableId(TableId.of(TABLE_IPV4_UNICAST))
774 .setMatch(match)
775 .setInstructions(instructions)
776 .setPriority(MAX_PRIORITY) // highest priority for exact
777 // match
778 .setBufferId(OFBufferId.NO_BUFFER)
779 .setIdleTimeout(0)
780 .setHardTimeout(0)
781 .setXid(getNextTransactionId())
782 .build();
783 msglist.add(myIpEntry);
784 }
785 sendMsg(msglist);
786 log.debug("Adding {} my-ip-rules in sw {}", msglist.size(), getStringId());
787 }
788
789 private void populateMySubnets() throws IOException {
790 List<OFMessage> msglist = new ArrayList<OFMessage>();
791 // next prefix-based subnet-IP's configured on my interfaces
792 // need to ARP for exact-IP, so write-action instruction to send to
793 // controller
794 // this has different mask and priority than earlier case
795 List<String> subnetIps = getMySubnetIps();
796 for (int i = 0; i < subnetIps.size(); i++) {
797 OFOxmEthType ethTypeIp = factory.oxms()
798 .ethType(EthType.IPv4);
799 OFOxmIpv4DstMasked ipPrefix = factory.oxms().ipv4DstMasked(
800 IPv4Address.of(subnetIps.get(i)),
801 IPv4Address.of(0xffffff00)); // '/24' mask
802 OFOxmList oxmListSlash24 = OFOxmList.of(ethTypeIp, ipPrefix);
803 OFMatchV3 match = factory.buildMatchV3()
804 .setOxmList(oxmListSlash24).build();
805 OFAction outc = factory.actions().buildOutput()
806 .setPort(OFPort.CONTROLLER).setMaxLen(OFPCML_NO_BUFFER)
807 .build();
808 OFInstruction writeInstr = factory.instructions().buildWriteActions()
809 .setActions(Collections.singletonList(outc)).build();
810 OFInstruction gotoInstr = factory.instructions().buildGotoTable()
811 .setTableId(TableId.of(TABLE_ACL)).build();
812 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
813 instructions.add(writeInstr);
814 instructions.add(gotoInstr);
815 OFMessage myIpEntry = factory.buildFlowAdd()
816 .setTableId(TableId.of(TABLE_IPV4_UNICAST))
817 .setMatch(match)
818 .setInstructions(instructions)
819 .setPriority(SLASH_24_PRIORITY)
820 .setBufferId(OFBufferId.NO_BUFFER)
821 .setIdleTimeout(0)
822 .setHardTimeout(0)
823 .setXid(getNextTransactionId())
824 .build();
825 msglist.add(myIpEntry);
826 }
827 sendMsg(msglist);
828 log.debug("Adding {} subnet-ip-rules in sw {}", msglist.size(), getStringId());
829 msglist.clear();
830 }
831
832 private void populateRoutes() throws IOException {
833 List<OFMessage> msglist = new ArrayList<OFMessage>();
834 // addresses where I know the next-hop's mac-address because it is a
835 // router port - so I have an L3 interface to it (and an MPLS interface)
836 List<RouteEntry> routerNextHopIps = getRouterNextHopIps();
837 for (int i = 0; i < routerNextHopIps.size(); i++) {
838 OFOxmEthType ethTypeIp = factory.oxms()
839 .ethType(EthType.IPv4);
840 OFOxmIpv4DstMasked ipPrefix = factory.oxms()
841 .ipv4DstMasked(
842 IPv4Address.of(routerNextHopIps.get(i).prefix),
843 IPv4Address.of(routerNextHopIps.get(i).mask)
844 );
845 OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix);
846 OFMatchV3 match = factory.buildMatchV3()
847 .setOxmList(oxmListSlash32).build();
848 OFAction outg = factory.actions().buildGroup()
849 .setGroup(OFGroup.of(0xa0000000 | // mpls group id
850 routerNextHopIps.get(i).nextHopPort))
851 .build();
852 // lots of actions before forwarding to mpls group, and
853 // unfortunately
854 // they need to be apply-actions
855
856 OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST);
857 OFOxmMplsLabel l = factory.oxms()
858 .mplsLabel(U32.of(routerNextHopIps.get(i).label));
859 OFAction setlabelid = factory.actions().buildSetField()
860 .setField(l).build();
861 OFAction copyTtlOut = factory.actions().copyTtlOut();
862 // OFAction setBos =
863 // factory.actions().buildSetField().setField(bos).build();
864
865 /*
866 writeActions.add(pushlabel); // need to be apply actions so can be
867 writeActions.add(copyTtlOut); // matched in pseudo-table
868 //writeActions.add(setlabelid); // bad support in cpqd
869 //writeActions.add(setBos); no support in loxigen
870 */
871
872 List<OFAction> applyActions = new ArrayList<OFAction>();
873 applyActions.add(pushlabel);
874 applyActions.add(copyTtlOut);
875 OFInstruction applyInstr = factory.instructions().buildApplyActions()
876 .setActions(applyActions).build();
877 List<OFAction> writeActions = new ArrayList<OFAction>();
878 writeActions.add(outg); // group will decr mpls-ttl, set mac-sa/da,
879 // vlan
880 OFInstruction writeInstr = factory.instructions().buildWriteActions()
881 .setActions(writeActions).build();
882
883 // necessary to match in pseudo-table to overcome cpqd 1.3 flaw
884 OFInstruction writeMeta = factory.instructions().buildWriteMetadata()
885 .setMetadata(U64.of(routerNextHopIps.get(i).label))
886 .setMetadataMask(METADATA_MASK).build();
887 /*OFInstruction gotoInstr = factory.instructions().buildGotoTable()
888 .setTableId(TableId.of(TABLE_ACL)).build();*/
889 OFInstruction gotoInstr = factory.instructions().buildGotoTable()
890 .setTableId(TableId.of(TABLE_META)).build();
891 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
892 instructions.add(applyInstr);
893 // instructions.add(writeInstr);// cannot write here - causes switch
894 // to crash
895 instructions.add(writeMeta);
896 instructions.add(gotoInstr);
897
898 int priority = -1;
899 if (routerNextHopIps.get(i).mask.equals("255.255.255.255")) {
900 priority = MAX_PRIORITY;
901 } else {
902 priority = SLASH_24_PRIORITY;
903 }
904 OFMessage myIpEntry = factory.buildFlowAdd()
905 .setTableId(TableId.of(TABLE_IPV4_UNICAST))
906 .setMatch(match)
907 .setInstructions(instructions)
908 .setPriority(priority)
909 .setBufferId(OFBufferId.NO_BUFFER)
910 .setIdleTimeout(0)
911 .setHardTimeout(0)
912 .setXid(getNextTransactionId())
913 .build();
914 msglist.add(myIpEntry);
915
916 // need to also handle psuedo-table entries to match-metadata and
917 // set mpls
918 // label-id
919 OFOxmEthType ethTypeMpls = factory.oxms()
920 .ethType(EthType.MPLS_UNICAST);
921 OFOxmMetadataMasked meta = factory.oxms()
922 .metadataMasked(
923 OFMetadata.ofRaw(routerNextHopIps.get(i).label),
924 OFMetadata.NO_MASK);
925 OFOxmList oxmListMeta = OFOxmList.of(ethTypeMpls, meta);
926 OFMatchV3 matchMeta = factory.buildMatchV3()
927 .setOxmList(oxmListMeta).build();
928 List<OFAction> writeActions2 = new ArrayList<OFAction>();
929 writeActions2.add(setlabelid);
930 OFAction outg2 = factory.actions().buildGroup()
931 .setGroup(OFGroup.of(routerNextHopIps.get(i).nextHopPort |
932 (192 << VLAN_ID_OFFSET)))
933 .build();
934 writeActions2.add(outg2);
935 OFInstruction writeInstr2 = factory.instructions().buildWriteActions()
936 .setActions(writeActions2).build();
937 OFInstruction gotoInstr2 = factory.instructions().buildGotoTable()
938 .setTableId(TableId.of(TABLE_ACL)).build();
939 List<OFInstruction> instructions2 = new ArrayList<OFInstruction>();
940 // unfortunately have to apply this action too
941 OFInstruction applyInstr2 = factory.instructions().buildApplyActions()
942 .setActions(writeActions2).build();
943 instructions2.add(applyInstr2);
944 // instructions2.add(writeInstr2);
945 // instructions2.add(gotoInstr2);
946
947 /*OFMatchV3 match3 = factory.buildMatchV3()
948 .setOxmList(OFOxmList.of(meta)).build();
949 OFInstruction clearInstruction = factory.instructions().clearActions();
950 List<OFInstruction> instructions3 = new ArrayList<OFInstruction>();
951 OFAction outc = factory.actions().buildOutput()
952 .setPort(OFPort.CONTROLLER).setMaxLen(OFPCML_NO_BUFFER)
953 .build();
954 OFInstruction writec = factory.instructions()
955 .writeActions(Collections.singletonList(outc));
956 instructions3.add(clearInstruction);
957 instructions3.add(writec);
958 instructions3.add(gotoInstr2); */
959 OFMessage myMetaEntry = factory.buildFlowAdd()
960 .setTableId(TableId.of(TABLE_META))
961 .setMatch(matchMeta)
962 .setInstructions(instructions2)
963 .setPriority(MAX_PRIORITY)
964 .setBufferId(OFBufferId.NO_BUFFER)
965 .setIdleTimeout(0)
966 .setHardTimeout(0)
967 .setXid(getNextTransactionId())
968 .build();
969 msglist.add(myMetaEntry);
970
971 }
972 sendMsg(msglist);
973 log.debug("Adding {} next-hop-router-rules in sw {}", msglist.size(),
974 getStringId());
975
976 // add a table-miss entry to table 4 for debugging - leave it out
977 // unclear packet state - causes switch to crash
978 // populateTableMissEntry(TABLE_META, false, true,
979 // true, TABLE_ACL);
980 }
981
982 private void populateHostRoutes() throws IOException {
983 List<OFMessage> msglist = new ArrayList<OFMessage>();
984 // addresses where I know the next hop's mac-address and I can set the
985 // destination mac in the match-instruction.write-action
986 // either I sent out arp-request or I got an arp-request from this host
987 List<RouteEntry> hostNextHopIps = getHostNextHopIps();
988 for (int i = 0; i < hostNextHopIps.size(); i++) {
989 OFOxmEthType ethTypeIp = factory.oxms()
990 .ethType(EthType.IPv4);
991 OFOxmIpv4DstMasked ipPrefix = factory.oxms()
992 .ipv4DstMasked(
993 IPv4Address.of(hostNextHopIps.get(i).prefix),
994 IPv4Address.NO_MASK); // host addr should be /32
995 OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix);
996 OFMatchV3 match = factory.buildMatchV3()
997 .setOxmList(oxmListSlash32).build();
998 OFAction setDmac = null, outg = null;
999 OFOxmEthDst dmac = factory.oxms()
1000 .ethDst(MacAddress.of(hostNextHopIps.get(i).dstMac));
1001 setDmac = factory.actions().buildSetField()
1002 .setField(dmac).build();
1003 outg = factory.actions().buildGroup()
1004 .setGroup(OFGroup.of(0x20000000 | hostNextHopIps.get(i).nextHopPort)) // l3group
1005 // id
1006 .build();
1007 List<OFAction> writeActions = new ArrayList<OFAction>();
1008 writeActions.add(setDmac);
1009 writeActions.add(outg);
1010 OFInstruction writeInstr = factory.instructions().buildWriteActions()
1011 .setActions(writeActions).build();
1012 OFInstruction gotoInstr = factory.instructions().buildGotoTable()
1013 .setTableId(TableId.of(TABLE_ACL)).build();
1014 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
1015 instructions.add(writeInstr);
1016 instructions.add(gotoInstr);
1017 OFMessage myIpEntry = factory.buildFlowAdd()
1018 .setTableId(TableId.of(TABLE_IPV4_UNICAST))
1019 .setMatch(match)
1020 .setInstructions(instructions)
1021 .setPriority(MAX_PRIORITY) // highest priority for exact
1022 // match
1023 .setBufferId(OFBufferId.NO_BUFFER)
1024 .setIdleTimeout(0)
1025 .setHardTimeout(0)
1026 .setXid(getNextTransactionId())
1027 .build();
1028 msglist.add(myIpEntry);
1029 }
1030 sendMsg(msglist);
1031 log.debug("Adding {} next-hop-host-rules in sw {}", msglist.size(), getStringId());
1032 }
1033
1034 private static class MplsEntry {
1035 int labelid;
1036 int portnum;
1037
1038 public MplsEntry(int labelid, int portnum) {
1039 this.labelid = labelid;
1040 this.portnum = portnum;
1041 }
1042 }
1043
1044 private List<MplsEntry> getMplsEntries() {
1045 List<MplsEntry> myLabels = new ArrayList<MplsEntry>();
1046 if (getId() == 0x1) {
1047 myLabels.add(new MplsEntry(101, OFPort.CONTROLLER.getPortNumber()));
1048 myLabels.add(new MplsEntry(103, 6));
1049 }
1050 if (getId() == 0x2) {
1051 myLabels.add(new MplsEntry(103, 2));
1052 myLabels.add(new MplsEntry(102, OFPort.CONTROLLER.getPortNumber()));
1053 myLabels.add(new MplsEntry(101, 1));
1054 }
1055 if (getId() == 0x3) {
1056 myLabels.add(new MplsEntry(103, OFPort.CONTROLLER.getPortNumber()));
1057 myLabels.add(new MplsEntry(101, 2));
1058 }
1059 return myLabels;
1060 }
1061
1062 private void populateMplsTable() throws IOException {
1063 List<OFMessage> msglist = new ArrayList<OFMessage>();
1064 List<MplsEntry> lfibEntries = getMplsEntries();
1065 for (int i = 0; i < lfibEntries.size(); i++) {
1066 OFOxmEthType ethTypeMpls = factory.oxms()
1067 .ethType(EthType.MPLS_UNICAST);
1068 OFOxmMplsLabel labelid = factory.oxms()
1069 .mplsLabel(U32.of(lfibEntries.get(i).labelid));
1070 OFOxmList oxmList = OFOxmList.of(ethTypeMpls, labelid);
1071 OFMatchV3 matchlabel = factory.buildMatchV3()
1072 .setOxmList(oxmList).build();
1073 OFAction poplabel = factory.actions().popMpls(EthType.IPv4);
1074 OFAction sendTo = null;
1075 if (lfibEntries.get(i).portnum == OFPort.CONTROLLER.getPortNumber()) {
1076 sendTo = factory.actions().output(OFPort.CONTROLLER,
1077 OFPCML_NO_BUFFER);
1078 } else {
1079 sendTo = factory.actions().group(OFGroup.of(
1080 0xa0000000 | lfibEntries.get(i).portnum));
1081 }
1082 List<OFAction> writeActions = new ArrayList<OFAction>();
1083 writeActions.add(poplabel);
1084 writeActions.add(sendTo);
1085 OFInstruction writeInstr = factory.instructions().buildWriteActions()
1086 .setActions(writeActions).build();
1087 OFInstruction gotoInstr = factory.instructions().buildGotoTable()
1088 .setTableId(TableId.of(TABLE_ACL)).build();
1089 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
1090 instructions.add(writeInstr);
1091 instructions.add(gotoInstr);
1092 OFMessage myMplsEntry = factory.buildFlowAdd()
1093 .setTableId(TableId.of(TABLE_MPLS))
1094 .setMatch(matchlabel)
1095 .setInstructions(instructions)
1096 .setPriority(MAX_PRIORITY) // exact match and exclusive
1097 .setBufferId(OFBufferId.NO_BUFFER)
1098 .setIdleTimeout(0)
1099 .setHardTimeout(0)
1100 .setXid(getNextTransactionId())
1101 .build();
1102 msglist.add(myMplsEntry);
1103 }
1104 sendMsg(msglist);
1105 log.debug("Adding {} mpls-forwarding-rules in sw {}", msglist.size(),
1106 getStringId());
1107
1108 // match for everything else to send to ACL table. Essentially
1109 // the table miss flow entry
1110 populateTableMissEntry(TABLE_MPLS, false, true,
1111 true, TABLE_ACL);
1112
1113 }
1114
1115 /**
1116 * By default if none of the booleans in the call are set, then the
1117 * table-miss entry is added with no instructions, which means that pipeline
1118 * execution will stop, and the action set associated with the packet will
1119 * be executed.
1120 *
1121 * @param tableToAdd
1122 * @param toControllerNow as an APPLY_ACTION instruction
1123 * @param toControllerWrite as a WRITE_ACITION instruction
1124 * @param toTable as a GOTO_TABLE instruction
1125 * @param tableToSend
alshabib452234e2014-11-25 00:03:49 -05001126 * @throws java.io.IOException
tom7ef8ff92014-09-17 13:08:06 -07001127 */
1128 @SuppressWarnings("unchecked")
1129 private void populateTableMissEntry(int tableToAdd, boolean toControllerNow,
1130 boolean toControllerWrite,
1131 boolean toTable, int tableToSend) {
1132 OFOxmList oxmList = OFOxmList.EMPTY;
1133 OFMatchV3 match = factory.buildMatchV3()
1134 .setOxmList(oxmList)
1135 .build();
1136 OFAction outc = factory.actions()
1137 .buildOutput()
1138 .setPort(OFPort.CONTROLLER)
1139 .setMaxLen(OFPCML_NO_BUFFER)
1140 .build();
1141 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
1142 if (toControllerNow) {
1143 // table-miss instruction to send to controller immediately
1144 OFInstruction instr = factory.instructions()
1145 .buildApplyActions()
1146 .setActions(Collections.singletonList(outc))
1147 .build();
1148 instructions.add(instr);
1149 }
1150
1151 if (toControllerWrite) {
1152 // table-miss instruction to write-action to send to controller
1153 // this will be executed whenever the action-set gets executed
1154 OFInstruction instr = factory.instructions()
1155 .buildWriteActions()
1156 .setActions(Collections.singletonList(outc))
1157 .build();
1158 instructions.add(instr);
1159 }
1160
1161 if (toTable) {
1162 // table-miss instruction to goto-table x
1163 OFInstruction instr = factory.instructions()
1164 .gotoTable(TableId.of(tableToSend));
1165 instructions.add(instr);
1166 }
1167
1168 if (!toControllerNow && !toControllerWrite && !toTable) {
1169 // table-miss has no instruction - at which point action-set will be
1170 // executed - if there is an action to output/group in the action
1171 // set
1172 // the packet will be sent there, otherwise it will be dropped.
1173 instructions = Collections.EMPTY_LIST;
1174 }
1175
1176 OFMessage tableMissEntry = factory.buildFlowAdd()
1177 .setTableId(TableId.of(tableToAdd))
1178 .setMatch(match) // match everything
1179 .setInstructions(instructions)
1180 .setPriority(MIN_PRIORITY)
1181 .setBufferId(OFBufferId.NO_BUFFER)
1182 .setIdleTimeout(0)
1183 .setHardTimeout(0)
1184 .setXid(getNextTransactionId())
1185 .build();
Jonathan Hart147b2ac2014-10-23 10:03:52 -07001186
1187 write(tableMissEntry);
tom7ef8ff92014-09-17 13:08:06 -07001188 }
1189
1190 private void sendBarrier(boolean finalBarrier) {
1191 int xid = getNextTransactionId();
1192 if (finalBarrier) {
1193 barrierXidToWaitFor = xid;
1194 }
1195 OFBarrierRequest br = factory
1196 .buildBarrierRequest()
1197 .setXid(xid)
1198 .build();
Jonathan Hart147b2ac2014-10-23 10:03:52 -07001199
1200 write(br);
tom7ef8ff92014-09-17 13:08:06 -07001201 }
1202
1203 @Override
1204 public Boolean supportNxRole() {
1205 return false;
1206 }
1207
1208 @Override
1209 public void write(OFMessage msg) {
Jonathan Hart147b2ac2014-10-23 10:03:52 -07001210 this.channel.write(Collections.singletonList(msg));
tom7ef8ff92014-09-17 13:08:06 -07001211
1212 }
1213
1214 @Override
1215 public void write(List<OFMessage> msgs) {
1216 this.channel.write(msgs);
1217 }
1218
1219}