-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support P4.org In-band Network Telemetry (INT) #4230
Open
jingfeihu
wants to merge
1
commit into
secdev:master
Choose a base branch
from
jingfeihu:master.hujf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
# scapy.contrib.description = Inband Network Telemetry Protocol (INT) | ||
# scapy.contrib.status = loads | ||
|
||
''' | ||
Inband Network Telemetry Protocol (INT) | ||
|
||
References: | ||
https://staging.p4.org/p4-spec/docs/INT_v2_1.pdf | ||
https://staging.p4.org/p4-spec/docs/telemetry_report_v2_0.pdf | ||
https://github.com/p4lang/p4-applications | ||
|
||
Example Packet Format: | ||
INT-MX mode: | ||
INToGre = Ether/IP/GRE/INTShimGre/INTMetaMx/Raw | ||
INToTCP = Ether/IP/TCP/INTShimTcpUdp/INTMetaMx/Raw | ||
INToUDP = Ether/IP/UDP/INTShimTcpUdp/INTMetaMx/Raw | ||
INToVXLAN = Ether/IP/UDP/VXLAN/INTShimVxlan/INTMetaMx/Raw | ||
INToGENEVE = Ether/IP/UDP/GENEVE/GeneveOptINT/INTMetaMx/Raw | ||
|
||
INT-MD mode: | ||
INToGre = Ether/IP/GRE/INTShimGre/INTMetaMd/INTMetaHop/Raw | ||
INToTCP = Ether/IP/TCP/INTShimTcpUdp/INTMetaMd/INTMetaHop/Raw | ||
INToUDP = Ether/IP/UDP/INTShimTcpUdp/INTMetaMd/INTMetaHop/Raw | ||
INToVXLAN = Ether/IP/UDP/VXLAN/INTShimVxlan/INTMetaMd/INTMetaHop/Raw | ||
INToGENEVE = Ether/IP/UDP/GENEVE/GeneveOptINT/INTMetaMd/INTMetaHop/Raw | ||
''' | ||
|
||
import struct | ||
from scapy.packet import Packet, bind_layers | ||
from scapy.fields import BitField, BitEnumField, FlagsField, ByteField, \ | ||
ShortField, IntField, LongField, FieldLenField, ConditionalField, \ | ||
MultipleTypeField, PacketField, PacketListField | ||
from scapy.layers.l2 import GRE | ||
from scapy.layers.inet import TCP, UDP | ||
from scapy.layers.vxlan import VXLAN | ||
|
||
INT_PRI_MASK = 0x80 | ||
INT_L4_DPORT = 0x4568 | ||
INT_GRE_PROTOCOL = 0x4569 | ||
INT_VXLAN_PROTOCOL = 0x82 | ||
INT_GENEVE_CLASSID = 0x0103 | ||
|
||
_INT_TYPE = { | ||
1: 'INT-MD', | ||
2: 'INT-DST', | ||
3: 'INT-MX', | ||
} | ||
|
||
_INT_GRE = { | ||
0: 'Original packet with GRE', | ||
1: 'Original packet without GRE', | ||
} | ||
|
||
_INT_GPE = { | ||
0: 'Original packet used VXLAN GPE encapsulation', | ||
1: 'Original packet used VXLAN encapsulation', | ||
} | ||
|
||
_INT_INSTR_BITMAP = [ | ||
'checksum', | ||
'reserved14', | ||
'reserved13', | ||
'reserved12', | ||
'reserved11', | ||
'reserved10', | ||
'reserved9', | ||
'buf_info', | ||
'tx_info', | ||
'l2_intf', | ||
'egr_ts', | ||
'igr_ts', | ||
'que_info', | ||
'latency', | ||
'l1_intf', | ||
'node_id', | ||
] | ||
|
||
|
||
class INTMetaHop(Packet): | ||
name = 'INTMetaHop' | ||
fields_desc = [ | ||
ConditionalField( | ||
IntField('node_id', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 0)) | ||
), | ||
ConditionalField( | ||
ShortField('igr_l1_intf', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 1)) | ||
), | ||
ConditionalField( | ||
ShortField('egr_l1_intf', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 1)) | ||
), | ||
ConditionalField( | ||
IntField('latency', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 2)) | ||
), | ||
ConditionalField( | ||
BitField('que_id', 0, 8), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 3)) | ||
), | ||
ConditionalField( | ||
BitField('que_occupy', 0, 24), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 3)) | ||
), | ||
ConditionalField( | ||
LongField('igr_ts', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 4)) | ||
), | ||
ConditionalField( | ||
LongField('egr_ts', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 5)) | ||
), | ||
ConditionalField( | ||
IntField('igr_l2_intf', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 6)) | ||
), | ||
ConditionalField( | ||
IntField('egr_l2_intf', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 6)) | ||
), | ||
ConditionalField( | ||
IntField('egr_tx_info', 0), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 7)) | ||
), | ||
ConditionalField( | ||
BitField('buf_id', 0, 8), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 8)) | ||
), | ||
ConditionalField( | ||
BitField('buf_occupy', 0, 24), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 8)) | ||
), | ||
ConditionalField( | ||
IntField('reserved9', 0xffffffff), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 9)) | ||
), | ||
ConditionalField( | ||
IntField('reserved10', 0xffffffff), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 10)) | ||
), | ||
ConditionalField( | ||
IntField('reserved11', 0xffffffff), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 11)) | ||
), | ||
ConditionalField( | ||
IntField('reserved12', 0xffffffff), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 12)) | ||
), | ||
ConditionalField( | ||
IntField('reserved13', 0xffffffff), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 13)) | ||
), | ||
ConditionalField( | ||
IntField('reserved14', 0xffffffff), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 14)) | ||
), | ||
ConditionalField( | ||
IntField('checksum', 0xffffffff), | ||
lambda pkt:pkt.parent.instr_bitmap & (0x1 << (15 - 15)) | ||
), | ||
] | ||
|
||
def extract_padding(self, s): | ||
return "", s | ||
|
||
|
||
class INTMetaMx(Packet): | ||
name = 'INTMetaMx' | ||
fields_desc = [ | ||
BitField('version', 0, 4), | ||
BitField('discard', 0, 1), | ||
BitField('reserved1', 0, 27), | ||
FlagsField('instr_bitmap', 0xfe00, 16, _INT_INSTR_BITMAP), | ||
ShortField('ds_id', 0), | ||
ShortField('ds_instr', 0), | ||
ShortField('ds_flags', 0), | ||
] | ||
|
||
def extract_padding(self, s): | ||
return "", s | ||
|
||
|
||
class INTMetaMd(Packet): | ||
name = 'INTMetaMd' | ||
fields_desc = [ | ||
BitField('version', 0, 4), | ||
BitField('discard', 0, 1), | ||
BitField('exceed_mht', 0, 1), | ||
BitField('exceed_mtu', 0, 1), | ||
BitField('reserved0', 0, 12), | ||
BitField('hop_len', 0, 5), | ||
BitField('hop_left', 0, 8), | ||
FlagsField('instr_bitmap', 0xfe00, 16, _INT_INSTR_BITMAP), | ||
ShortField('ds_id', 0), | ||
ShortField('ds_instr', 0), | ||
ShortField('ds_flags', 0), | ||
PacketListField('meta_hops', [], INTMetaHop, | ||
length_from=lambda pkt: pkt.parent.length * 4 - 12), | ||
] | ||
|
||
def post_build(self, pkt, pay): | ||
if self.meta_hops is not None: | ||
tmp_len = len(self.meta_hops[0]) // 4 | ||
old_value = struct.unpack("B", pkt[2:3])[0] | ||
new_value = (old_value & 0b11100000) | (tmp_len & 0b00011111) | ||
pkt = pkt[:2] + struct.pack("B", new_value) + pkt[3:] | ||
return pkt + pay | ||
|
||
def extract_padding(self, s): | ||
return "", s | ||
|
||
|
||
class INTShimTcpUdp(Packet): | ||
name = 'INTShimTcpUdp' | ||
fields_desc = [ | ||
BitEnumField('type', 1, 4, _INT_TYPE), | ||
BitField('npt', 0, 2), | ||
BitField('reserved1', 0, 2), | ||
FieldLenField('length', None, | ||
length_of="metadata", | ||
adjust=lambda pkt, x: x // 4, fmt="B"), | ||
ConditionalField(ByteField('reserved3', 0), lambda pkt: pkt.npt == 0), | ||
ConditionalField(BitField('dscp', 0, 6), lambda pkt: pkt.npt == 0), | ||
ConditionalField(BitField('reserved4', 0, 2), lambda pkt: pkt.npt == 0), | ||
ConditionalField(ShortField('l4_dport', 0), lambda pkt: pkt.npt == 1), | ||
ConditionalField(ByteField('ip_proto', 0), lambda pkt: pkt.npt == 2), | ||
ConditionalField(ByteField('reserved5', 0), lambda pkt: pkt.npt == 2), | ||
MultipleTypeField([ | ||
(PacketField('metadata', None, INTMetaMd), lambda pkt: pkt.type == 1), | ||
(PacketField('metadata', None, INTMetaMx), lambda pkt: pkt.type == 3), ], | ||
PacketField('metadata', None, INTMetaMd) | ||
), | ||
] | ||
|
||
|
||
class INTShimGre(Packet): | ||
name = 'INTShimGre' | ||
fields_desc = [ | ||
BitEnumField('type', 1, 4, _INT_TYPE), | ||
BitEnumField('gre', 0, 1, _INT_GRE), | ||
BitField('reserved0', 0, 3), | ||
FieldLenField('length', None, | ||
length_of="metadata", | ||
adjust=lambda pkt, x: x // 4, fmt="B"), | ||
ShortField('gre_proto', 0), | ||
MultipleTypeField([ | ||
(PacketField('metadata', None, INTMetaMd), lambda pkt: pkt.type == 1), | ||
(PacketField('metadata', None, INTMetaMx), lambda pkt: pkt.type == 3), ], | ||
PacketField('metadata', None, INTMetaMd) | ||
), | ||
] | ||
|
||
|
||
class INTShimVxlan(Packet): | ||
name = 'INTShimVxlan' | ||
fields_desc = [ | ||
BitEnumField('type', 1, 4, _INT_TYPE), | ||
BitField('reserved2', 0, 4), | ||
FieldLenField('length', None, | ||
length_of="metadata", | ||
adjust=lambda pkt, x: x // 4, fmt="B"), | ||
BitEnumField('gpe', 0, 1, _INT_GPE), | ||
BitField('reserved6', 0, 7), | ||
ByteField('vxlan_proto', 0), | ||
MultipleTypeField([ | ||
(PacketField('metadata', None, INTMetaMd), lambda pkt: pkt.type == 1), | ||
(PacketField('metadata', None, INTMetaMx), lambda pkt: pkt.type == 3), ], | ||
PacketField('metadata', None, INTMetaMd) | ||
), | ||
] | ||
|
||
|
||
bind_layers(UDP, INTShimTcpUdp, dport=INT_L4_DPORT) | ||
bind_layers(TCP, INTShimTcpUdp, dport=INT_L4_DPORT) | ||
bind_layers(GRE, INTShimGre, proto=INT_GRE_PROTOCOL) | ||
bind_layers(VXLAN, INTShimVxlan, NextProtocol=INT_VXLAN_PROTOCOL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -806,6 +806,15 @@ def mysummary(self): | |
else: | ||
return self.sprintf("TCP %TCP.sport% > %TCP.dport% %TCP.flags%") | ||
|
||
def guess_payload_class(self, payload): | ||
from scapy.contrib.int import INTShimTcpUdp, INT_PRI_MASK | ||
if ((isinstance(self.underlayer, IP) and | ||
((self.underlayer.tos & INT_PRI_MASK) == INT_PRI_MASK)) or | ||
(isinstance(self.underlayer, scapy.layers.inet6.IPv6) and | ||
((self.underlayer.tc & INT_PRI_MASK) == INT_PRI_MASK))): | ||
return INTShimTcpUdp | ||
return Packet.guess_payload_class(self, payload) | ||
|
||
|
||
class UDP(Packet): | ||
name = "UDP" | ||
|
@@ -862,6 +871,15 @@ def mysummary(self): | |
else: | ||
return self.sprintf("UDP %UDP.sport% > %UDP.dport%") | ||
|
||
def guess_payload_class(self, payload): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
from scapy.contrib.int import INTShimTcpUdp, INT_PRI_MASK | ||
if ((isinstance(self.underlayer, IP) and | ||
((self.underlayer.tos & INT_PRI_MASK) == INT_PRI_MASK)) or | ||
(isinstance(self.underlayer, scapy.layers.inet6.IPv6) and | ||
((self.underlayer.tc & INT_PRI_MASK) == INT_PRI_MASK))): | ||
return INTShimTcpUdp | ||
return Packet.guess_payload_class(self, payload) | ||
|
||
|
||
icmptypes = {0: "echo-reply", | ||
3: "dest-unreach", | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its not a good idea to import your contrib layer in guess_payload_class of TCP. Can you maybe move this port to dispatch_hook?