Skip to contentSkip to conversation

ss: Show more info (ring,fanout) for packet socks

PATCH Superseded Vadim Kochan 2 participants 7 messages May 13, 2015 – May 14, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ss: Show more info (ring,fanout) for packet socks

From: Vadim Kochan <[email protected]>

Print such info like version, tx/rx ring, fanout for packet sockets when '-e' option was specified.

---
Would it be problematic for parsers as it uses '-e' option? Or may be it would be better to use '-e -e' ?

 misc/ss.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 79 insertions(+), 2 deletions(-)
misc/ss.c +79 -2
index 870cad1..74797a8 100644
@@ -2834,13 +2834,27 @@ static int packet_stats_print(struct sockstat *s, const struct filter *f)
 	return 0;
 }
 
+static void packet_show_ring(struct packet_diag_ring *ring)
+{
+	printf("blk_size:%d", ring->pdr_block_size);
+	printf(",blk_nr:%d", ring->pdr_block_nr);
+	printf(",frm_size:%d", ring->pdr_frame_size);
+	printf(",frm_nr:%d", ring->pdr_frame_nr);
+	printf(",tmo:%d", ring->pdr_retire_tmo);
+	printf(",features:0x%x", ring->pdr_features);
+}
+
 static int packet_show_sock(const struct sockaddr_nl *addr,
 		struct nlmsghdr *nlh, void *arg)
 {
 	const struct filter *f = arg;
 	struct packet_diag_msg *r = NLMSG_DATA(nlh);
+	struct packet_diag_info *pinfo = NULL;
+	struct packet_diag_ring *ring_rx = NULL, *ring_tx = NULL;
 	struct rtattr *tb[PACKET_DIAG_MAX+1];
 	struct sockstat stat = {};
+	uint32_t fanout = 0;
+	bool has_fanout = false;
 
 	parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr*)(r+1),
 		     nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
@@ -2861,16 +2875,78 @@ static int packet_show_sock(const struct sockaddr_nl *addr,
 	}
 
 	if (tb[PACKET_DIAG_INFO]) {
-		struct packet_diag_info *pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]);
+		pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]);
 		stat.lport = stat.iface = pinfo->pdi_index;
 	}
 
 	if (tb[PACKET_DIAG_UID])
 		stat.uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]);
 
+	if (tb[PACKET_DIAG_RX_RING])
+		ring_rx = RTA_DATA(tb[PACKET_DIAG_RX_RING]);
+
+	if (tb[PACKET_DIAG_TX_RING])
+		ring_tx = RTA_DATA(tb[PACKET_DIAG_TX_RING]);
+
+	if (tb[PACKET_DIAG_FANOUT]) {
+		has_fanout = true;
+		fanout = *(uint32_t *)RTA_DATA(tb[PACKET_DIAG_FANOUT]);
+	}
+
 	if (packet_stats_print(&stat, f))
 		return 0;
 
+	if (show_details) {
+		if (pinfo) {
+			printf("\n\tver:%d", pinfo->pdi_version);
+			printf(" cpy_thresh:%d", pinfo->pdi_copy_thresh);
+			printf(" flags( ");
+			if (pinfo->pdi_flags & PDI_RUNNING)
+				printf("running");
+			if (pinfo->pdi_flags & PDI_AUXDATA)
+				printf(" auxdata");
+			if (pinfo->pdi_flags & PDI_ORIGDEV)
+				printf(" origdev");
+			if (pinfo->pdi_flags & PDI_VNETHDR)
+				printf(" vnethdr");
+			if (pinfo->pdi_flags & PDI_LOSS)
+				printf(" loss");
+			if (!pinfo->pdi_flags)
+				printf("0");
+			printf(" )");
+		}
+		if (ring_rx) {
+			printf("\n\tring_rx(");
+			packet_show_ring(ring_rx);
+			printf(")");
+		}
+		if (ring_tx) {
+			printf("\n\tring_tx(");
+			packet_show_ring(ring_tx);
+			printf(")");
+		}
+		if (has_fanout) {
+			uint16_t type = (fanout >> 16) & 0xffff;
+
+			printf("\n\tfanout(");
+			printf("id:%d,", fanout & 0xff);
+			printf("type:0x%x", type);
+			if (type == 0)
+				printf("(hash)");
+			else if (type == 1)
+				printf("(lb)");
+			else if (type == 2)
+				printf("(cpu)");
+			else if (type == 3)
+				printf("(rollover)");
+			else if (type == 4)
+				printf("(random)");
+			else if (type == 5)
+				printf("(qm)");
+			printf(")");
+		}
+	}
+
 	if (show_bpf && tb[PACKET_DIAG_FILTER]) {
 		struct sock_filter *fil =
 		       RTA_DATA(tb[PACKET_DIAG_FILTER]);
@@ -2894,7 +2970,8 @@ static int packet_show_netlink(struct filter *f)
 	DIAG_REQUEST(req, struct packet_diag_req r);
 
 	req.r.sdiag_family = AF_PACKET;
-	req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO | PACKET_SHOW_FILTER;
+	req.r.pdiag_show = PACKET_SHOW_INFO | PACKET_SHOW_MEMINFO |
+		PACKET_SHOW_FILTER | PACKET_SHOW_RING_CFG | PACKET_SHOW_FANOUT;
 
 	return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock);
 }
Signature · 1 line
2.3.1
#1 lore raw

misc/ss.c static int packet_show_sock(const struct sockaddr_nl *addr, · line 2929

Quoted 3 lines
misc/ss.c +3 -0
+               }
+               if (has_fanout) {
+                       uint16_t type = (fanout >> 16) & 0xffff;

type can be modified by flags in the upper 8 bits. Better mask those out and report them separately.

Quoted 16 lines
misc/ss.c +16 -0
+
+                       printf("\n\tfanout(");
+                       printf("id:%d,", fanout & 0xff);
+                       printf("type:0x%x", type);
+                       if (type == 0)
+                               printf("(hash)");
+                       else if (type == 1)
+                               printf("(lb)");
+                       else if (type == 2)
+                               printf("(cpu)");
+                       else if (type == 3)
+                               printf("(rollover)");
+                       else if (type == 4)
+                               printf("(random)");
+                       else if (type == 5)
+                               printf("(qm)");
#2 lore raw
Quoted 6 lines · Willem de Bruijn

misc/ss.c static int packet_show_sock(const struct sockaddr_nl *addr, · line 2929

Quoted 3 lines
misc/ss.c +3 -0
+               }
+               if (has_fanout) {
+                       uint16_t type = (fanout >> 16) & 0xffff;

type can be modified by flags in the upper 8 bits. Better mask those out and report them separately.

Kernel puts only id & type in diag message, flags are stored in separated member of 'struct packet_sock'.

Quoted 17 lines
Quoted 17 lines
misc/ss.c +16 -0
+
+                       printf("\n\tfanout(");
+                       printf("id:%d,", fanout & 0xff);
+                       printf("type:0x%x", type);
+                       if (type == 0)
+                               printf("(hash)");
+                       else if (type == 1)
+                               printf("(lb)");
+                       else if (type == 2)
+                               printf("(cpu)");
+                       else if (type == 3)
+                               printf("(rollover)");
+                       else if (type == 4)
+                               printf("(random)");
+                       else if (type == 5)
+                               printf("(qm)");
#3 lore raw
Quoted 9 lines · Vadim Kochan
Quoted 6 lines · Willem de Bruijn

misc/ss.c static int packet_show_sock(const struct sockaddr_nl *addr, · line 2929

Quoted 3 lines
misc/ss.c +3 -0
+               }
+               if (has_fanout) {
+                       uint16_t type = (fanout >> 16) & 0xffff;

type can be modified by flags in the upper 8 bits. Better mask those out and report them separately.

Kernel puts only id & type in diag message, flags are stored in separated member of 'struct packet_sock'.

I don't mean pdi_flags, but

PACKET_FANOUT_FLAG_ROLLOVER
PACKET_FANOUT_FLAG_DEFRAG

which are passed as part of this u32 by the process during setsockopt PACKET_FANOUT.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
misc/ss.c +16 -0
+
+                       printf("\n\tfanout(");
+                       printf("id:%d,", fanout & 0xff);
+                       printf("type:0x%x", type);
+                       if (type == 0)
+                               printf("(hash)");
+                       else if (type == 1)
+                               printf("(lb)");
+                       else if (type == 2)
+                               printf("(cpu)");
+                       else if (type == 3)
+                               printf("(rollover)");
+                       else if (type == 4)
+                               printf("(random)");
+                       else if (type == 5)
+                               printf("(qm)");
#4 lore raw
Quoted 8 lines · Willem de Bruijn
Quoted 7 lines · Vadim Kochan
Quoted 6 lines · Willem de Bruijn

misc/ss.c static int packet_show_sock(const struct sockaddr_nl *addr, · line 2929

Quoted 3 lines
misc/ss.c +3 -0
+               }
+               if (has_fanout) {
+                       uint16_t type = (fanout >> 16) & 0xffff;

type can be modified by flags in the upper 8 bits. Better mask those out and report them separately.

Quoted 7 lines
Quoted 2 lines

Kernel puts only id & type in diag message, flags are stored in separated member of 'struct packet_sock'.

I don't mean pdi_flags, but

PACKET_FANOUT_FLAG_ROLLOVER
PACKET_FANOUT_FLAG_DEFRAG

sure, I understood what you meant.

Quoted 4 lines

which are passed as part of this u32 by the process during setsockopt PACKET_FANOUT.

OK, its better to look into the kernel:

net/packet/af_packet.c

static int fanout_add(struct sock *sk, u16 id, u16 type_flags)

{
	struct packet_fanout *f, *match;
	u8 type = type_flags & 0xff;
	u8 flags = type_flags >> 8;

	...

	match->id = id;
	match->type = type;
	match->flags = flags;

	...
}

As I see this function is called from setsockopt, takes type_flags and splits it into type & flags and store them separately in struct packet_fanout.

Then what I see in:

net/packet/diag.c:

static int pdiag_put_fanout(struct packet_sock *po, struct sk_buff *nlskb)

{
	int ret = 0;

	mutex_lock(&fanout_mutex);
	if (po->fanout) {
		u32 val;

		val = (u32)po->fanout->id | ((u32)po->fanout->type << 16);
		ret = nla_put_u32(nlskb, PACKET_DIAG_FANOUT, val);
	}
	mutex_unlock(&fanout_mutex);

	return ret;
}

I see that to put flags here it needs to use po->fanout->flags and mix them into u32 val, but instead I see that it uses only id & type.

Please correct me if I am wrong.

Thanks,

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
misc/ss.c +16 -0
+
+                       printf("\n\tfanout(");
+                       printf("id:%d,", fanout & 0xff);
+                       printf("type:0x%x", type);
+                       if (type == 0)
+                               printf("(hash)");
+                       else if (type == 1)
+                               printf("(lb)");
+                       else if (type == 2)
+                               printf("(cpu)");
+                       else if (type == 3)
+                               printf("(rollover)");
+                       else if (type == 4)
+                               printf("(random)");
+                       else if (type == 5)
+                               printf("(qm)");
#5 lore raw
Quoted 64 lines · Vadim Kochan
Quoted 8 lines · Willem de Bruijn
Quoted 7 lines · Vadim Kochan
Quoted 6 lines · Willem de Bruijn

misc/ss.c static int packet_show_sock(const struct sockaddr_nl *addr, · line 2929

Quoted 3 lines
misc/ss.c +3 -0
+               }
+               if (has_fanout) {
+                       uint16_t type = (fanout >> 16) & 0xffff;

type can be modified by flags in the upper 8 bits. Better mask those out and report them separately.

Quoted 7 lines
Quoted 2 lines

Kernel puts only id & type in diag message, flags are stored in separated member of 'struct packet_sock'.

I don't mean pdi_flags, but

PACKET_FANOUT_FLAG_ROLLOVER
PACKET_FANOUT_FLAG_DEFRAG

sure, I understood what you meant.

Quoted 4 lines

which are passed as part of this u32 by the process during setsockopt PACKET_FANOUT.

OK, its better to look into the kernel:

net/packet/af_packet.c

static int fanout_add(struct sock *sk, u16 id, u16 type_flags)

{
        struct packet_fanout *f, *match;
        u8 type = type_flags & 0xff;
        u8 flags = type_flags >> 8;

        ...

        match->id = id;
        match->type = type;
        match->flags = flags;

        ...
}

As I see this function is called from setsockopt, takes type_flags and splits it into type & flags and store them separately in struct packet_fanout.

Then what I see in:

net/packet/diag.c:

static int pdiag_put_fanout(struct packet_sock *po, struct sk_buff *nlskb)

{
        int ret = 0;

        mutex_lock(&fanout_mutex);
        if (po->fanout) {
                u32 val;

                val = (u32)po->fanout->id | ((u32)po->fanout->type << 16);
                ret = nla_put_u32(nlskb, PACKET_DIAG_FANOUT, val);
        }
        mutex_unlock(&fanout_mutex);

        return ret;
}
Quoted 4 lines

I see that to put flags here it needs to use po->fanout->flags and mix them into u32 val, but instead I see that it uses only id & type.

Please correct me if I am wrong.

Thanks for the details. You are correct. Flags are not mixed in, like are in the getsockopt case. In that case, type can only be 8 bits.

Quoted 7 lines

Thanks,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
misc/ss.c +3 -0
+
+                       printf("\n\tfanout(");
+                       printf("id:%d,", fanout & 0xff);

ID can be 16 bits.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
misc/ss.c +13 -0
+                       printf("type:0x%x", type);
+                       if (type == 0)
+                               printf("(hash)");
+                       else if (type == 1)
+                               printf("(lb)");
+                       else if (type == 2)
+                               printf("(cpu)");
+                       else if (type == 3)
+                               printf("(rollover)");
+                       else if (type == 4)
+                               printf("(random)");
+                       else if (type == 5)
+                               printf("(qm)");
#6 lore raw
Quoted 74 lines · Willem de Bruijn
Quoted 64 lines · Vadim Kochan
Quoted 8 lines · Willem de Bruijn
Quoted 7 lines · Vadim Kochan
Quoted 6 lines · Willem de Bruijn

misc/ss.c static int packet_show_sock(const struct sockaddr_nl *addr, · line 2929

Quoted 3 lines
misc/ss.c +3 -0
+               }
+               if (has_fanout) {
+                       uint16_t type = (fanout >> 16) & 0xffff;

type can be modified by flags in the upper 8 bits. Better mask those out and report them separately.

Quoted 7 lines
Quoted 2 lines

Kernel puts only id & type in diag message, flags are stored in separated member of 'struct packet_sock'.

I don't mean pdi_flags, but

PACKET_FANOUT_FLAG_ROLLOVER
PACKET_FANOUT_FLAG_DEFRAG

sure, I understood what you meant.

Quoted 4 lines

which are passed as part of this u32 by the process during setsockopt PACKET_FANOUT.

OK, its better to look into the kernel:

net/packet/af_packet.c

static int fanout_add(struct sock *sk, u16 id, u16 type_flags)

{
        struct packet_fanout *f, *match;
        u8 type = type_flags & 0xff;
        u8 flags = type_flags >> 8;

        ...

        match->id = id;
        match->type = type;
        match->flags = flags;

        ...
}

As I see this function is called from setsockopt, takes type_flags and splits it into type & flags and store them separately in struct packet_fanout.

Then what I see in:

net/packet/diag.c:

static int pdiag_put_fanout(struct packet_sock *po, struct sk_buff *nlskb)

{
        int ret = 0;

        mutex_lock(&fanout_mutex);
        if (po->fanout) {
                u32 val;

                val = (u32)po->fanout->id | ((u32)po->fanout->type << 16);
                ret = nla_put_u32(nlskb, PACKET_DIAG_FANOUT, val);
        }
        mutex_unlock(&fanout_mutex);

        return ret;
}
Quoted 4 lines

I see that to put flags here it needs to use po->fanout->flags and mix them into u32 val, but instead I see that it uses only id & type.

Please correct me if I am wrong.

Thanks for the details. You are correct. Flags are not mixed in, like are in the getsockopt case. In that case, type can only be 8 bits.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

Thanks,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
misc/ss.c +3 -0
+
+                       printf("\n\tfanout(");
+                       printf("id:%d,", fanout & 0xff);

ID can be 16 bits.

Right, will fix it in v2, thanks!

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
misc/ss.c +13 -0
+                       printf("type:0x%x", type);
+                       if (type == 0)
+                               printf("(hash)");
+                       else if (type == 1)
+                               printf("(lb)");
+                       else if (type == 2)
+                               printf("(cpu)");
+                       else if (type == 3)
+                               printf("(rollover)");
+                       else if (type == 4)
+                               printf("(random)");
+                       else if (type == 5)
+                               printf("(qm)");