Skip to contentSkip to conversation

ovpn: extend statistics with per-peer drop and event counters

RFC New Marco Baffo 1 participant 3 messages Sep 11

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

RFC v3 1/3 ovpn: extend statistics with per-peer drop and event counters

Drops are currently accounted only via the undifferentiated netdev rx/tx_dropped counters, hiding the actual drop reason. Add per-peer atomic64_t counters (struct ovpn_peer_estats) for the significant drop reasons and events in the data path:

* RX: decrypt errors, replay errors, unknown key-id, unsupported inner protocol, RPF check failures
* TX: encrypt errors, IV exhaustion, missing primary key, missing transport, GSO segmentation errors
* events: keepalives received/sent, endpoint floats

Only ovpn-specific drop reasons are covered. Generic ENOMEM failures and teardown races are not accounted.

Export the counters in a new OVPN_A_PEER_ESTATS nest in the OVPN_CMD_PEER_GET reply and teach ovpn-cli to print them.

---
Changes in v3: none.
Changes in v2: rebased.

 Documentation/netlink/specs/ovpn.yaml       | 62 +++++++++++++++++++++
 drivers/net/ovpn/io.c                       | 25 +++++++--
 drivers/net/ovpn/netlink-gen.c              | 19 ++++++-
 drivers/net/ovpn/netlink-gen.h              |  3 +-
 drivers/net/ovpn/netlink.c                  | 37 +++++++++++-
 drivers/net/ovpn/peer.c                     |  5 ++
 drivers/net/ovpn/peer.h                     |  2 +
 drivers/net/ovpn/stats.h                    | 33 +++++++++++
 include/uapi/linux/ovpn.h                   | 20 +++++++
 tools/testing/selftests/net/ovpn/ovpn-cli.c | 60 ++++++++++++++++++++
 10 files changed, 259 insertions(+), 7 deletions(-)
Documentation/netlink/specs/ovpn.yaml +62 -0
index ac50d1d7c00a6..dd1bb96c312dd 100644
@@ -175,6 +175,68 @@ attribute-sets:
           will advertise the tx-id to be used on the link.
         checks:
           max: 0xFFFFFF
+      -
+        name: estats
+        type: nest
+        doc: Extended statistics, per-peer drop/event counters
+        nested-attributes: peer-estats
+  -
+    name: peer-estats
+    attributes:
+      -
+        name: rx-decrypt-errors
+        type: uint
+        doc: Number of packets dropped due to decryption failure
+      -
+        name: rx-replay-errors
+        type: uint
+        doc: Number of packets dropped by the replay protection check
+      -
+        name: rx-unknown-keyid
+        type: uint
+        doc: Number of packets dropped due to unknown key ID
+      -
+        name: rx-unsupported-proto
+        type: uint
+        doc: >-
+          Number of packets dropped due to unsupported or malformed inner
+          protocol
+      -
+        name: rx-rpf-errors
+        type: uint
+        doc: Number of packets dropped by the reverse path filtering check
+      -
+        name: tx-encrypt-errors
+        type: uint
+        doc: Number of packets dropped due to encryption failure
+      -
+        name: tx-iv-exhausted
+        type: uint
+        doc: Number of packets dropped due to packet ID (IV) exhaustion
+      -
+        name: tx-no-key
+        type: uint
+        doc: Number of packets dropped due to missing primary key
+      -
+        name: tx-no-transport
+        type: uint
+        doc: Number of packets dropped due to missing transport socket
+      -
+        name: tx-gso-errors
+        type: uint
+        doc: Number of packets dropped due to GSO segmentation failure
+      -
+        name: keepalive-rx
+        type: uint
+        doc: Number of keepalive packets received from this peer
+      -
+        name: keepalive-tx
+        type: uint
+        doc: Number of keepalive packets sent to this peer
+      -
+        name: float-count
+        type: uint
+        doc: Number of times the peer endpoint floated
   -
     name: peer-new-input
     subset-of: peer
drivers/net/ovpn/io.c +21 -4
index 9526f8096da60..37547f6117e9c 100644
@@ -128,8 +128,10 @@ void ovpn_decrypt_post(void *data, int ret)
 	/* crypto is done, cleanup skb CB and its members */
 	kfree(ovpn_skb_cb(skb)->crypto_tmp);
 
-	if (unlikely(ret < 0))
+	if (unlikely(ret < 0)) {
+		atomic64_inc(&peer->estats.rx_decrypt_errors);
 		goto drop;
+	}
 
 	/* PID sits after the op */
 	pid = (__force __be32 *)(skb->data + OVPN_OPCODE_SIZE);
@@ -138,6 +140,7 @@ void ovpn_decrypt_post(void *data, int ret)
 		net_err_ratelimited("%s: PKT ID RX error for peer %u: %d\n",
 				    netdev_name(peer->ovpn->dev), peer->id,
 				    ret);
+		atomic64_inc(&peer->estats.rx_replay_errors);
 		goto drop;
 	}
 
@@ -165,6 +168,7 @@ void ovpn_decrypt_post(void *data, int ret)
 			net_info_ratelimited("%s: NULL packet received from peer %u\n",
 					     netdev_name(peer->ovpn->dev),
 					     peer->id);
+			atomic64_inc(&peer->estats.rx_unsupported_proto);
 			goto drop;
 		}
 
@@ -172,6 +176,7 @@ void ovpn_decrypt_post(void *data, int ret)
 			net_dbg_ratelimited("%s: ping received from peer %u\n",
 					    netdev_name(peer->ovpn->dev),
 					    peer->id);
+			atomic64_inc(&peer->estats.keepalive_rx);
 			/* we drop the packet, but this is not a failure */
 			consume_skb(skb);
 			goto drop_nocount;
@@ -179,6 +184,7 @@ void ovpn_decrypt_post(void *data, int ret)
 
 		net_info_ratelimited("%s: unsupported protocol received from peer %u\n",
 				     netdev_name(peer->ovpn->dev), peer->id);
+		atomic64_inc(&peer->estats.rx_unsupported_proto);
 		goto drop;
 	}
 	skb->protocol = proto;
@@ -193,6 +199,7 @@ void ovpn_decrypt_post(void *data, int ret)
 			net_dbg_ratelimited("%s: RPF dropped packet from peer %u, src: %pI4\n",
 					    netdev_name(peer->ovpn->dev),
 					    peer->id, &ip_hdr(skb)->saddr);
+		atomic64_inc(&peer->estats.rx_rpf_errors);
 		goto drop;
 	}
 
@@ -225,6 +232,7 @@ void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb)
 		net_info_ratelimited("%s: no available key for peer %u, key-id: %u\n",
 				     netdev_name(peer->ovpn->dev), peer->id,
 				     key_id);
+		atomic64_inc(&peer->estats.rx_unknown_keyid);
 		ovpn_dev_dstats_rx_dropped(peer->ovpn->dev);
 		kfree_skb(skb);
 		ovpn_peer_put(peer);
@@ -266,19 +274,24 @@ void ovpn_encrypt_post(void *data, int ret)
 			/* let userspace know so that a new key must be negotiated */
 			ovpn_nl_key_swap_notify(peer, ks->key_id);
 
+		atomic64_inc(&peer->estats.tx_iv_exhausted);
 		goto err;
 	}
 
-	if (unlikely(ret < 0))
+	if (unlikely(ret < 0)) {
+		atomic64_inc(&peer->estats.tx_encrypt_errors);
 		goto err;
+	}
 
 	skb_mark_not_on_list(skb);
 	orig_len = skb->len;
 
 	rcu_read_lock();
 	sock = rcu_dereference(peer->sock);
-	if (unlikely(!sock))
+	if (unlikely(!sock)) {
+		atomic64_inc(&peer->estats.tx_no_transport);
 		goto err_unlock;
+	}
 
 	switch (sock->sk->sk_protocol) {
 	case IPPROTO_UDP:
@@ -289,6 +302,7 @@ void ovpn_encrypt_post(void *data, int ret)
 		break;
 	default:
 		/* no transport configured yet */
+		atomic64_inc(&peer->estats.tx_no_transport);
 		goto err_unlock;
 	}
 
@@ -315,8 +329,10 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 
 	/* get primary key to be used for encrypting data */
 	ks = ovpn_crypto_key_slot_primary(&peer->crypto);
-	if (unlikely(!ks))
+	if (unlikely(!ks)) {
+		atomic64_inc(&peer->estats.tx_no_key);
 		return false;
+	}
 
 	/* take a reference to the peer because the crypto code may run async.
 	 * ovpn_encrypt_post() will release it upon completion
@@ -397,6 +413,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 			ret = PTR_ERR(segments);
 			net_err_ratelimited("%s: cannot segment payload packet: %d\n",
 					    netdev_name(dev), ret);
+			atomic64_inc(&peer->estats.tx_gso_errors);
 			goto drop;
 		}
 
drivers/net/ovpn/netlink-gen.c +18 -1
index 92d2fdc17c2e8..d427db6c563fe 100644
@@ -63,7 +63,7 @@ const struct nla_policy ovpn_keydir_nl_policy[OVPN_A_KEYDIR_NONCE_TAIL + 1] = {
 	[OVPN_A_KEYDIR_NONCE_TAIL] = NLA_POLICY_EXACT_LEN(OVPN_NONCE_TAIL_SIZE),
 };
 
-const struct nla_policy ovpn_peer_nl_policy[OVPN_A_PEER_TX_ID + 1] = {
+const struct nla_policy ovpn_peer_nl_policy[OVPN_A_PEER_ESTATS + 1] = {
 	[OVPN_A_PEER_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &ovpn_a_peer_id_range),
 	[OVPN_A_PEER_REMOTE_IPV4] = { .type = NLA_BE32, },
 	[OVPN_A_PEER_REMOTE_IPV6] = NLA_POLICY_EXACT_LEN(16),
@@ -88,12 +88,29 @@ const struct nla_policy ovpn_peer_nl_policy[OVPN_A_PEER_TX_ID + 1] = {
 	[OVPN_A_PEER_LINK_RX_PACKETS] = { .type = NLA_UINT, },
 	[OVPN_A_PEER_LINK_TX_PACKETS] = { .type = NLA_UINT, },
 	[OVPN_A_PEER_TX_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &ovpn_a_peer_tx_id_range),
+	[OVPN_A_PEER_ESTATS] = NLA_POLICY_NESTED(ovpn_peer_estats_nl_policy),
 };
 
 const struct nla_policy ovpn_peer_del_input_nl_policy[OVPN_A_PEER_ID + 1] = {
 	[OVPN_A_PEER_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &ovpn_a_peer_id_range),
 };
 
+const struct nla_policy ovpn_peer_estats_nl_policy[OVPN_A_PEER_ESTATS_FLOAT_COUNT + 1] = {
+	[OVPN_A_PEER_ESTATS_RX_DECRYPT_ERRORS] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_RX_REPLAY_ERRORS] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_RX_UNKNOWN_KEYID] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_RX_UNSUPPORTED_PROTO] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_RX_RPF_ERRORS] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_TX_ENCRYPT_ERRORS] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_TX_IV_EXHAUSTED] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_TX_NO_KEY] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_TX_NO_TRANSPORT] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_TX_GSO_ERRORS] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_KEEPALIVE_RX] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_KEEPALIVE_TX] = { .type = NLA_UINT, },
+	[OVPN_A_PEER_ESTATS_FLOAT_COUNT] = { .type = NLA_UINT, },
+};
+
 const struct nla_policy ovpn_peer_new_input_nl_policy[OVPN_A_PEER_TX_ID + 1] = {
 	[OVPN_A_PEER_ID] = NLA_POLICY_FULL_RANGE(NLA_U32, &ovpn_a_peer_id_range),
 	[OVPN_A_PEER_REMOTE_IPV4] = { .type = NLA_BE32, },
drivers/net/ovpn/netlink-gen.h +2 -1
index 67cd85f86173f..197e4a992d692 100644
@@ -18,8 +18,9 @@ extern const struct nla_policy ovpn_keyconf_del_input_nl_policy[OVPN_A_KEYCONF_S
 extern const struct nla_policy ovpn_keyconf_get_nl_policy[OVPN_A_KEYCONF_CIPHER_ALG + 1];
 extern const struct nla_policy ovpn_keyconf_swap_input_nl_policy[OVPN_A_KEYCONF_PEER_ID + 1];
 extern const struct nla_policy ovpn_keydir_nl_policy[OVPN_A_KEYDIR_NONCE_TAIL + 1];
-extern const struct nla_policy ovpn_peer_nl_policy[OVPN_A_PEER_TX_ID + 1];
+extern const struct nla_policy ovpn_peer_nl_policy[OVPN_A_PEER_ESTATS + 1];
 extern const struct nla_policy ovpn_peer_del_input_nl_policy[OVPN_A_PEER_ID + 1];
+extern const struct nla_policy ovpn_peer_estats_nl_policy[OVPN_A_PEER_ESTATS_FLOAT_COUNT + 1];
 extern const struct nla_policy ovpn_peer_new_input_nl_policy[OVPN_A_PEER_TX_ID + 1];
 extern const struct nla_policy ovpn_peer_set_input_nl_policy[OVPN_A_PEER_TX_ID + 1];
 
drivers/net/ovpn/netlink.c +36 -1
index 4dad852941982..9e03b4cefceec 100644
@@ -551,9 +551,9 @@ static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *info,
 			     int flags)
 {
 	const struct ovpn_bind *bind;
+	struct nlattr *attr, *estats;
 	struct ovpn_socket *sock;
 	int ret = -EMSGSIZE;
-	struct nlattr *attr;
 	__be16 local_port;
 	void *hdr;
 	int id;
@@ -654,6 +654,41 @@ static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *info,
 			 atomic64_read(&peer->link_stats.tx.packets)))
 		goto err;
 
+	estats = nla_nest_start(skb, OVPN_A_PEER_ESTATS);
+	if (!estats)
+		goto err;
+
+	if (/* drop/event counters */
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_RX_DECRYPT_ERRORS,
+			 atomic64_read(&peer->estats.rx_decrypt_errors)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_RX_REPLAY_ERRORS,
+			 atomic64_read(&peer->estats.rx_replay_errors)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_RX_UNKNOWN_KEYID,
+			 atomic64_read(&peer->estats.rx_unknown_keyid)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_RX_UNSUPPORTED_PROTO,
+			 atomic64_read(&peer->estats.rx_unsupported_proto)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_RX_RPF_ERRORS,
+			 atomic64_read(&peer->estats.rx_rpf_errors)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_TX_ENCRYPT_ERRORS,
+			 atomic64_read(&peer->estats.tx_encrypt_errors)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_TX_IV_EXHAUSTED,
+			 atomic64_read(&peer->estats.tx_iv_exhausted)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_TX_NO_KEY,
+			 atomic64_read(&peer->estats.tx_no_key)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_TX_NO_TRANSPORT,
+			 atomic64_read(&peer->estats.tx_no_transport)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_TX_GSO_ERRORS,
+			 atomic64_read(&peer->estats.tx_gso_errors)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_KEEPALIVE_RX,
+			 atomic64_read(&peer->estats.keepalive_rx)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_KEEPALIVE_TX,
+			 atomic64_read(&peer->estats.keepalive_tx)) ||
+	    nla_put_uint(skb, OVPN_A_PEER_ESTATS_FLOAT_COUNT,
+			 atomic64_read(&peer->estats.floats)))
+		goto err;
+
+	nla_nest_end(skb, estats);
+
 	nla_nest_end(skb, attr);
 	genlmsg_end(skb, hdr);
 
drivers/net/ovpn/peer.c +5 -0
index c95656ca7c357..08cdd179de320 100644
@@ -77,6 +77,10 @@ static void ovpn_peer_keepalive_send(struct work_struct *work)
 	struct ovpn_peer *peer = container_of(work, struct ovpn_peer,
 					      keepalive_work);
 
+	/* count attempted keepalives: if the TX path fails afterwards,
+	 * keepalive_tx will include a transmission that was not sent
+	 */
+	atomic64_inc(&peer->estats.keepalive_tx);
 	local_bh_disable();
 	ovpn_xmit_special(peer, ovpn_keepalive_message,
 			  sizeof(ovpn_keepalive_message));
@@ -309,6 +313,7 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)
 
 	spin_unlock_bh(&peer->lock);
 
+	atomic64_inc(&peer->estats.floats);
 	ovpn_nl_peer_float_notify(peer, &ss);
 
 	/* rehashing is required only in MP mode as P2P has one peer
drivers/net/ovpn/peer.h +2 -0
index dfa5c0037e02b..5667bad91c31f 100644
@@ -60,6 +60,7 @@
  * @rcu: used to free peer in an RCU safe way
  * @release_entry: entry for the socket release list
  * @keepalive_work: used to schedule keepalive sending
+ * @estats: per-peer drop/event counters
  */
 struct ovpn_peer {
 	struct ovpn_priv *ovpn;
@@ -114,6 +115,7 @@ struct ovpn_peer {
 	struct rcu_head rcu;
 	struct llist_node release_entry;
 	struct work_struct keepalive_work;
+	struct ovpn_peer_estats estats;
 };
 
 /**
drivers/net/ovpn/stats.h +33 -0
index 3a45b97c00568..8b900e0c11210 100644
@@ -25,6 +25,39 @@ struct ovpn_peer_stats {
 	struct ovpn_peer_stat tx;
 };
 
+/**
+ * struct ovpn_peer_estats - per-peer drop/event counters
+ * @rx_decrypt_errors: packets dropped due to decryption/auth failure
+ * @rx_replay_errors: packets dropped by the replay protection check
+ * @rx_unknown_keyid: packets dropped due to unknown key ID
+ * @rx_unsupported_proto: packets dropped due to unsupported or malformed
+ *			  inner protocol
+ * @rx_rpf_errors: packets dropped by the reverse path filtering check
+ * @tx_encrypt_errors: packets dropped due to encryption failure
+ * @tx_iv_exhausted: packets dropped due to packet ID (IV) exhaustion
+ * @tx_no_key: packets dropped due to missing primary key
+ * @tx_no_transport: packets dropped due to missing transport socket
+ * @tx_gso_errors: packets dropped due to GSO segmentation failure
+ * @keepalive_rx: keepalive packets received from this peer
+ * @keepalive_tx: keepalive packets sent to this peer
+ * @floats: number of times the peer endpoint floated
+ */
+struct ovpn_peer_estats {
+	atomic64_t rx_decrypt_errors;
+	atomic64_t rx_replay_errors;
+	atomic64_t rx_unknown_keyid;
+	atomic64_t rx_unsupported_proto;
+	atomic64_t rx_rpf_errors;
+	atomic64_t tx_encrypt_errors;
+	atomic64_t tx_iv_exhausted;
+	atomic64_t tx_no_key;
+	atomic64_t tx_no_transport;
+	atomic64_t tx_gso_errors;
+	atomic64_t keepalive_rx;
+	atomic64_t keepalive_tx;
+	atomic64_t floats;
+};
+
 void ovpn_peer_stats_init(struct ovpn_peer_stats *ps);
 
 static inline void ovpn_peer_stats_increment(struct ovpn_peer_stat *stat,
include/uapi/linux/ovpn.h +20 -0
index 06690090a1a95..e5ed635c03732 100644
@@ -56,11 +56,31 @@ enum {
 	OVPN_A_PEER_LINK_RX_PACKETS,
 	OVPN_A_PEER_LINK_TX_PACKETS,
 	OVPN_A_PEER_TX_ID,
+	OVPN_A_PEER_ESTATS,
 
 	__OVPN_A_PEER_MAX,
 	OVPN_A_PEER_MAX = (__OVPN_A_PEER_MAX - 1)
 };
 
+enum {
+	OVPN_A_PEER_ESTATS_RX_DECRYPT_ERRORS = 1,
+	OVPN_A_PEER_ESTATS_RX_REPLAY_ERRORS,
+	OVPN_A_PEER_ESTATS_RX_UNKNOWN_KEYID,
+	OVPN_A_PEER_ESTATS_RX_UNSUPPORTED_PROTO,
+	OVPN_A_PEER_ESTATS_RX_RPF_ERRORS,
+	OVPN_A_PEER_ESTATS_TX_ENCRYPT_ERRORS,
+	OVPN_A_PEER_ESTATS_TX_IV_EXHAUSTED,
+	OVPN_A_PEER_ESTATS_TX_NO_KEY,
+	OVPN_A_PEER_ESTATS_TX_NO_TRANSPORT,
+	OVPN_A_PEER_ESTATS_TX_GSO_ERRORS,
+	OVPN_A_PEER_ESTATS_KEEPALIVE_RX,
+	OVPN_A_PEER_ESTATS_KEEPALIVE_TX,
+	OVPN_A_PEER_ESTATS_FLOAT_COUNT,
+
+	__OVPN_A_PEER_ESTATS_MAX,
+	OVPN_A_PEER_ESTATS_MAX = (__OVPN_A_PEER_ESTATS_MAX - 1)
+};
+
 enum {
 	OVPN_A_KEYCONF_PEER_ID = 1,
 	OVPN_A_KEYCONF_SLOT,
tools/testing/selftests/net/ovpn/ovpn-cli.c +60 -0

66 lines not on this page · raw message

Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#1 lore raw

RFC v3 2/3 ovpn: export detailed statistics via ethtool

Detailed per-peer counters are currently available only through netlink. Maintain device-wide copies of the same counters, incremented together with the per-peer ones, and expose them through ethtool.

Store the device-wide counters in per-CPU storage and aggregate them when queried, avoiding an additional device-wide atomic update in packet processing paths. The totals remain monotonic across peer deletion and interface down/up.

Also count packets dropped because no peer matched the source (RX) or destination (TX) address, and locally generated packets with an invalid protocol. These cannot be attributed to an individual peer and are therefore exported via ethtool only.

---
Changes in v3:
- Define shared macro lists for peer drop, peer event and device-only drop counters, allowing the drop lists to be reused by patch 3.
- Generate counter indices and ethtool names from these lists, replacing the name/index descriptor table with an array indexed by counter ID.
- Add a compile-time check that the peer counter count matches struct ovpn_peer_estats.

Changes in v2: rebased.

 drivers/net/ovpn/io.c       | 33 ++++++++++-------
 drivers/net/ovpn/main.c     | 73 +++++++++++++++++++++++++++++++++++--
 drivers/net/ovpn/ovpnpriv.h |  2 +
 drivers/net/ovpn/peer.c     |  4 +-
 drivers/net/ovpn/peer.h     | 16 ++++++++
 drivers/net/ovpn/stats.h    | 65 +++++++++++++++++++++++++++++++++
 drivers/net/ovpn/udp.c      |  5 ++-
 7 files changed, 178 insertions(+), 20 deletions(-)
drivers/net/ovpn/io.c +19 -14
index 37547f6117e9c..ca3e8a9680694 100644
@@ -129,7 +129,7 @@ void ovpn_decrypt_post(void *data, int ret)
 	kfree(ovpn_skb_cb(skb)->crypto_tmp);
 
 	if (unlikely(ret < 0)) {
-		atomic64_inc(&peer->estats.rx_decrypt_errors);
+		ovpn_estats_inc(peer, rx_decrypt_errors);
 		goto drop;
 	}
 
@@ -140,7 +140,7 @@ void ovpn_decrypt_post(void *data, int ret)
 		net_err_ratelimited("%s: PKT ID RX error for peer %u: %d\n",
 				    netdev_name(peer->ovpn->dev), peer->id,
 				    ret);
-		atomic64_inc(&peer->estats.rx_replay_errors);
+		ovpn_estats_inc(peer, rx_replay_errors);
 		goto drop;
 	}
 
@@ -168,7 +168,7 @@ void ovpn_decrypt_post(void *data, int ret)
 			net_info_ratelimited("%s: NULL packet received from peer %u\n",
 					     netdev_name(peer->ovpn->dev),
 					     peer->id);
-			atomic64_inc(&peer->estats.rx_unsupported_proto);
+			ovpn_estats_inc(peer, rx_unsupported_proto);
 			goto drop;
 		}
 
@@ -176,7 +176,7 @@ void ovpn_decrypt_post(void *data, int ret)
 			net_dbg_ratelimited("%s: ping received from peer %u\n",
 					    netdev_name(peer->ovpn->dev),
 					    peer->id);
-			atomic64_inc(&peer->estats.keepalive_rx);
+			ovpn_estats_inc(peer, keepalive_rx);
 			/* we drop the packet, but this is not a failure */
 			consume_skb(skb);
 			goto drop_nocount;
@@ -184,7 +184,7 @@ void ovpn_decrypt_post(void *data, int ret)
 
 		net_info_ratelimited("%s: unsupported protocol received from peer %u\n",
 				     netdev_name(peer->ovpn->dev), peer->id);
-		atomic64_inc(&peer->estats.rx_unsupported_proto);
+		ovpn_estats_inc(peer, rx_unsupported_proto);
 		goto drop;
 	}
 	skb->protocol = proto;
@@ -199,7 +199,7 @@ void ovpn_decrypt_post(void *data, int ret)
 			net_dbg_ratelimited("%s: RPF dropped packet from peer %u, src: %pI4\n",
 					    netdev_name(peer->ovpn->dev),
 					    peer->id, &ip_hdr(skb)->saddr);
-		atomic64_inc(&peer->estats.rx_rpf_errors);
+		ovpn_estats_inc(peer, rx_rpf_errors);
 		goto drop;
 	}
 
@@ -232,7 +232,7 @@ void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb)
 		net_info_ratelimited("%s: no available key for peer %u, key-id: %u\n",
 				     netdev_name(peer->ovpn->dev), peer->id,
 				     key_id);
-		atomic64_inc(&peer->estats.rx_unknown_keyid);
+		ovpn_estats_inc(peer, rx_unknown_keyid);
 		ovpn_dev_dstats_rx_dropped(peer->ovpn->dev);
 		kfree_skb(skb);
 		ovpn_peer_put(peer);
@@ -274,12 +274,12 @@ void ovpn_encrypt_post(void *data, int ret)
 			/* let userspace know so that a new key must be negotiated */
 			ovpn_nl_key_swap_notify(peer, ks->key_id);
 
-		atomic64_inc(&peer->estats.tx_iv_exhausted);
+		ovpn_estats_inc(peer, tx_iv_exhausted);
 		goto err;
 	}
 
 	if (unlikely(ret < 0)) {
-		atomic64_inc(&peer->estats.tx_encrypt_errors);
+		ovpn_estats_inc(peer, tx_encrypt_errors);
 		goto err;
 	}
 
@@ -289,7 +289,7 @@ void ovpn_encrypt_post(void *data, int ret)
 	rcu_read_lock();
 	sock = rcu_dereference(peer->sock);
 	if (unlikely(!sock)) {
-		atomic64_inc(&peer->estats.tx_no_transport);
+		ovpn_estats_inc(peer, tx_no_transport);
 		goto err_unlock;
 	}
 
@@ -302,7 +302,7 @@ void ovpn_encrypt_post(void *data, int ret)
 		break;
 	default:
 		/* no transport configured yet */
-		atomic64_inc(&peer->estats.tx_no_transport);
+		ovpn_estats_inc(peer, tx_no_transport);
 		goto err_unlock;
 	}
 
@@ -330,7 +330,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)
 	/* get primary key to be used for encrypting data */
 	ks = ovpn_crypto_key_slot_primary(&peer->crypto);
 	if (unlikely(!ks)) {
-		atomic64_inc(&peer->estats.tx_no_key);
+		ovpn_estats_inc(peer, tx_no_key);
 		return false;
 	}
 
@@ -384,8 +384,11 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 
 	/* verify IP header size in network packet */
 	proto = ovpn_ip_check_protocol(skb);
-	if (unlikely(!proto || skb->protocol != proto))
+	if (unlikely(!proto || skb->protocol != proto)) {
+		ovpn_dev_estats_inc(ovpn->estats,
+				    OVPN_DEV_ESTAT_TX_BAD_PROTO);
 		goto drop_no_peer;
+	}
 
 	/* retrieve peer serving the destination IP of this packet */
 	peer = ovpn_peer_get_by_dst(ovpn, skb);
@@ -402,6 +405,8 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 					    &ipv6_hdr(skb)->daddr);
 			break;
 		}
+		ovpn_dev_estats_inc(ovpn->estats,
+				    OVPN_DEV_ESTAT_TX_NO_PEER);
 		goto drop_no_peer;
 	}
 	/* dst was needed for peer selection - it can now be dropped */
@@ -413,7 +418,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 			ret = PTR_ERR(segments);
 			net_err_ratelimited("%s: cannot segment payload packet: %d\n",
 					    netdev_name(dev), ret);
-			atomic64_inc(&peer->estats.tx_gso_errors);
+			ovpn_estats_inc(peer, tx_gso_errors);
 			goto drop;
 		}
 
drivers/net/ovpn/main.c +70 -3

104 lines not on this page · raw message

drivers/net/ovpn/ovpnpriv.h +2 -0
index 84499140e4bd9..56aec8d403e00 100644
@@ -45,6 +45,7 @@ struct ovpn_peer_collection {
  * @peer: in P2P mode, this is the only remote peer
  * @gro_cells: pointer to the Generic Receive Offload cell
  * @keepalive_work: struct used to schedule keepalive periodic job
+ * @estats: monotonic per-CPU device-wide drop/event counters
  */
 struct ovpn_priv {
 	struct net_device *dev;
@@ -54,6 +55,7 @@ struct ovpn_priv {
 	struct ovpn_peer __rcu *peer;
 	struct gro_cells gro_cells;
 	struct delayed_work keepalive_work;
+	struct ovpn_dev_estats __percpu *estats;
 };
 
 #endif /* _NET_OVPN_OVPNSTRUCT_H_ */
drivers/net/ovpn/peer.c +2 -2
index 08cdd179de320..37bb57d7ff704 100644
@@ -80,7 +80,7 @@ static void ovpn_peer_keepalive_send(struct work_struct *work)
 	/* count attempted keepalives: if the TX path fails afterwards,
 	 * keepalive_tx will include a transmission that was not sent
 	 */
-	atomic64_inc(&peer->estats.keepalive_tx);
+	ovpn_estats_inc(peer, keepalive_tx);
 	local_bh_disable();
 	ovpn_xmit_special(peer, ovpn_keepalive_message,
 			  sizeof(ovpn_keepalive_message));
@@ -313,7 +313,7 @@ void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)
 
 	spin_unlock_bh(&peer->lock);
 
-	atomic64_inc(&peer->estats.floats);
+	ovpn_estats_inc(peer, floats);
 	ovpn_nl_peer_float_notify(peer, &ss);
 
 	/* rehashing is required only in MP mode as P2P has one peer
drivers/net/ovpn/peer.h +16 -0
index 5667bad91c31f..0279580011741 100644
@@ -118,6 +118,22 @@ struct ovpn_peer {
 	struct ovpn_peer_estats estats;
 };
 
+/**
+ * ovpn_estats_inc - increment a counter in both the per-peer and the
+ *		     device-wide extended statistics
+ * @peer: peer whose counter should be incremented
+ * @counter: name of the counter member in struct ovpn_peer_estats
+ *
+ * Device-wide counters are monotonic, they do not decrease when a peer
+ * is deleted. For device only counters use ovpn_dev_estats_inc() instead.
+ */
+#define ovpn_estats_inc(peer, counter)					\
+	do {								\
+		atomic64_inc(&(peer)->estats.counter);			\
+		ovpn_dev_estats_inc((peer)->ovpn->estats,		\
+				    OVPN_PEER_ESTAT_IDX(counter));	\
+	} while (0)
+
 /**
  * ovpn_peer_hold - increase reference counter
  * @peer: the peer whose counter should be increased
drivers/net/ovpn/stats.h +65 -0

71 lines not on this page · raw message

drivers/net/ovpn/udp.c +4 -1
index 7f69e8890b5b5..5ee196a8d4309 100644
@@ -116,8 +116,11 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 	else
 		peer = ovpn_peer_get_by_id(ovpn, peer_id);
 
-	if (unlikely(!peer))
+	if (unlikely(!peer)) {
+		ovpn_dev_estats_inc(ovpn->estats,
+				    OVPN_DEV_ESTAT_RX_NO_PEER);
 		goto drop;
+	}
 
 	/* pop off outer UDP header */
 	__skb_pull(skb, sizeof(struct udphdr));
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#2 lore raw

RFC v3 3/3 ovpn: report packet drop reasons

In reply to Marco Baffo

Register an OVPN drop reason subsystem and pass explicit reasons to the skb free helpers in RX and TX error paths. Generate reason codes and strings from the existing drop counter lists. Use core reasons for short UDP packets and unsupported DATA_V1 packets.

---
Changes: this patch was introduced in v3.

 drivers/net/ovpn/drop.h  | 39 ++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/io.c    | 45 +++++++++++++++++++++++++++++-----------
 drivers/net/ovpn/main.c  | 19 +++++++++++++++++
 drivers/net/ovpn/udp.c   | 19 +++++++++++------
 include/net/dropreason.h |  6 ++++++
 5 files changed, 110 insertions(+), 18 deletions(-)
 create mode 100644 drivers/net/ovpn/drop.h
drivers/net/ovpn/drop.h new file +39 -0
new file mode 100644
index 0000000000000..75e04ba57a778
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* OpenVPN data channel offload
+ *
+ *  Copyright (C) 2026 OpenVPN, Inc.
+ *  Author: Marco Baffo <[email protected]>
+ *          Antonio Quartulli <[email protected]>
+ */
+
+#ifndef _NET_OVPN_DROP_H_
+#define _NET_OVPN_DROP_H_
+
+#include <linux/skbuff.h>
+#include <net/dropreason.h>
+
+#include "stats.h"
+
+enum ovpn_drop_reason {
+	__OVPN_DROP_REASON = SKB_DROP_REASON_SUBSYS_OVPN <<
+			     SKB_DROP_REASON_SUBSYS_SHIFT,
+#define OVPN_DROP_REASON_ENUM(_counter, _name) OVPN_DROP_##_name,
+	OVPN_PEER_DROP_ESTATS(OVPN_DROP_REASON_ENUM)
+	OVPN_DEV_DROP_ESTATS(OVPN_DROP_REASON_ENUM)
+#undef OVPN_DROP_REASON_ENUM
+	OVPN_DROP_MAX,
+};
+
+static inline void ovpn_kfree_skb_reason(struct sk_buff *skb,
+					 enum ovpn_drop_reason reason)
+{
+	kfree_skb_reason(skb, (u32)reason);
+}
+
+static inline void ovpn_kfree_skb_list_reason(struct sk_buff *skb,
+					      enum ovpn_drop_reason reason)
+{
+	kfree_skb_list_reason(skb, (u32)reason);
+}
+
+#endif /* _NET_OVPN_DROP_H_ */
drivers/net/ovpn/io.c +33 -12

200 lines not on this page · raw message

drivers/net/ovpn/main.c +19 -0
index 6a34557e89561..8cbb88282e742 100644
@@ -19,6 +19,7 @@
 #include <uapi/linux/if_arp.h>
 
 #include "ovpnpriv.h"
+#include "drop.h"
 #include "main.h"
 #include "netlink.h"
 #include "io.h"
@@ -170,6 +171,20 @@ static const char * const ovpn_ethtool_stats[] = {
 #undef OVPN_ETHTOOL_ESTAT
 };
 
+static const char * const ovpn_drop_reasons[] = {
+#define OVPN_DROP_REASON_STRING(_counter, _name)		\
+	[(OVPN_DROP_##_name) & ~SKB_DROP_REASON_SUBSYS_MASK] =	\
+		"OVPN_DROP_" #_name,
+	OVPN_PEER_DROP_ESTATS(OVPN_DROP_REASON_STRING)
+	OVPN_DEV_DROP_ESTATS(OVPN_DROP_REASON_STRING)
+#undef OVPN_DROP_REASON_STRING
+};
+
+static const struct drop_reason_list ovpn_drop_reason_list = {
+	.reasons = ovpn_drop_reasons,
+	.n_reasons = ARRAY_SIZE(ovpn_drop_reasons),
+};
+
 static void ovpn_get_strings(struct net_device *dev, u32 stringset, u8 *data)
 {
 	unsigned int i;
@@ -353,6 +368,9 @@ static int __init ovpn_init(void)
 		goto unreg_rtnl;
 	}
 
+	drop_reasons_register_subsys(SKB_DROP_REASON_SUBSYS_OVPN,
+				     &ovpn_drop_reason_list);
+
 	return 0;
 
 unreg_rtnl:
@@ -369,6 +387,7 @@ static __exit void ovpn_cleanup(void)
 	rtnl_link_unregister(&ovpn_link_ops);
 
 	flush_workqueue(ovpn_wq);
+	drop_reasons_unregister_subsys(SKB_DROP_REASON_SUBSYS_OVPN);
 	rcu_barrier();
 
 	destroy_workqueue(ovpn_wq);
drivers/net/ovpn/udp.c +13 -6
index 5ee196a8d4309..882c9672acfe0 100644
@@ -21,6 +21,7 @@
 #include "ovpnpriv.h"
 #include "main.h"
 #include "bind.h"
+#include "drop.h"
 #include "io.h"
 #include "peer.h"
 #include "proto.h"
@@ -66,6 +67,7 @@ static struct ovpn_socket *ovpn_socket_from_udp_sock(struct sock *sk)
  */
 static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 {
+	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
 	struct ovpn_socket *ovpn_sock;
 	struct ovpn_priv *ovpn;
 	struct ovpn_peer *peer;
@@ -89,18 +91,22 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 	 * header are accessible.
 	 * They are required to fetch the OP code, the key ID and the peer ID.
 	 */
-	if (unlikely(!pskb_may_pull(skb, sizeof(struct udphdr) +
-				    OVPN_OPCODE_SIZE))) {
-		net_dbg_ratelimited("%s: packet too small from UDP socket\n",
-				    netdev_name(ovpn->dev));
+	reason = pskb_may_pull_reason(skb, sizeof(struct udphdr) +
+				    OVPN_OPCODE_SIZE);
+	if (unlikely(reason != SKB_NOT_DROPPED_YET)) {
+		if (reason == SKB_DROP_REASON_PKT_TOO_SMALL)
+			net_dbg_ratelimited("%s: packet too small from UDP socket\n",
+					    netdev_name(ovpn->dev));
 		goto drop;
 	}
 
 	opcode = ovpn_opcode_from_skb(skb, sizeof(struct udphdr));
 	if (unlikely(opcode != OVPN_DATA_V2)) {
 		/* DATA_V1 is not supported */
-		if (opcode == OVPN_DATA_V1)
+		if (opcode == OVPN_DATA_V1) {
+			reason = SKB_DROP_REASON_UNHANDLED_PROTO;
 			goto drop;
+		}
 
 		/* unknown or control packet: let it bubble up to userspace */
 		return 1;
@@ -119,6 +125,7 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 	if (unlikely(!peer)) {
 		ovpn_dev_estats_inc(ovpn->estats,
 				    OVPN_DEV_ESTAT_RX_NO_PEER);
+		reason = (enum skb_drop_reason)OVPN_DROP_RX_NO_PEER;
 		goto drop;
 	}
 
@@ -130,7 +137,7 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 drop:
 	ovpn_dev_dstats_rx_dropped(ovpn->dev);
 drop_noovpn:
-	kfree_skb(skb);
+	kfree_skb_reason(skb, reason);
 	return 0;
 }
 
include/net/dropreason.h +6 -0
index 1df60645fb278..d520f5996b85a 100644
@@ -29,6 +29,12 @@ enum skb_drop_reason_subsys {
 	 */
 	SKB_DROP_REASON_SUBSYS_QDISC,
 
+	/**
+	 * @SKB_DROP_REASON_SUBSYS_OVPN: OpenVPN data channel drop reasons,
+	 * see drivers/net/ovpn/drop.h
+	 */
+	SKB_DROP_REASON_SUBSYS_OVPN,
+
 	/** @SKB_DROP_REASON_SUBSYS_NUM: number of subsystems defined */
 	SKB_DROP_REASON_SUBSYS_NUM
 };
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel