Inheritance diagram for Packet:

Click's Packet class represents network packets within a router. Packet objects are passed from Element to Element via the Element::push() and Element::pull() functions. The vast majority of elements handle packets.
A packet consists of a data buffer, which stores the actual packet wire data, and a set of annotations, which store extra information calculated about the packet, such as the destination address to be used for routing. Every Packet object has different annotations, but a data buffer may be shared among multiple Packet objects, saving memory and speeding up packet copies. (See Packet::clone.) As a result a Packet's data buffer is not writable. To write into a packet, turn it into a nonshared WritablePacket first, using uniqueify(), push(), or put().
A packet's data buffer is a single flat array of bytes. The buffer may be larger than the actual packet data, leaving unused spaces called headroom and tailroom before and after the data proper. Prepending headers or appending data to a packet can be quite efficient if there is enough headroom or tailroom.
The relationships among a Packet object's data buffer variables is shown here:
data() end_data()
| |
|<- headroom() ->|<----- length() ----->|<- tailroom() ->|
| v v |
+================+======================+================+
|XXXXXXXXXXXXXXXX| PACKET CONTENTS |XXXXXXXXXXXXXXXX|
+================+======================+================+
^ ^
|<------------------ buffer_length() ------------------->|
| |
buffer() end_buffer()
Most code that manipulates packets is interested only in data() and length().
To create a Packet, call one of the make() functions. To destroy a Packet, call kill(). To clone a Packet, which creates a new Packet object that shares this packet's data, call clone(). To uniqueify a Packet, which unshares the packet data if necessary, call uniqueify(). To allocate extra space for headers or trailers, call push() and put(). To remove headers or trailers, call pull() and take().
data() end_data()
| |
push() | pull() take() | put()
<======= | =======> <======= | =======>
v v
+===========+================================+===========+
|XXXXXXXXXXX| PACKET CONTENTS |XXXXXXXXXXX|
+===========+================================+===========+
Packet objects are implemented in different ways in different drivers. The userlevel driver has its own C++ implementation. In the linuxmodule driver, however, Packet is an overlay on Linux's native sk_buff object: the Packet methods access underlying sk_buff data directly, with no overhead. (For example, Packet::data() returns the sk_buff's data field.)
Annotations are extra information about a packet above and beyond the packet data. Packet supports several specific annotations, plus a user annotation area available for arbitrary use by elements.
New packets start wth all annotations set to zero or null. Cloning a packet copies its annotations.
Data | |
| enum | { default_headroom = 28, min_buffer_length = 64 } |
| static WritablePacket * | make (uint32_t headroom, const void *data, uint32_t length, uint32_t tailroom) |
| Create and return a new packet. | |
| static WritablePacket * | make (const void *data, uint32_t length) |
| Create and return a new packet. | |
| static WritablePacket * | make (uint32_t length) |
| Create and return a new packet. | |
| static Packet * | make (struct sk_buff *skb) |
| Change an sk_buff into a Packet (linuxmodule). | |
| static WritablePacket * | make (unsigned char *data, uint32_t length, void(*destructor)(unsigned char *, size_t)) |
| Create and return a new packet (userlevel). | |
| void | kill () |
| Delete this packet. | |
| bool | shared () const |
| Test whether this packet's data is shared. | |
| Packet * | clone () |
| Create a clone of this packet. | |
| WritablePacket * | uniqueify () |
| Return an unshared packet containing this packet's data. | |
| const unsigned char * | data () const |
| Return the packet's data pointer. | |
| const unsigned char * | end_data () const |
| Return the packet's end data pointer. | |
| uint32_t | length () const |
| Return the packet's length. | |
| uint32_t | headroom () const |
| Return the packet's headroom. | |
| uint32_t | tailroom () const |
| Return the packet's tailroom. | |
| const unsigned char * | buffer () const |
| Return a pointer to the packet's data buffer. | |
| const unsigned char * | end_buffer () const |
| Return the packet's end data buffer pointer. | |
| uint32_t | buffer_length () const |
| Return the packet's buffer length. | |
| sk_buff * | skb () |
| sk_buff * | skb () const |
| WritablePacket * | push (uint32_t len) |
| Add space for a header before the packet. | |
| WritablePacket * | push_mac_header (uint32_t len) |
| Add space for a MAC header before the packet. | |
| Packet * | nonunique_push (uint32_t len) |
| Add space for a header before the packet. | |
| void | pull (uint32_t len) |
| Remove a header from the front of the packet. | |
| WritablePacket * | put (uint32_t len) |
| Add space for data after the packet. | |
| Packet * | nonunique_put (uint32_t len) |
| Add space for data after the packet. | |
| void | take (uint32_t len) |
| Remove space from the end of the packet. | |
| Packet * | shift_data (int offset, bool free_on_failure=true) |
| Shift packet data within the data buffer. | |
| void | shrink_data (const unsigned char *data, uint32_t length) |
| Shrink the packet's data. | |
| void | change_headroom_and_length (uint32_t headroom, uint32_t length) |
| Shift the packet's data view to a different part of its buffer. | |
Annotations | |
| enum | { anno_size = 48 } |
| enum | PacketType { HOST = 0, BROADCAST = 1, MULTICAST = 2, OTHERHOST = 3, OUTGOING = 4, LOOPBACK = 5, FASTROUTE = 6 } |
| Values for packet_type_anno(). Must agree with Linux's PACKET_ constants in <linux/if_packet.h>. More... | |
| enum | { dst_ip_anno_offset = 0, dst_ip_anno_size = 4, dst_ip6_anno_offset = 0, dst_ip6_anno_size = 16 } |
| const Timestamp & | timestamp_anno () const |
| Return the timestamp annotation. | |
| Timestamp & | timestamp_anno () |
| void | set_timestamp_anno (const Timestamp &t) |
| Set the timestamp annotation. | |
| net_device * | device_anno () const |
| Return the device annotation. | |
| void | set_device_anno (net_device *dev) |
| Set the device annotation. | |
| PacketType | packet_type_anno () const |
| Return the packet type annotation. | |
| void | set_packet_type_anno (PacketType t) |
| Set the packet type annotation. | |
| Packet * | next () const |
| Return the next packet annotation. | |
| Packet *& | next () |
| void | set_next (Packet *p) |
| Set the next packet annotation. | |
| Packet * | prev () const |
| Return the previous packet annotation. | |
| Packet *& | prev () |
| void | set_prev (Packet *p) |
| Set the previous packet annotation. | |
| IPAddress | dst_ip_anno () const |
| Return the destination IPv4 address annotation. | |
| void | set_dst_ip_anno (IPAddress addr) |
| Set the destination IPv4 address annotation. | |
| void * | anno () |
| Return a pointer to the annotation area. | |
| const void * | anno () const |
| uint8_t * | anno_u8 () |
| Return a pointer to the annotation area as uint8_ts. | |
| const uint8_t * | anno_u8 () const |
| overload | |
| uint32_t * | anno_u32 () |
| Return a pointer to the annotation area as uint32_ts. | |
| const uint32_t * | anno_u32 () const |
| overload | |
| uint8_t | anno_u8 (int i) const |
| Return annotation byte at offset i. | |
| void | set_anno_u8 (int i, uint8_t v) |
| Set annotation byte at offset i. | |
| uint16_t | anno_u16 (int i) const |
| Return 16-bit annotation at offset i. | |
| void | set_anno_u16 (int i, uint16_t v) |
| Set 16-bit annotation at offset i. | |
| int16_t | anno_s16 (int i) const |
| Return 16-bit annotation at offset i. | |
| void | set_anno_s16 (int i, int16_t v) |
| Set 16-bit annotation at offset i. | |
| uint32_t | anno_u32 (int i) const |
| Return 32-bit annotation at offset i. | |
| void | set_anno_u32 (int i, uint32_t v) |
| Set 32-bit annotation at offset i. | |
| int32_t | anno_s32 (int i) const |
| Return 32-bit annotation at offset i. | |
| void | set_anno_s32 (int i, int32_t v) |
| Set 32-bit annotation at offset i. | |
| uint64_t | anno_u64 (int i) const |
| Return 64-bit annotation at offset i. | |
| void | set_anno_u64 (int i, uint64_t v) |
| Set 64-bit annotation at offset i. | |
| void | clear_annotations (bool all=true) |
| Clear all packet annotations. | |
| void | copy_annotations (const Packet *) |
| Copy most packet annotations from p. | |
Header Pointers | |
| bool | has_mac_header () const |
| Return true iff the packet's MAC header pointer is set. | |
| const unsigned char * | mac_header () const |
| Return the packet's MAC header pointer. | |
| int | mac_header_offset () const |
| Return the offset from the packet data to the MAC header. | |
| uint32_t | mac_header_length () const |
| Return the MAC header length. | |
| int | mac_length () const |
| Return the packet's length starting from its MAC header pointer. | |
| void | set_mac_header (const unsigned char *p) |
| Set the MAC header pointer. | |
| void | set_mac_header (const unsigned char *p, uint32_t len) |
| Set the MAC and network header pointers. | |
| void | clear_mac_header () |
| Unset the MAC header pointer. | |
| bool | has_network_header () const |
| Return true iff the packet's network header pointer is set. | |
| const unsigned char * | network_header () const |
| Return the packet's network header pointer. | |
| int | network_header_offset () const |
| Return the offset from the packet data to the network header. | |
| uint32_t | network_header_length () const |
| Return the network header length. | |
| int | network_length () const |
| Return the packet's length starting from its network header pointer. | |
| void | set_network_header (const unsigned char *p, uint32_t len) |
| Set the network and transport header pointers. | |
| void | set_network_header_length (uint32_t len) |
| Set the network header length. | |
| void | clear_network_header () |
| Unset the network header pointer. | |
| bool | has_transport_header () const |
| Return true iff the packet's network header pointer is set. | |
| const unsigned char * | transport_header () const |
| Return the packet's transport header pointer. | |
| int | transport_header_offset () const |
| Return the offset from the packet data to the transport header. | |
| int | transport_length () const |
| Return the packet's length starting from its transport header pointer. | |
| void | clear_transport_header () |
| Unset the transport header pointer. | |
| const click_ether * | ether_header () const |
| Return the packet's MAC header pointer as Ethernet. | |
| void | set_ether_header (const click_ether *ethh) |
| Set the MAC header pointer to an Ethernet header. | |
| const click_ip * | ip_header () const |
| Return the packet's network header pointer as IPv4. | |
| int | ip_header_offset () const |
| Return the offset from the packet data to the IP header. | |
| uint32_t | ip_header_length () const |
| Return the IP header length. | |
| void | set_ip_header (const click_ip *iph, uint32_t len) |
| Set the network header pointer to an IPv4 header. | |
| const click_ip6 * | ip6_header () const |
| Return the packet's network header pointer as IPv6. | |
| int | ip6_header_offset () const |
| Return the offset from the packet data to the IPv6 header. | |
| uint32_t | ip6_header_length () const |
| Return the IPv6 header length. | |
| void | set_ip6_header (const click_ip6 *ip6h) |
| Set the network header pointer to an IPv6 header. | |
| void | set_ip6_header (const click_ip6 *ip6h, uint32_t len) |
| Set the network header pointer to an IPv6 header. | |
| const click_icmp * | icmp_header () const |
| Return the packet's transport header pointer as ICMP. | |
| const click_tcp * | tcp_header () const |
| Return the packet's transport header pointer as TCP. | |
| const click_udp * | udp_header () const |
| Return the packet's transport header pointer as UDP. | |
| anonymous enum |
| default_headroom | Default packet headroom() for Packet::make(). 4-byte aligned. |
| min_buffer_length | Minimum buffer_length() for Packet::make() |
| enum Packet::PacketType |
Values for packet_type_anno(). Must agree with Linux's PACKET_ constants in <linux/if_packet.h>.
| HOST | Packet was sent to this host. |
| BROADCAST | Packet was sent to a link-level multicast address. |
| MULTICAST | Packet was sent to a link-level multicast address. |
| OTHERHOST | Packet was sent to a different host, but received anyway. The receiving device is probably in promiscuous mode. |
| OUTGOING | Packet was generated by this host and is being sent elsewhere. |
| WritablePacket * Packet::make | ( | uint32_t | headroom, | |
| const void * | data, | |||
| uint32_t | length, | |||
| uint32_t | tailroom | |||
| ) | [static] |
Create and return a new packet.
| headroom | headroom in new packet | |
| data | data to be copied into the new packet | |
| length | length of packet | |
| tailroom | tailroom in new packet |
The new packet's annotations are cleared and its header pointers are null.
| WritablePacket * Packet::make | ( | const void * | data, | |
| uint32_t | length | |||
| ) | [inline, static] |
Create and return a new packet.
| data | data to be copied into the new packet | |
| length | length of packet |
The returned packet's annotations are cleared and its header pointers are null.
| WritablePacket * Packet::make | ( | uint32_t | length | ) | [inline, static] |
Create and return a new packet.
| length | length of packet |
The returned packet's annotations are cleared and its header pointers are null.
| Packet * Packet::make | ( | struct sk_buff * | skb | ) | [inline, static] |
Change an sk_buff into a Packet (linuxmodule).
| skb | input sk_buff |
skb->users is 1, then skb is orphaned by skb_orphan(skb) and returned. If it is larger than 1, then skb is cloned and the clone is returned. (sk_buffs used for Click Packet objects must have skb->users == 1.) Null might be returned if there's no memory for the clone.The returned packet's annotations and header pointers are not cleared: they have the same values they did in the sk_buff. If the packet came from Linux, then the header pointers and shared annotations (timestamp, packet type, next/prev packet) might have valid values, but the Click annotations (address, user) likely do not. Use clear_annotations() to clear them.
| WritablePacket * Packet::make | ( | unsigned char * | data, | |
| uint32_t | length, | |||
| void(*)(unsigned char *, size_t) | destructor | |||
| ) | [static] |
Create and return a new packet (userlevel).
| data | data used in the new packet | |
| length | length of packet | |
| destructor | destructor function |
delete[] data.) The packet has zero headroom and tailroom.The returned packet's annotations are cleared and its header pointers are null.
| void Packet::kill | ( | ) | [inline] |
Delete this packet.
The packet header (including annotations) is destroyed and its memory returned to the system. The packet's data is also freed if this is the last clone.
| bool Packet::shared | ( | ) | const [inline] |
Test whether this packet's data is shared.
Returns true iff the packet's data is shared. If shared() is false, then the result of uniqueify() will equal this.
| Packet * Packet::clone | ( | ) |
Create a clone of this packet.
| WritablePacket * Packet::uniqueify | ( | ) | [inline] |
Return an unshared packet containing this packet's data.
The returned WritablePacket pointer may not equal the input Packet pointer, so do not use the input pointer after the uniqueify() call.
The input packet's headroom and tailroom areas are copied in addition to its true contents. The header annotations are shifted to point into the new packet data if necessary.
uniqueify() is usually used like this:
WritablePacket *q = p->uniqueify(); if (!q) return 0; // p must not be used here.
| const unsigned char * Packet::data | ( | ) | const [inline] |
Return the packet's data pointer.
This is the pointer to the first byte of packet data.
Reimplemented in WritablePacket.
| const unsigned char * Packet::end_data | ( | ) | const [inline] |
Return the packet's end data pointer.
The result points at the byte following the packet data.
Reimplemented in WritablePacket.
| uint32_t Packet::length | ( | ) | const [inline] |
Return the packet's length.
| uint32_t Packet::headroom | ( | ) | const [inline] |
Return the packet's headroom.
The headroom is the amount of space available in the current packet buffer before data(). A push() operation is cheap if the packet's unshared and the length pushed is less than headroom().
| uint32_t Packet::tailroom | ( | ) | const [inline] |
Return the packet's tailroom.
The tailroom is the amount of space available in the current packet buffer following end_data(). A put() operation is cheap if the packet's unshared and the length put is less than tailroom().
| const unsigned char * Packet::buffer | ( | ) | const [inline] |
Return a pointer to the packet's data buffer.
The result points at the packet's headroom, not its data.
Reimplemented in WritablePacket.
| const unsigned char * Packet::end_buffer | ( | ) | const [inline] |
Return the packet's end data buffer pointer.
The result points past the packet's tailroom.
Reimplemented in WritablePacket.
| uint32_t Packet::buffer_length | ( | ) | const [inline] |
| WritablePacket * Packet::push | ( | uint32_t | len | ) | [inline] |
Add space for a header before the packet.
| len | amount of space to add |
If a data copy would be required, but the copy fails because of lack of memory, then the current packet is freed.
push() is usually used like this:
WritablePacket *q = p->push(14); if (!q) return 0; // p must not be used here.
| WritablePacket * Packet::push_mac_header | ( | uint32_t | len | ) | [inline] |
Add space for a MAC header before the packet.
| len | amount of space to add and length of MAC header |
| Packet * Packet::nonunique_push | ( | uint32_t | len | ) | [inline] |
Add space for a header before the packet.
| len | amount of space to add |
If a data copy would be required, but the copy fails because of lack of memory, then the current packet is freed.
| void Packet::pull | ( | uint32_t | len | ) | [inline] |
Remove a header from the front of the packet.
| len | amount of space to remove |
It is an error to attempt to pull more than length() bytes.
| WritablePacket * Packet::put | ( | uint32_t | len | ) | [inline] |
Add space for data after the packet.
| len | amount of space to add |
If a data copy would be required, but the copy fails because of lack of memory, then the current packet is freed.
put() is usually used like this:
WritablePacket *q = p->put(100); if (!q) return 0; // p must not be used here.
| Packet * Packet::nonunique_put | ( | uint32_t | len | ) | [inline] |
Add space for data after the packet.
| len | amount of space to add |
If a data copy would be required, but the copy fails because of lack of memory, then the current packet is freed.
| void Packet::take | ( | uint32_t | len | ) | [inline] |
Remove space from the end of the packet.
| len | amount of space to remove |
It is an error to attempt to pull more than length() bytes.
new end_data() == old end_data() - len
| Packet * Packet::shift_data | ( | int | offset, | |
| bool | free_on_failure = true | |||
| ) |
Shift packet data within the data buffer.
| offset | amount to shift packet data | |
| free_on_failure | if true, then delete the input packet on failure |
If the packet is shared() or there isn't enough headroom or tailroom for the operation, the packet is passed to uniqueify() first. This can fail if there isn't enough memory. If it fails, shift_data returns null, and if free_on_failure is true (the default), the input packet is freed.
The packet's mac_header, network_header, and transport_header areas are preserved, even if they lie within the headroom. Any headroom outside these regions may be overwritten, as may any tailroom.
| void Packet::shrink_data | ( | const unsigned char * | data, | |
| uint32_t | length | |||
| ) | [inline] |
Shrink the packet's data.
| data | new data pointer | |
| length | new length |
shrink_data() removes all of a packet's headroom and tailroom. The resulting packet has data() equal to data, length() equal to length, and headroom() and tailroom() equal to zero.
data >= data(), data <= end_data(), data + length >= data(), and data + length <= end_data()
| void Packet::change_headroom_and_length | ( | uint32_t | headroom, | |
| uint32_t | length | |||
| ) | [inline] |
Shift the packet's data view to a different part of its buffer.
| headroom | new headroom | |
| length | new length |
new end_buffer() == old end_buffer()
new headroom() == headroom
new length() == length
| bool Packet::has_mac_header | ( | ) | const [inline] |
| const unsigned char * Packet::mac_header | ( | ) | const [inline] |
Return the packet's MAC header pointer.
Reimplemented in WritablePacket.
| int Packet::mac_header_offset | ( | ) | const [inline] |
Return the offset from the packet data to the MAC header.
| uint32_t Packet::mac_header_length | ( | ) | const [inline] |
Return the MAC header length.
| int Packet::mac_length | ( | ) | const [inline] |
Return the packet's length starting from its MAC header pointer.
| void Packet::set_mac_header | ( | const unsigned char * | p | ) | [inline] |
Set the MAC header pointer.
| p | new header pointer |
| void Packet::set_mac_header | ( | const unsigned char * | p, | |
| uint32_t | len | |||
| ) | [inline] |
Set the MAC and network header pointers.
| p | new MAC header pointer | |
| len | new MAC header length |
| void Packet::clear_mac_header | ( | ) | [inline] |
Unset the MAC header pointer.
| bool Packet::has_network_header | ( | ) | const [inline] |
Return true iff the packet's network header pointer is set.
| const unsigned char * Packet::network_header | ( | ) | const [inline] |
Return the packet's network header pointer.
Reimplemented in WritablePacket.
| int Packet::network_header_offset | ( | ) | const [inline] |
Return the offset from the packet data to the network header.
| uint32_t Packet::network_header_length | ( | ) | const [inline] |
Return the network header length.
| int Packet::network_length | ( | ) | const [inline] |
Return the packet's length starting from its network header pointer.
| void Packet::set_network_header | ( | const unsigned char * | p, | |
| uint32_t | len | |||
| ) | [inline] |
Set the network and transport header pointers.
| p | new network header pointer | |
| len | new network header length |
| void Packet::set_network_header_length | ( | uint32_t | len | ) | [inline] |
Set the network header length.
| len | new network header length |
| void Packet::clear_network_header | ( | ) | [inline] |
Unset the network header pointer.
| bool Packet::has_transport_header | ( | ) | const [inline] |
Return true iff the packet's network header pointer is set.
| const unsigned char * Packet::transport_header | ( | ) | const [inline] |
Return the packet's transport header pointer.
Reimplemented in WritablePacket.
| int Packet::transport_header_offset | ( | ) | const [inline] |
Return the offset from the packet data to the transport header.
| int Packet::transport_length | ( | ) | const [inline] |
Return the packet's length starting from its transport header pointer.
| void Packet::clear_transport_header | ( | ) | [inline] |
Unset the transport header pointer.
| const click_ether * Packet::ether_header | ( | ) |