Commit b32b4a5f authored by Yonghong Song's avatar Yonghong Song

add padding to the end of structure

Commit 538a84e1 ("provide padded structure for
table_{key|leaf}_desc API") added explicit padding, if needed,
before every structure member.

This is not enough as if an array of elements are returned
from C++ to python side, the size of structure must be correct.

This patch also adds padding after the last structure field
to make structure size in python side consistent with C++
side. With this patch, I experimented with the pull request
for tcpretrans.py by Matthias Tafelmeier, the type
  struct ipv6_flow_key_t {
      unsigned __int128 saddr;
      unsigned __int128 daddr;
      u64 lport;
      u64 dport;
  }
can be replaced with
  struct ipv6_flow_key_t {
      unsigned __int128 saddr;
      unsigned __int128 daddr;
      u16 lport;
      u16 dport;
  }
where the original type of lport/dport is u16.
Some other ipv6 related data structures can also be simplified.
Signed-off-by: default avatarYonghong Song <yhs@fb.com>
parent b384b76e
...@@ -145,6 +145,13 @@ bool BMapDeclVisitor::VisitRecordDecl(RecordDecl *D) { ...@@ -145,6 +145,13 @@ bool BMapDeclVisitor::VisitRecordDecl(RecordDecl *D) {
Offset = FieldOffset + FieldSize; Offset = FieldOffset + FieldSize;
genJSONForField(F); genJSONForField(F);
} }
/* Additional Padding after the last field so that the Record Size matches */
CharUnits RecordSize = Layout.getSize();
if (RecordSize > Offset) {
result_ += "[\"__pad_end\",\"char\",["
+ to_string((RecordSize - Offset).getQuantity()) + "]], ";
}
} }
if (!D->getDefinition()->field_empty()) if (!D->getDefinition()->field_empty())
......
...@@ -685,6 +685,7 @@ struct key_t { ...@@ -685,6 +685,7 @@ struct key_t {
}; };
u8 f1_3; /* offset 48 */ u8 f1_3; /* offset 48 */
unsigned __int128 f1_4; /* offset 64 */ unsigned __int128 f1_4; /* offset 64 */
char f1_5; /* offset 80 */
}; };
struct value_t { struct value_t {
u8 src[4] __attribute__ ((aligned (8))); /* offset 0 */ u8 src[4] __attribute__ ((aligned (8))); /* offset 0 */
...@@ -694,8 +695,8 @@ BPF_HASH(table1, struct key_t, struct value_t); ...@@ -694,8 +695,8 @@ BPF_HASH(table1, struct key_t, struct value_t);
""" """
b = BPF(text=text) b = BPF(text=text)
table = b['table1'] table = b['table1']
self.assertEqual(ct.sizeof(table.Key), 80) self.assertEqual(ct.sizeof(table.Key), 96)
self.assertEqual(ct.sizeof(table.Leaf), 12) self.assertEqual(ct.sizeof(table.Leaf), 16)
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment