# How to embed Ghost Thread feature in P4 code

**URL:** <https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398>\
**Category:** Getting Started with P4\
**Created:** [November 9, 2025, 8:58am UTC](https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398 "2025-11-09T08:58:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![herano1999](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@herano1999](https://forum.p4.org/u/herano1999)\
**Post date:** [November 9, 2025, 8:58am UTC](https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398/1 "2025-11-09T08:58:47Z")

</div>

Hi!

Recently, I’ve been struggling to implement Bolt (NSDI ‘23). It’s quite frustrating that I cannot find any reference code except sfc\_ghost.p4, sfc\_trigger.p4 (under [open-p4studio/pkgsrc/switch-p4-16/p4src/shared at main · p4lang/open-p4studio · GitHub](https://github.com/p4lang/open-p4studio/blob/main/pkgsrc/switch-p4-16/p4src/shared) ), switch\_sfc\_signal.py, and switch\_sfc\_trigger.py (under [open-p4studio/pkgsrc/switch-p4-16/ptf/api at main · p4lang/open-p4studio · GitHub](https://github.com/p4lang/open-p4studio/blob/main/pkgsrc/switch-p4-16/ptf/api)). I’m confused about the differences between these codes and how to embed the P4 code into my code. Could someone give me a hint?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![p4prof](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/p4prof/32/251_2.png) [@p4prof](https://forum.p4.org/u/p4prof)\
**Post date:** [November 11, 2025, 7:28am UTC](https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398/2 "2025-11-11T07:28:37Z")

</div>

Dear @herano1999 ,

The difference between the files you are referencing is quite simple: .p4 files are the data plane code, and .py files are PTF tests for the corresponding feature.

In terms of integrating that code into your program, you should probably look at it as an example, no more and then implement whatever you need. The code you are referring to was written specifically for the big data plane program, called switch.p4 and makes a lot of assumptions about it, not to mention that implements certain specific functionality as was envisioned by the program designers (or requested by some Tofino customers).

I would also recommend reaching out to the paper authors.

Happy hacking,  
Vladimir

---

<div class="post-metadata">

**Author:** ![herano1999](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@herano1999](https://forum.p4.org/u/herano1999)\
**Post date:** [November 12, 2025, 12:12pm UTC](https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398/3 "2025-11-12T12:12:53Z")

</div>

Hi @p4prof ,

Thanks for your reply. After checking the [tofino2\_arch.p4](https://github.com/p4lang/p4c/blob/main/backends/tofino/bf-p4c/p4include/tofino2_arch.p4), I think at the dataplane, I should leverage the `gh_intr_md` in `IngressT` to get the queue length at the egress. But I’m not sure about it, because there is another control block `GhostT`. However, this control block has no `out` data, so I’m wondering why this control block was designed this way.

For the previous question, I’m sorry I didn’t explain the question clearly. I want to know the difference between the two P4 codes and the two Python codes. After diving into these codes, I think the two Python codes are testing different features, and the P4 codes both implement the SFC. Though I still cannot find where the relative control blocks (such as `IngressSfcPrepare`, `IngressSfcTrigger` and so on) are called, I think it isn’t a matter if I can directly leverage the `gh_intr_md`.

Thanks!

---

<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:** [November 12, 2025, 6:23pm UTC](https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398/4 "2025-11-12T18:23:04Z")

</div>

Vladimir knows this much better than I do, but I might be able to convey the basic idea correctly.

The “ghost” thread runs in the ingress pipeline, and runs along side of ingress packet processing, and it has access to some ingress resources. It is not intended to have any output packet, but to cause changes to the state of ingress resources, typically by writing to one or more P4 register arrays, that the normal packet ingress processing code can then read.

So for example, the ghost thread might write queue depth information to a P4 register array, that ingress packet processing might then read and use to affect how ingress packets are processed.

---

<div class="post-metadata">

**Author:** ![herano1999](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@herano1999](https://forum.p4.org/u/herano1999)\
**Post date:** [November 13, 2025, 2:30am UTC](https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398/5 "2025-11-13T02:30:58Z")

</div>

Hi, and thank you @andyfingerhut! It sounds reasonable to me. So why is there another control block `GhostT`. This control block only contains one input `gh_intr_md`, and seems to overlap with the input from Ingress. I can envision two possible implementation approaches:

- Leverage the `gh_intr_md` at ingress directly.
- Or send CPU packets by the `GhostT` block to modify the control plane. Read the relevant table at ingress.

Which approach is correct (or both OK/not OK)? The second approach may be somewhat complicated to implement, and I suspect it will introduce overhead.

---

<div class="post-metadata">

**Author:** ![p4prof](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/p4prof/32/251_2.png) [@p4prof](https://forum.p4.org/u/p4prof)\
**Post date:** [November 13, 2025, 10:02am UTC](https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398/6 "2025-11-13T10:02:51Z")

</div>

@herano1999 ,

@andyfingerhut is correct – the ghost thread is a separate control that can be included in a T2NA switch package. It runs independently in the ingress pipeline and GhostT is the control that describes the processing that can occur there. As you can see [in the definition](https://github.com/p4lang/p4c/blob/17ade03c106793ef0d3cd15a3f789de2760ed486/backends/tofino/bf-p4c/p4include/tofino2_arch.p4#L65), there is not much there, just a single input argument, meaning that you cannot send a packet or anything.

What you **can** do is you can instantiate a `Register()` extern at the **top level** (i.e., outside any control) and then use the Ghost control to store values there and let the Ingress control to read these values and do whatever it needs.

That’s how it works. The ghost thread can do a little bit of additional processing, but not much, really. The file you pointed to (`sfc_ghost.p4`) is a pretty good example, actually.

Happy hacking,  
Vladimir

---

<div class="post-metadata">

**Author:** ![herano1999](https://avatars.discourse-cdn.com/v4/letter/h/b9bd4f/32.png) [@herano1999](https://forum.p4.org/u/herano1999)\
**Post date:** [November 13, 2025, 11:26am UTC](https://forum.p4.org/t/how-to-embed-ghost-thread-feature-in-p4-code/1398/7 "2025-11-13T11:26:06Z")

</div>

I think I’m starting to get it. I will try to implement. Thank you for your help @p4prof @andyfingerhut !

Cheers!
