Skip to content

Commit 2e1153b

Browse files
authored
Flowcache fixes 1 (#682)
* Move optional steps for testing VMs out of README.md file This may help avoid new users being confused that they must follow those instructions. Signed-off-by: Andy Fingerhut <andy_fingerhut@alum.wustl.edu> * Add 2025-Jan-30 versions of source code for install.sh Signed-off-by: Andy Fingerhut <andy_fingerhut@alum.wustl.edu> * Some fixes for flowcache exercise mycontroller.py In particular, use correct values for PacketIn controller metadata fields. Signed-off-by: Andy Fingerhut <andy_fingerhut@alum.wustl.edu> --------- Signed-off-by: Andy Fingerhut <andy_fingerhut@alum.wustl.edu>
1 parent ab29d06 commit 2e1153b

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

exercises/flowcache/solution/flowcache.p4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ control MyIngress(inout headers_t hdr,
253253
// IPv4, nor PacketOut packets from the controller.
254254
// TODO: Update per-input-port packet count for packets
255255
// dropped because they are not IPv4.
256+
drop_packet();
256257
}
257258
}
258259
}

exercises/flowcache/solution/mycontroller.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ def addFlowRule( ingress_sw, src_ip_addr, dst_ip_addr, protocol, port, new_dscp,
181181
ingress_sw.WriteTableEntry(table_entry)
182182
print("Installed ingress rule on %s" % ingress_sw.name)
183183

184+
def PacketOutMetadataList(opcode, reserved1, operand0):
185+
# This function does not use the generated contents of the P4Info
186+
# file to map PacketOut metadata fields to indices. If you change
187+
# the PacketOut metadata format in the P4 program, this code must
188+
# be manually updated to match.
189+
return [{"value": opcode, "bitwidth": 8},
190+
{"value": reserved1, "bitwidth": 8},
191+
{"value": operand0, "bitwidth": 32}]
192+
184193
def sendPacketOut(sw ,payload, metadatas):
185194
#TODO remove for exercise
186195
sw.PacketOut(payload, metadatas)
@@ -236,7 +245,8 @@ async def printCounter(p4info_helper, sw, counter_name, index):
236245
async def processPacket(message):
237246
payload = message["packet-in"].packet.payload
238247
packet = message["packet-in"].packet
239-
print("Received %d PacketIn messages" % (len(payload)))
248+
print("Received PacketIn message of length %d bytes from switch %s"
249+
"" % (len(payload), message["sw"].name))
240250
if len(payload) > 0:
241251
i = 0
242252
pkt = Ether(payload)
@@ -269,7 +279,9 @@ async def processPacket(message):
269279
new_dscp_int = 5
270280
global_data['index'] = int(pkt[IP].dst.split('.')[3])
271281
dst_eth_addr = global_data[ip_da_str]
272-
metadatas = [{ "value": 0, "bitwidth": 8 }, { "value": 3, "bitwidth": 32}]
282+
metadatas = PacketOutMetadataList(
283+
global_data['controller_opcode_name2int']['SEND_TO_PORT_IN_OPERAND0'],
284+
0, dest_port_int)
273285
sendPacketOut(message["sw"], payload, metadatas)
274286
addFlowRule(message["sw"],
275287
src_ip_addr,
@@ -384,6 +396,8 @@ async def main(p4info_file_path, bmv2_file_path):
384396

385397
global_data['punt_reason_name2int'], global_data['punt_reason_int2name'] = \
386398
serializableEnumDict(p4info_helper.p4info, 'PuntReason_t')
399+
global_data['controller_opcode_name2int'], global_data['controller_opcode_int2name'] = \
400+
serializableEnumDict(p4info_helper.p4info, 'ControllerOpcode_t')
387401

388402
try:
389403
replicas = [{ "egress_port": global_data['CPU_PORT'], "instance": 1 }]
@@ -436,4 +450,4 @@ async def main(p4info_file_path, bmv2_file_path):
436450
parser.print_help()
437451
print("\nBMv2 JSON file not found: %s\nHave you run 'make'?" % args.bmv2_json)
438452
parser.exit(1)
439-
asyncio.run(main(args.p4info, args.bmv2_json))
453+
asyncio.run(main(args.p4info, args.bmv2_json))

0 commit comments

Comments
 (0)