Can we do ML to P4 code Translation?

Can we write the P4 code for these attributes we got from ML decision tree? We need to do this to block video traffic and forward non-video traffic to a specific port. Our setup has remote onos connected to BMv2 switches and hosts.

if Timestamp <= 14020.50:
if Timestamp <= 11157.00:
return 1
else:
if Bwd_Packet_Length_Mean <= 1889.91:
if Destination_IP <= 180881256.00:
if Timestamp <= 12256.50:
if Source_IP <= 3232243840.00:
return 0
else:
return 1
else:
if Flow_ID <= 160140.50:
if act_data_pkt_fwd <= 7.50:
if Bwd_Packet_Length_Max <= 216.50:
if Avg_Bwd_Segment_Size <= 5.88:
if Flow_IAT_Max <= 20133.50:
return 0
else:
return 0
else:
if Init_Win_bytes_forward <= 2331.00:
return 0
else:
return 0
else:
if Flow_Duration <= 722762.00:
return 0
else:
return 0
else:
if Fwd_Packet_Length_Min <= 3.00:
if Fwd_IAT_Min <= 1.50:
return 0
else:
return 0
else:
return 0
else:
if Fwd_IAT_Max <= 12005848.00:
if Fwd_IAT_Min <= 21.00:
return 0
else:
return 0
else:
return 0
else:
if Destination_Port <= 443.50:
if Init_Win_bytes_backward <= 788.50:
if Init_Win_bytes_backward <= 82.50:
if Init_Win_bytes_backward <= 9.50:
if Init_Win_bytes_forward <= 294.50:
return 0
else:
return 0
else:
return 0
else:
if Init_Win_bytes_backward <= 319.50:
if Destination_IP <= 1249469504.00:
return 0
else:
return 1
else:
if Flow_ID <= 30883.50:
return 0
else:
if Flow_ID <= 50463.50:
if Flow_ID <= 46664.00:
return 0
else:
return 0
else:
if Flow_ID <= 187878.00:
return 0
else:
return 0
else:
if Idle_Std <= 682704.41:
if Destination_IP <= 392860592.00:
return 0
else:
if Destination_Port <= 261.50:
if Fwd_IAT_Total <= 59253812.00:
if Source_Port <= 57706.00:
return 0
else:
return 0
else:
return 0
else:
if Bwd_Packet_Length_Max <= 146.50:
return 0
else:
if Bwd_IAT_Std <= 80937.34:
return 0
else:
return 0
else:
return 0
else:
if Source_IP <= 1249606912.00:
if Bwd_Packet_Length_Max <= 452.00:
if Flow_ID <= 30442.00:
return 0
else:
if Destination_Port <= 60581.50:
if Bwd_Packets_s <= 2120.89:
if Flow_ID <= 153997.50:
return 0
else:
return 0
else:
return 0
else:
return 0
else:
return 0
else:
return 0
else:
if Fwd_Packet_Length_Max <= 1093.50:
return 0
else:
return 1
else:
return 1

So P4 has if statements, and for example if all of the code you have was inside of a control, with the variables in ‘if’ conditions as in parameters, and the return value as a single out parameter, then you could replace all of the return statements with assignments to that parameter. Then invoke that control from your top level packet processing code wherever you wanted to, and Bob’s your uncle.

If you are using the BMv2 software switch, it should be able to do packet processing using P4 code like that with no problem. Whether that code “fits” into the hardware constraints of other P4-programmable hardware devices is a separate question that would have to be addressed on a device-by-device case.

1 Like