55 Commits

Author SHA1 Message Date
Pau Espin Pedrol
a2289c0730 Increase per PDR (F-TEID) hashtable size
In tunmap scenario there's usually 2 PDRs/local-F-TEIDs per session,
hence it makes sense to at least double the hashtable size with respect
to session hashtable. In here we actually multiply it by 2 twice since
this is actually global and not per peer, hence we account for multiple
peers.

Change-Id: I7ee3e875020d707c05e13ee70952174ca4daad30
2025-02-18 19:36:44 +01:00
Pau Espin Pedrol
d2241da833 up_peer: Increase up/cp seid hashtable sizes
During a 200K session setup, this can still take quite a noticeable
amount of time (20% of a full CPU) during session creation.

Change-Id: I424778a4c75000fb67ba4725a6a3e3e91a80fc76
2025-02-18 18:59:41 +01:00
Pau Espin Pedrol
68c0e96a81 Introduce hashtable to look up session in endpoint by UP SEID
This is used every time a new PFCP session was being allocated.
It allows looking up if a given UP SEID exists in any of the peers.
Before this patch, iterating over tens/hundreds of UP peers on every
session creation took 38% of a full CPU.

Change-Id: I617b405e59a3435a1c4912bf4161bc9e036754b4
2025-02-18 18:59:08 +01:00
Pau Espin Pedrol
b54baeff98 up_peer: Document field
Change-Id: I194074f657fb793681658c021579880f352a3f91
2025-02-18 18:48:02 +01:00
Pau Espin Pedrol
b6d689d677 Introduce hashtable to look up gtp_tundev by local TEID
Use this hashtable while looking up for tunend based on
<access.local.teid, access.remote.teid, access.remote.addr>.
This kind of look up is used every time a session is added or removed,
which means potentially thousands of tunend sessions were being iterated
linerarly every time.

For simplification (easier/quicker hashtable key generation), reduce the
whole key presented above to a more general one based on
"access.local.teid". This is usually enough since we are anyways
allocating local TEIDs globally per tunnel without caring about remote
address.

Change-Id: Ib12ecc8ce87175071c52c0ed2217a29d901f0f05
2025-02-17 19:33:13 +01:00
Pau Espin Pedrol
bede7f130f Introduce hashtable to lookup chain_id
This lookup was taking ages specially when UPF already managed thousands
of sessions.

Related: SYS#6398
Change-Id: I7df8fd945eedbda98bd08e9fb2f382e0f55c2983
2025-01-31 19:13:27 +01:00
Pau Espin Pedrol
c27e317b21 Introduce hashtable to lookup session by F-TEID
This is a hot path when creating new sessions. Previous code would take
ages specially when UPF already had thousands of sessions created and a
new TEID to allocate was being looked up.

Related: SYS#6398
Change-Id: I90ecbb07b242c1de2298261019f24aa5f5810fda
2025-01-31 19:12:42 +01:00
Pau Espin Pedrol
63c98c38f1 Remove unused file up_session_to_gtp.c
Change-Id: Iabdf4855be72759569d1ac254774311f8682a2df
2025-01-31 15:35:28 +01:00
Neels Janosch Hofmeyr
9c904c185a osmo-upf: add VTY 'gtp-echo' command
Allow sending GTPv1-U Echo Requests to GTP peers via new VTY command

  gtp1u-echo send to (A.B.C.D|X:X::X:X)
  gtp1u-echo send to (A.B.C.D|X:X::X:X) local-ip (A.B.C.D|X:X::X:X)
  gtp1u-echo send to (A.B.C.D|X:X::X:X) local-dev DEV_NAME

Related: OS#6541
Tweaked-by: Oliver Smith <osmith@sysmocom.de>
Change-Id: I970dccd7a27b098eea9e660822e24e2c4b059fc6
2024-11-05 11:05:21 +01:00
Neels Janosch Hofmeyr
b3d4ca6d80 nft: batch nftables commands
Store nftables ruleset commands for a limited time and a limited volume
before submitting in batch, in order to minimize the overhead associated
with submitting nftables commands.

Configurable by 'timer nft X32' and 'X33'

Change-Id: Ib0a8e86b29bab1559d94fc55a89daa00ec670318
2024-10-24 11:20:24 +00:00
Neels Janosch Hofmeyr
40a30fce4a tunmap: ensure assigned chain_id is unused
When handing out a chain_id, make sure it is not in use yet.

So far picking a chain_id was of PoC grade quality. As osmo-upf is
approaching production grade, make this waterproof.

So far with inefficient iteration of all sessions; faster lookup follows
in I36a75ec4698cd83558185c1f202400eb53ae8ff6.

Related: OS#5900
Change-Id: I139b46de0bd15185a7a06109d55f7c759755ec81
2023-03-18 01:33:05 +00:00
Neels Janosch Hofmeyr
27a90869c7 add unique_ids_test.c
Verify that skipping used IDs works for:
- PFCP UP-SEID
- GTP local TEID
- chain_id for nft rulesets -- so far expected to fail,
  fix follows in I139b46de0bd15185a7a06109d55f7c759755ec81.

Related: OS#5900
Change-Id: I36acff15f22d23ade4d281c2af3eb117dfc10359
2023-03-18 01:33:05 +00:00
Neels Janosch Hofmeyr
36cca044c4 cosmetic: rename next_seid to next_up_seid
There are UP-SEID and CP-SEID. Only UP-SEID are chosen by the UPF.
Clarify naming.

Change-Id: Ib725857079400accb4781f8a91eca6495b6b92a9
2023-02-24 01:52:02 +01:00
Neels Janosch Hofmeyr
8e17c9933c move next_teid from up_endpoint to g_upf
up_endpoint is about the PFCP endpoint, handing out local TEID is about
local GTP endpoints. Move the TEID allocation to g_upf / upf.c.

An upcoming patch will use a hash table in g_upf to speed up lookup
whether a local TEID is already in use; cosmetically prepare for that.

Change-Id: I8eae5b53c563400ddfded264678d9cfb28b6f737
2023-02-24 01:52:02 +01:00
Neels Janosch Hofmeyr
1961cf90b5 cosmetic: rename g_upf->gtp to tunend, ->nft to tunmap
Upcoming patch I8eae5b53c563400ddfded264678d9cfb28b6f737 will introduce
a g_upf->gtp sub struct for more generally GTP related things (local
TEID assignment).

Change-Id: I74df838af50f38604e2ff06cac0af11ccfdab386
2023-02-24 01:51:06 +01:00
Neels Janosch Hofmeyr
0a87f42f10 cosmetic: reduce dup in tunnel struct definitions
Use a common struct upf_tun_ep and struct upf_tun for both tunend and
tunmap definitions, with a nicer local / remote sub-structuring.

Change-Id: I07866e2acbeb74914e1fd6f66839a5a8ae247b1e
2023-02-22 16:05:03 +01:00
Neels Janosch Hofmeyr
75c07af406 cosmetic: simplify naming: struct upf_tunmap, struct upf_tunend
The "desc" has no meaning, every struct is a description of its data.

The "nft" and "gtp" hint at the specific "nftables" and "GTP kernel
module" implementations. I'd rather keep it more abstract and shorter.
That serves removing dup of shared bits in an upcoming patch.

Change-Id: I15e4552a20067265abb8d2dd716861cd50270028
2023-02-22 16:05:03 +01:00
Neels Janosch Hofmeyr
a2f2650786 minor api doc
Change-Id: I2291e34545844d3a6b82c0e9a3278bb422cc890c
2023-02-22 16:05:03 +01:00
Neels Janosch Hofmeyr
4e1c680e59 tunmap: refactor nft ruleset: fix "martians" and "1024"
Take care of two problems:
- limitation of <= 1024 base chains in nftables, so far meaning we can
  establish at most 1024 GTP tunnel mappings.
- mangling of source IP in prerouting so far meaning that the system
  needs to be configured to permit 'martian' packets

The new ruleset separates in pre- and post-routing, so that we set a new
destination IP address in pre-routing, and set a new source IP address
in post-routing. Hence no problem with martian packet rejection.

The new ruleset uses verdict maps, which are more efficient, and do not
hit a limit of 1024 as base chains do.

Before, the nft rule used one chain id. In the new ruleset, each tunmap
now needs two distinct chain ids. Refactor.

Related: SYS#6327 SYS#6264
Change-Id: Iccb975a1c0f8a2087f7b7dc4942a6b41f5675a13
2023-02-09 18:14:09 +01:00
Neels Janosch Hofmeyr
091603c4a4 deprecate cfg 'nft rule tunmap append'
Subsequent patch will refactor the tunmap nft ruleset. Instead of
adapting the 'tunmap append' feature to the new ruleset, rather drop
this feature entirely.

The 'nft rule tunmap append' was intended for enabling 'trace' in the
nft ruleset. However, the same can be achieved via the nft cmdline tool.
For example:

 sudo nft 'add chain filter trace_chain { type filter hook prerouting priority -301; }'
 sudo nft 'add rule filter trace_chain meta nftrace set 1'

Related: SYS#6327 SYS#6264
Change-Id: I1ae36f2f520217254c81fd765d27333ff0f457b2
2023-02-09 00:13:08 +01:00
Neels Janosch Hofmeyr
fae0ed6d24 move GTP port definitions to upf.h
upf_gtp.h is for the GTP kernel module interaction. The GTP port numbers
are also relevant for the netfilter part, upf_nft.h. An upcoming patch
will use PORT_GTP1_U in the nft ruleset.

Related: SYS#6327 SYS#6264
Change-Id: I37d13cfee225c7ee2cc45525b76d9579d65e847c
2023-02-09 00:13:08 +01:00
Neels Janosch Hofmeyr
2a2884fbbe tunend: choose local GTP addr by Network Instance IEs
Implement handling of the Network Instance IEs from PFCP for tunend,
like already done for tunmap.

In 'tunend' cfg, allow indicating a local GTP address for both 'dev
create' and 'dev use'. Select a GTP device by the local address the
Network Instance IE in PFCP PDR indicates.

Related: SYS#6192
Change-Id: I376c09bfc1844df1e61d2efac17561fac614858b
2023-01-06 00:18:40 +01:00
Neels Janosch Hofmeyr
8525c49c5d add cfg: tunmap / nft-rule append
It can be useful to add 'meta nftrace set 1' to nftables rules to help
analysis / site debugging. Add the possibility to do this by cfg.

Instead of adding the fixed string of 'meta nftrace set 1', allow
appending arbitrary strings to the nftables rules, to accomodate any
other future tweaks that may be useful.

Related: SYS#6192
Change-Id: Ia1fac67108902a48b43d8d1dc184ccf541fd9ba8
2022-12-09 18:28:19 +01:00
Neels Janosch Hofmeyr
eb8361f4c5 nft: allow to get the ruleset string without running
Separate string composition of the nftables ruleset from the actual
actvation of the ruleset to nftables.

For a 'show' VTY command added in upcoming patch, I'd like to be able to
vty_out() an nftables rule set. Provide API for that.

Change-Id: I0124a68ccf1ac7b90c5cc32d0cbf58d0cc219ccc
2022-12-09 18:28:19 +01:00
Neels Janosch Hofmeyr
08af1f15f8 nft: ensure to assign rule id only once
Make sure an assigned id is not overwritten.

So far this function was guaranteed to be called only once. But I would
like to allow getting the nftables ruleset string more than once in a
future patch. Prepare that.

Change-Id: I4e8c48c01fb2f5d4cfd223fe03abbf15b1a55670
2022-12-09 18:28:19 +01:00
Neels Janosch Hofmeyr
629647a535 in GTP actions, also store local GTP addrs
At first, this mostly improves logging of GTP actions.

Subsequently, we will use these to:
- for tunend, pick a GTP device based on the local interface.
- for tunmap, change the netfilter rules to match on the *local* GTP
  address instead of the remote one.

Related: SYS#6192
Change-Id: I8488c478c4790d3882b22dcdb1f127838e23dd7b
2022-12-09 17:25:58 +00:00
Neels Janosch Hofmeyr
3572241df5 tunmap: choose local GTP addr by Network Instance IEs
Add 'netinst' config section to osmo-upf.cfg, to define Network Instance
name to local IP address mappings.

For the tunmap use case (forwarding GTP tunnels), heed the Network
Instance IEs in PFCP session creation and return IP addresses in F-TEIDs
accordingly.

Related: SYS#6192
Related: I37bebc7d6ef75c3e6ae05e81b83a1b5895839a64 (osmo-ttcn3-hacks)
Change-Id: I15ee046a1c37b83b8a83527a67a6215a30106d81
2022-12-09 17:25:58 +00:00
Neels Janosch Hofmeyr
e68eca0e8f clarify comments and naming around PDR+FAR classification
No functional change.

Rename forw_to_core to access_to_core.
Rename forw_from_core to core_to_access.

Rename add_gtp_action_endecaps to add_gtp_action_tunend.
Rename add_gtp_action_forw to add_gtp_action_tunmap.

Add assertions to clearly indicate expected PDR and reverse PDR
directions.

Tweak various comments and log messages.

Fix some comments that have Access / Core flipped.

Change-Id: Ia199bb6944476eff6af89b5ab015a9a2f8ce330e
2022-12-09 17:25:58 +00:00
Neels Janosch Hofmeyr
d7f683a66c VTY 'show gtp': more accurately identify local/remote IP
Indicate whether a shown IP address is local or remote, by adding '-l'
or '-r' to the field names shown.

So far, osmo-upf is only tracking remote GTP addrs, but we are about to
implement choosing local GTP addrs by Network Instance IEs. Those should
also be shown and will need to be set apart from the remote addresses.

Related: I440466f1cc9689391869ac2579a4497ef6008adb (osmo-ttcn3-hacks)
Change-Id: Ic539ebe84a0853f665e5b8b8489dd587e6907287
2022-12-09 17:25:58 +00:00
Neels Janosch Hofmeyr
28180a6246 cosmetic: rename upf_gtp_dev_tunnel_* to upf_gtp_dev_tunend_*
Change-Id: I73e7b3c4841520909185aaba3ec41c6cf1e3ff51
2022-11-19 00:10:14 +01:00
Neels Janosch Hofmeyr
0fca3412d8 drop unused upf_gtp_dev_is_tunnel_active()
Change-Id: Ia8517f702118af55ce47a4f63fb08ac5ee284217
2022-11-19 00:09:56 +01:00
Neels Janosch Hofmeyr
b183aa84af cosmetic: rename upf_gtp_tun to upf_gtp_tunend
Change-Id: I0815012679237838a031e28c0afb98b7e0d184bd
2022-11-19 00:03:28 +01:00
Neels Janosch Hofmeyr
527f1b3b94 cosmetic: in code, rename 'endecaps' to 'tunend'
Change-Id: I55ce7cc842f36b2528e6a1a15d6f3bcb960f492b
2022-11-18 23:49:14 +01:00
Neels Janosch Hofmeyr
95eb2c6a89 VTY: rename 'gtp' to 'tunend'
So far the config nodes were named after the implementation:
"GTP kernel module" = "gtp" and
"netfilter" = "nft"

We found that this is confusing, since both are related to handling GTP.
Rename "gtp" to "tunend"; a subsequent patch will rename "nft" to
"tunmap".

Keep a hidden "gtp" VTY cmd as backwards compat alias.

In log output, also print "tunend" instead of "endecaps"
(up_gtp_action_to_str_buf()).

Related: SYS#6192
Change-Id: I49ac7b1f8b5b74f586edfed1dfb29f9af55a521b
2022-11-18 23:49:14 +01:00
Neels Janosch Hofmeyr
a3b5488b69 drop unused enum up_session_kind
Change-Id: I10e17338485b11d6c03da209c70323f69f93fc2e
2022-11-18 23:49:14 +01:00
Neels Janosch Hofmeyr
114277cff7 Allow running without a GTP dev
Allow running without opening a GTP dev for encapsulation/decapsulation.
Probe and open the mnl socket for talking to the GTP kernel module only
when actual GTP devices exist in the config.

A site that is only doing tunnel proxying via netfilter hence does not
require GTP support in the kernel.

Change-Id: Ibb79b3ce1906136f77a895ff6f691d72a92c9fb9
2022-08-30 14:21:14 +00:00
Neels Hofmeyr
0388579be9 implement GTPv1-U ECHO response
Accept data on the GTPv1-U socket and respond to GTPv1-U ECHO REQUEST
messages.

We should keep a deterministic recovery counter that increases with
every restart. As a quick and dirty way just use the current time at
startup for now, until osmo-upf reaches production maturity.

Related: OS#5599
Change-Id: I135370a7723e2c667ec681f50c21107cde63ea5b
2022-07-22 17:18:24 +02:00
Neels Hofmeyr
06482c6554 implement GTP tunnel mapping via netfilter
Implement support for PFCP rulesets that ask for mapping a GTP tunnel:
forwarding GTP payload between two GTP tunnels.

For a GTP tunnel mapping, dispatch netfilter rules that detect GTP
packets with a given source address and TEID, and replace the TEID and
destination address according to the PFCP ruleset.

The netfilter implementation is chosen to effect the packet rewriting
and forwarding to take place directly in the kernel, for high throughput
of GTP packets.

Related: SYS#5599
Change-Id: Ic0d319eb4f98cd51a5999c804c4203ab0bdda650
2022-07-20 17:08:53 +02:00
Neels Hofmeyr
bee02fc34f add VTY option gtp/mockup, for VTY tests
To avoid actions that require cap_net_admin permissions on build
servers, add this option to "dry run" all kernel GTP actions. Same will
be added for netfilter rules.

On startup, osmo-upf opens sockets to GTP kernel module / NFT ctx.
However, on build servers, this would require giving cap_net_admin
permissions just to run the VTY tests.

Related: SYS#5599
Change-Id: I3b9c796186307fd8562abcff3f0ccfab0e88b6c8
2022-07-20 17:07:48 +02:00
Neels Hofmeyr
2eeec08d1e add osmo-upf
Related: SYS#5599
Change-Id: I745bcbde6859004c41ddbfd2558036bf9a2d1de2
2022-06-19 14:13:28 +02:00
Neels Hofmeyr
f93859548f move libosmo-pfcp to libosmo-pfcp.git
The first user of this is osmo-hnbgw, to implement GTP mapping via a
UPF.

Related: SYS#5895
Change-Id: I1464cdd846b00707b0abba9126aa5bb784b7caf1
2022-06-17 16:59:15 +02:00
Neels Hofmeyr
3574c79194 move libosmo-gtlv to libosmo-pfcp.git
Related: SYS#5599
Change-Id: Id72cdf94da60d4b6d09d0044c74e672c4412c15d
2022-06-17 16:59:15 +02:00
Neels Hofmeyr
bff9378f25 add pfcp_endpoint
Related: SYS#5599
Change-Id: Ic8d42e201b63064a71b40ca45a5a40e29941e8ac
2022-06-09 23:40:20 +02:00
Neels Hofmeyr
d212f0ac7b libosmo-pfcp: implement PFCP header and msg handling
Related: SYS#5599
Change-Id: I3f85ea052a6b7c064244a8093777e53a47c8c61e
2022-06-07 02:23:07 +02:00
Neels Hofmeyr
3274cc3398 api: add osmo_pfcp_ie_node_id_to_str_c()
So far we had only osmo_pfcp_enc_to_str_node_id(), used for PFCP message
to string conversion. It behaves like a common _to_str_buf() function,
but has an inconvenient void* arg (for use with libosmo-tlv).

Implement the string conversion as common _to_str_buf() and _to_str_c()
functions, and call that from osmo_pfcp_enc_to_str_node_id(). That's
useful for log messages coming up in a subsequent patch.

Related: SYS#5599
Change-Id: I5c580bc510afce58a03dea0861db9630b063b2ae
2022-06-07 02:13:28 +02:00
Neels Hofmeyr
bd9747a591 pfcp ie: tweak CP Function Features
The spec indicates three bytes of CP Function Features, but both
wireshark and ttcn3 expect only one byte. This makes sense because only
eight CP F.F. flags are defined.

Drop those two always-zero bytes, hence pass the wireshark dissector and
ttcn3 parsing without warnings.

Related: SYS#5599
Change-Id: Icda891a2f3401e58f142f229465403d5dc8befe5
2022-06-07 02:08:29 +02:00
Neels Hofmeyr
897581e06d pfcp/Makefile.am: add missing pfcp_ies_auto.h entry
Even though it is a generated header, it must still be listed in
pfcp_HEADERS.

Related: SYS#5599
Change-Id: I6fbfe1fcd084f2d16334bb3e44d9891d9485d59f
2022-06-07 02:08:19 +02:00
Neels Hofmeyr
9267debbeb libosmo-pfcp: implement/generate TLV and IE value coding
Related: SYS#5599
Change-Id: I3069045b2d42dac88d955c636230adc64a7a4aa7
2022-04-01 12:17:34 +02:00
Neels Hofmeyr
e1abe10cfa libosmo-pfcp: add pfcp_proto.h pfcp_strs.h
Related: SYS#5599
Change-Id: I568b821e89007ed52eeefcdbcb6edd8052a8b5be
2022-04-01 12:17:34 +02:00
Neels Hofmeyr
d879afd381 libosmo-gtlv: add TLIV capability
During code review, it was indicated that some TLV protocols that we
will likely deal with in the near future also employ an I, and instance
value of a tag. Add TLIV support.

A usage example for a manually implemented TLIV structure is found in
tests/libosmo-gtlv/gtlv_test.c.

A usage example for a generated TLIV protocol is found in
tests/libosmo-gtlv/test_tliv/.

Related: SYS#5599
Change-Id: I0a076e54dfba6038cc779cb7c8f3967d212226aa
2022-03-22 00:27:08 +01:00