# How to add and remove a protocol header in a packet？

**URL:** <https://forum.p4.org/t/how-to-add-and-remove-a-protocol-header-in-a-packet/640>\
**Category:** Uncategorized\
**Created:** [February 13, 2023, 4:29pm UTC](https://forum.p4.org/t/how-to-add-and-remove-a-protocol-header-in-a-packet/640 "2023-02-13T16:29:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duang](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Duang](https://forum.p4.org/u/Duang)\
**Post date:** [February 13, 2023, 4:29pm UTC](https://forum.p4.org/t/how-to-add-and-remove-a-protocol-header-in-a-packet/640/1 "2023-02-13T16:29:25Z")

</div>

I want to delete the IP header in a data packet and add a custom network layer header, how can I do it? For example, a data packet header contains mac, ipv4, tcp, and now I want to delete the ipv4 header and replace it with a custom header sip, and the final output header format is mac, sip, tcp.

---

<div class="post-metadata">

**Author:** ![ederollora](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/ederollora/32/21_2.png) [@ederollora](https://forum.p4.org/u/ederollora)\
**Post date:** [February 13, 2023, 6:53pm UTC](https://forum.p4.org/t/how-to-add-and-remove-a-protocol-header-in-a-packet/640/2 "2023-02-13T18:53:59Z")

</div>

Hi @Duang ,

Welcome to the forum.

Removing a header is as easy as:

```auto
hdr.ipv4.setInvalid();

```

If you want to add another header, you just `setValid()` that header, that you needed to define beforehand and then add the values to the fields. Make sure you also `emit` the new header as depicted below:

```auto

header new_l3_header_t {
    addr_t dstAddr;
}

struct headers {
    ipv4_t ipv4;
    new_l3_header_t new_l3_header;
}

//define yourself the parser

control Whatevergress (inout headers hdr,
                  inout metadata meta,
                  inout standard_metadata_t standard_metadata){

    apply(){

        hdr.ipv4.setInvalid();
        hdr.new_l3_header.setValid();
        hdr.new_l3_header.dstAddr = (addr_t) some_value;

    }
}

// Deparse the packet

control YourDeparser(packet_out packet, 
                                   in headers hdr) {
    apply {
        // Other headers
        packet.emit(hdr.ipv4);
        packet.emit(hdr.new_l3_header);
    }
}

```

You can learn these concepts if you try all the exercises from the P4 tutorial: [GitHub - p4lang/tutorials: P4 language tutorials](https://github.com/p4lang/tutorials)

Cheers,

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [February 13, 2023, 8:43pm UTC](https://forum.p4.org/t/how-to-add-and-remove-a-protocol-header-in-a-packet/640/3 "2023-02-13T20:43:08Z")

</div>

One minor additional point, but not only must you call setValid() on a header in order to add it, but it must also appear in your deparser code with an emit call. Doing setValid() on a header that is NOT emit’ed in the deparser will NOT cause it to appear in the output packet.

---

<div class="post-metadata">

**Author:** ![ederollora](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/ederollora/32/21_2.png) [@ederollora](https://forum.p4.org/u/ederollora)\
**Post date:** [February 13, 2023, 8:55pm UTC](https://forum.p4.org/t/how-to-add-and-remove-a-protocol-header-in-a-packet/640/4 "2023-02-13T20:55:15Z")

</div>

Modified my answer. Thanks @andyfingerhut🙏 🙂

---

<div class="post-metadata">

**Author:** ![Duang](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Duang](https://forum.p4.org/u/Duang)\
**Post date:** [February 14, 2023, 2:02am UTC](https://forum.p4.org/t/how-to-add-and-remove-a-protocol-header-in-a-packet/640/5 "2023-02-14T02:02:41Z")

</div>

Thank you for your help, it worked great for me.

---

<div class="post-metadata">

**Author:** ![Duang](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Duang](https://forum.p4.org/u/Duang)\
**Post date:** [February 14, 2023, 2:03am UTC](https://forum.p4.org/t/how-to-add-and-remove-a-protocol-header-in-a-packet/640/6 "2023-02-14T02:03:35Z")

</div>

Thanks a lot for your advice and help。
