# grpc — Microservice RPCs & Router gNMI Streams (TCP 50051) (`grpc`)

> **FrameAssembly v3.2.2** · Protocol 48 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/grpc

- **Full Protocol Name:** gRPC Remote Procedure Calls over HTTP/2
- **Keyword:** `grpc`
- **OSI Layer & Transport:** Layer 7 · TCP 50051
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** gRPC over HTTP/2, RFC 9113
- **Wireshark Display Filter:** `grpc || http2`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 50051 -> gRPC`

## Overview

Whether it is Kubernetes east-west microservices or Arista/Cisco `gnmi.gNMI/Subscribe` streaming telemetry on TCP `50051`, `grpc` builds the length-prefixed Protobuf envelope and `grpc-status` trailers.

gRPC (`grpc`) runs Remote Procedure Calls over HTTP/2 on **TCP port `50051`** (and network telemetry ports such as `57400`/`9339` for gNMI). On the wire, a gRPC call encodes `:path /<service>/<method>` with `content-type: application/grpc` inside HTTP/2 `HEADERS`, followed by a 5-byte length-prefixed gRPC `DATA` frame (`1-byte compressed flag` + `4-byte message length` + Protobuf payload), and finishes with an HTTP/2 trailer carrying `grpc-status`.

FrameAssembly builds valid HTTP/2 + gRPC request frames from `service` and `method` names, or emits a gRPC response trailer when you specify `grpcstatus` (`0` OK, `7` PERMISSION_DENIED, `14` UNAVAILABLE).

## Minimal Quickstart (`grpc_quickstart.frasm`)

```frameassembly
host caller  { ip 10.42.0.15 }
host backend { ip 10.42.0.50 }

compile {
    caller -> backend grpc service "inventory.StockService" method "CheckItem" wait 2ms
    caller <- backend grpc grpcstatus 0
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `50051` (`psh ack`) and formats HTTP/2 `HEADERS` (`:method POST`, `:path /<service>/<method>`, `content-type: application/grpc`).
- Wraps request payloads in the 5-byte gRPC Length-Prefixed-Message header (`Compressed-Flag = 0` + 32-bit big-endian message length).
- Emits an HTTP/2 `END_STREAM | END_HEADERS` trailer frame carrying `grpc-status: <code_num>` whenever `grpcstatus` is set.

## Practical Lab Example: gNMI Router Telemetry Subscription + Backend UNAVAILABLE Failover (`grpc_service_mesh_lab.frasm`)

Opens a TCP 50051 connection, invokes a `telemetry.Collector/Export` RPC succeeding with `grpcstatus 0`, and captures a second call returning `grpcstatus 14` (`UNAVAILABLE`).

```frameassembly
host edge_agent { ip 172.16.40.12 }
host mesh_gw    { ip 172.16.40.50 }

compile {
    edge_agent -> mesh_gw tcp syn srcport 55051 dstport 50051 win 65535 mss 1460 wait 1ms
    edge_agent <- mesh_gw tcp syn ack win 65535 mss 1460 wait 1ms
    edge_agent -> mesh_gw tcp ack wait 1ms
    // 1. Healthy gRPC Export call -> OK (0)
    edge_agent -> mesh_gw grpc service "telemetry.Collector" method "Export" wait 2ms
    edge_agent <- mesh_gw grpc grpcstatus 0 wait 4ms
    // 2. Upstream maintenance -> UNAVAILABLE (14)
    edge_agent -> mesh_gw grpc service "auth.TokenVerifier" method "Validate" wait 2ms
    edge_agent <- mesh_gw grpc grpcstatus 14
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show grpc_service_mesh_lab.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly grpc_service_mesh_lab.frasm grpc_service_mesh_lab.pcap
frameassembly grpc_service_mesh_lab.frasm grpc_service_mesh_lab.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse grpc_service_mesh_lab.pcap grpc_service_mesh_lab_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r grpc_service_mesh_lab.pcap
```

## All `grpc` Keywords & Header Options

Packet line pattern: `src_host -> dst_host grpc [service "<text>"] [method "<text>"] [grpcstatus <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `service "<text>"` | Package-qualified gRPC service name | `"helloworld.Greeter"` | Sets the service prefix in the HTTP/2 `:path` pseudo-header (`/<service>/<method>`). |
| `method "<text>"` | gRPC RPC method name | `"SayHello"` | Sets the method suffix in the HTTP/2 `:path` pseudo-header. |
| `grpcstatus <number>` | gRPC status code (`0` OK, `3` INVALID_ARGUMENT, `7` PERMISSION_DENIED, `14` UNAVAILABLE, `16` UNAUTHENTICATED) | `Omitted on requests` | Switches the packet to a gRPC Trailers response frame carrying `grpc-status`. |

## Cross-Layer Options Valid with `grpc`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 50051` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323 options (`mss`, `wscale`, `sackperm`, `tsval`, `tsecr`). |

## Common Mistakes & Quick Fixes

### Use grpcstatus (Not status) for gRPC Status Codes

- **Avoid:** `caller <- backend grpc status 14`
- **Valid v3.2.2 Syntax:** `caller <- backend grpc grpcstatus 14`

gRPC reports RPC completion codes in the `grpc-status` trailer (`0..16`) rather than HTTP status `200`, so the modifier is named `grpcstatus`.

### Do Not Include Leading Slashes in service or method

- **Avoid:** `caller -> backend grpc service "/helloworld.Greeter" method "/SayHello"`
- **Valid v3.2.2 Syntax:** `caller -> backend grpc service "helloworld.Greeter" method "SayHello"`

FrameAssembly automatically joins `service` and `method` into `/:service/:method`—pass plain names without extra `/` slashes.
