forked from travelping/upg-vpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupf_pfcp_server.c
1395 lines (1146 loc) · 36.3 KB
/
upf_pfcp_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file
udp upf_pfcp server
*/
#include <math.h>
#include <inttypes.h>
#include <vnet/ip/ip.h>
#include <vnet/udp/udp.h>
#include <vnet/session/session.h>
#include <vnet/session/application_interface.h>
#include <vppinfra/bihash_vec8_8.h>
#include <vppinfra/bihash_template.h>
#include <vppinfra/bihash_template.c>
#include "upf_pfcp.h"
#include "upf_pfcp_api.h"
#include "upf_pfcp_server.h"
#define RESPONSE_TIMEOUT 30
#define TW_SECS_PER_CLOCK 10e-3 /* 10ms */
#define TW_CLOCKS_PER_SECOND (1 / TW_SECS_PER_CLOCK)
#if CLIB_DEBUG > 1
#define upf_debug clib_warning
#define urr_debug_out(format, args...) \
_clib_error (CLIB_ERROR_WARNING, NULL, 0, format, ## args)
#else
#define upf_debug(...) \
do { } while (0)
#define urr_debug_out(...) \
do { } while (0)
#endif
static void upf_pfcp_make_response (pfcp_msg_t * resp, pfcp_msg_t * req);
static void restart_response_timer (pfcp_msg_t * msg);
pfcp_server_main_t pfcp_server_main;
#define MAX_HDRS_LEN 100 /* Max number of bytes for headers */
static void
upf_pfcp_send_data (pfcp_msg_t * msg)
{
app_session_transport_t at;
svm_msg_q_t *mq;
session_t *s;
vlib_main_t *vm = vlib_get_main ();
s = session_get_from_handle_if_valid (msg->session_handle);
if (!s)
return;
mq = session_main_get_vpp_event_queue (s->thread_index);
at.is_ip4 = ip46_address_is_ip4 (&msg->lcl.address);
at.lcl_ip = msg->lcl.address;
at.rmt_ip = msg->rmt.address;
at.lcl_port = msg->lcl.port;
at.rmt_port = msg->rmt.port;
app_send_dgram_raw (s->tx_fifo, &at, mq, msg->data,
_vec_len (msg->data), SESSION_IO_EVT_TX,
1 /* do_evt */ , 0);
/*
* This is needed in case if there are > 0 workers
* as PFCP still runs on the main thread which can't
* process the session queue by itself w/o being
* "nudged" this way
*/
if (vm->thread_index == 0 && vlib_num_workers ())
session_queue_run_on_main_thread (vm);
}
static int
encode_pfcp_session_msg (upf_session_t * sx,
pfcp_decoded_msg_t * dmsg, pfcp_msg_t * msg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
upf_main_t *gtm = &upf_main;
upf_node_assoc_t *n;
int r = 0;
init_pfcp_msg (msg);
n = pool_elt_at_index (gtm->nodes, sx->assoc.node);
msg->seq_no = clib_atomic_add_fetch (&psm->seq_no, 1) % 0x1000000;
msg->node = sx->assoc.node;
msg->session_index = sx - gtm->sessions;
dmsg->seq_no = msg->seq_no;
dmsg->seid = sx->cp_seid;
if ((r = pfcp_encode_msg (dmsg, &msg->data)) != 0)
return r;
msg->session_handle = n->session_handle;
msg->lcl.address = sx->up_address;
msg->rmt.address = sx->cp_address;
msg->lcl.port = clib_host_to_net_u16 (UDP_DST_PORT_PFCP);
msg->rmt.port = clib_host_to_net_u16 (UDP_DST_PORT_PFCP);
upf_debug ("PFCP Session Msg on session 0x%016lx from %U:%d to %U:%d\n",
msg->session_handle,
format_ip46_address, &msg->lcl.address, IP46_TYPE_ANY,
clib_net_to_host_u16 (msg->lcl.port),
format_ip46_address, &msg->rmt.address, IP46_TYPE_ANY,
clib_net_to_host_u16 (msg->rmt.port));
return 0;
}
static int
encode_pfcp_node_msg (upf_node_assoc_t * n,
pfcp_decoded_msg_t * dmsg, pfcp_msg_t * msg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
upf_main_t *gtm = &upf_main;
int r = 0;
init_pfcp_msg (msg);
msg->seq_no = clib_atomic_add_fetch (&psm->seq_no, 1) % 0x1000000;
msg->node = n - gtm->nodes;
dmsg->seq_no = msg->seq_no;
if ((r = pfcp_encode_msg (dmsg, &msg->data)) != 0)
return r;
msg->session_handle = n->session_handle;
msg->lcl.address = n->lcl_addr;
msg->rmt.address = n->rmt_addr;
msg->lcl.port = clib_host_to_net_u16 (UDP_DST_PORT_PFCP);
msg->rmt.port = clib_host_to_net_u16 (UDP_DST_PORT_PFCP);
upf_debug ("PFCP Node Msg on session 0x%016lx from %U:%d to %U:%d\n",
msg->session_handle,
format_ip46_address, &msg->lcl.address, IP46_TYPE_ANY,
clib_net_to_host_u16 (msg->lcl.port),
format_ip46_address, &msg->rmt.address, IP46_TYPE_ANY,
clib_net_to_host_u16 (msg->rmt.port));
return 0;
}
static int
upf_pfcp_server_rx_msg (pfcp_msg_t * msg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
int len = vec_len (msg->data);
if (len < 4)
return -1;
upf_debug ("%U", format_pfcp_msg_hdr, (pfcp_header_t *) msg->data);
if (!pfcp_msg_version_valid (msg->data))
{
pfcp_msg_t resp;
upf_debug ("PFCP: msg version invalid: %d.",
pfcp_msg_version (msg->data));
memset (&resp, 0, sizeof (resp));
upf_pfcp_make_response (&resp, msg);
pfcp_encode_version_not_supported_response (&msg->data);
upf_pfcp_send_data (&resp);
vec_free (resp.data);
return 0;
}
if (!pfcp_msg_enough_len (msg->data, len))
{
upf_debug ("PFCP: msg length invalid, data %d, msg %d.",
len, pfcp_msg_length (msg->data));
return -1;
}
msg->node = ~0;
msg->seq_no = pfcp_msg_seq (msg->data);
switch (pfcp_msg_type (msg->data))
{
case PFCP_HEARTBEAT_REQUEST:
case PFCP_PFD_MANAGEMENT_REQUEST:
case PFCP_ASSOCIATION_SETUP_REQUEST:
case PFCP_ASSOCIATION_UPDATE_REQUEST:
case PFCP_ASSOCIATION_RELEASE_REQUEST:
case PFCP_NODE_REPORT_REQUEST:
case PFCP_SESSION_SET_DELETION_REQUEST:
case PFCP_SESSION_ESTABLISHMENT_REQUEST:
case PFCP_SESSION_MODIFICATION_REQUEST:
case PFCP_SESSION_DELETION_REQUEST:
case PFCP_SESSION_REPORT_REQUEST:
{
uword *p = NULL;
p = mhash_get (&psm->response_q, msg->request_key);
if (!p)
{
upf_pfcp_handle_msg (msg);
}
else
{
pfcp_msg_t *resp = pfcp_msg_pool_elt_at_index (psm, p[0]);
upf_debug ("resend... %d\n", p[0]);
upf_pfcp_send_data (resp);
restart_response_timer (resp);
}
break;
}
case PFCP_HEARTBEAT_RESPONSE:
case PFCP_PFD_MANAGEMENT_RESPONSE:
case PFCP_ASSOCIATION_SETUP_RESPONSE:
case PFCP_ASSOCIATION_UPDATE_RESPONSE:
case PFCP_ASSOCIATION_RELEASE_RESPONSE:
case PFCP_VERSION_NOT_SUPPORTED_RESPONSE:
case PFCP_NODE_REPORT_RESPONSE:
case PFCP_SESSION_SET_DELETION_RESPONSE:
case PFCP_SESSION_ESTABLISHMENT_RESPONSE:
case PFCP_SESSION_MODIFICATION_RESPONSE:
case PFCP_SESSION_DELETION_RESPONSE:
case PFCP_SESSION_REPORT_RESPONSE:
{
pfcp_msg_t *req;
uword *p;
p = hash_get (psm->request_q, msg->seq_no);
upf_debug ("Msg Seq No: %u, %p, idx %u\n", msg->seq_no, p,
p ? p[0] : ~0);
if (!p)
break;
req = pfcp_msg_pool_elt_at_index (psm, p[0]);
hash_unset (psm->request_q, msg->seq_no);
upf_pfcp_server_stop_msg_timer (req);
msg->node = req->node;
pfcp_msg_pool_put (psm, req);
upf_pfcp_handle_msg (msg);
break;
}
default:
break;
}
return 0;
}
static pfcp_msg_t *
build_pfcp_session_msg (upf_session_t * sx, pfcp_decoded_msg_t * dmsg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
pfcp_msg_t *msg;
int r = 0;
msg = pfcp_msg_pool_get (psm);
if ((r = encode_pfcp_session_msg (sx, dmsg, msg)) != 0)
{
pfcp_msg_pool_put (psm, msg);
return NULL;
}
return msg;
}
static pfcp_msg_t *
build_pfcp_node_msg (upf_node_assoc_t * n, pfcp_decoded_msg_t * dmsg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
pfcp_msg_t *msg;
int r = 0;
msg = pfcp_msg_pool_get (psm);
if ((r = encode_pfcp_node_msg (n, dmsg, msg)) != 0)
{
pfcp_msg_pool_put (psm, msg);
return NULL;
}
return msg;
}
int
upf_pfcp_send_request (upf_session_t * sx, pfcp_decoded_msg_t * dmsg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
vlib_main_t *vm = psm->vlib_main;
pfcp_msg_t *msg;
int r = -1;
msg = clib_mem_alloc_aligned_no_fail (sizeof (*msg), CLIB_CACHE_LINE_BYTES);
memset (msg, 0, sizeof (*msg));
if ((r = encode_pfcp_session_msg (sx, dmsg, msg)) != 0)
{
clib_mem_free (msg);
goto out_free;
}
upf_debug ("sending NOTIFY event %p", msg);
vlib_process_signal_event_mt (vm, pfcp_api_process_node.index, EVENT_TX,
(uword) msg);
out_free:
pfcp_free_dmsg_contents (dmsg);
return r;
}
static void
enqueue_request (pfcp_msg_t * msg, u32 n1, u32 t1)
{
pfcp_server_main_t *psm = &pfcp_server_main;
u32 id = pfcp_msg_get_index (psm, msg);
upf_debug ("Msg Seq No: %u, idx %u\n", msg->seq_no, id);
msg->n1 = n1;
msg->t1 = t1;
hash_set (psm->request_q, msg->seq_no, id);
msg->timer =
upf_pfcp_server_start_timer (PFCP_SERVER_T1, msg->seq_no, msg->t1);
msg->expires_at = psm->now + msg->t1;
}
static void
request_t1_expired (u32 seq_no)
{
pfcp_server_main_t *psm = &pfcp_server_main;
upf_main_t *gtm = &upf_main;
pfcp_msg_t *msg;
uword *p;
p = hash_get (psm->request_q, seq_no);
upf_debug ("Msg Seq No: %u, %p, idx %u\n", seq_no, p, p ? p[0] : ~0);
if (!p)
/* msg already processed, overlap of timeout and late answer */
return;
msg = pfcp_msg_pool_elt_at_index (psm, p[0]);
upf_debug ("Msg Seq No: %u, %p, n1 %u\n", msg->seq_no, msg, msg->n1);
if (--msg->n1 != 0)
{
upf_debug ("resend...\n");
msg->timer =
upf_pfcp_server_start_timer (PFCP_SERVER_T1, msg->seq_no, msg->t1);
msg->expires_at = psm->now + msg->t1;
upf_pfcp_send_data (msg);
}
else
{
u8 type = pfcp_msg_type (msg->data);
upf_debug ("abort...\n");
// TODO: handle communication breakdown....
hash_unset (psm->request_q, msg->seq_no);
pfcp_msg_pool_put (psm, msg);
if (type == PFCP_HEARTBEAT_REQUEST
&& !pool_is_free_index (gtm->nodes, msg->node))
{
upf_node_assoc_t *n = pool_elt_at_index (gtm->nodes, msg->node);
pfcp_release_association (n);
}
}
}
static void
upf_pfcp_server_send_request (pfcp_msg_t * msg)
{
enqueue_request (msg, 3, 10);
upf_pfcp_send_data (msg);
}
static void
upf_pfcp_server_send_session_request (upf_session_t * sx,
pfcp_decoded_msg_t * dmsg)
{
pfcp_msg_t *msg;
if ((msg = build_pfcp_session_msg (sx, dmsg)))
{
upf_debug ("Msg: %p\n", msg);
upf_pfcp_server_send_request (msg);
}
}
static void
upf_pfcp_server_send_node_request (upf_node_assoc_t * n,
pfcp_decoded_msg_t * dmsg)
{
pfcp_msg_t *msg;
if ((msg = build_pfcp_node_msg (n, dmsg)))
{
upf_debug ("Node Msg: %p\n", msg);
upf_pfcp_server_send_request (msg);
}
}
static void
response_expired (u32 id)
{
pfcp_server_main_t *psm = &pfcp_server_main;
pfcp_msg_t *msg = pfcp_msg_pool_elt_at_index (psm, id);
upf_debug ("Msg Seq No: %u, %p, idx %u\n", msg->seq_no, msg, id);
upf_debug ("release...\n");
mhash_unset (&psm->response_q, msg->request_key, NULL);
pfcp_msg_pool_put (psm, msg);
}
static void
restart_response_timer (pfcp_msg_t * msg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
u32 id = pfcp_msg_get_index (psm, msg);
upf_debug ("Msg Seq No: %u, idx %u\n", msg->seq_no, id);
upf_pfcp_server_stop_msg_timer (msg);
msg->timer =
upf_pfcp_server_start_timer (PFCP_SERVER_RESPONSE, id, RESPONSE_TIMEOUT);
msg->expires_at = psm->now + RESPONSE_TIMEOUT;
}
static void
enqueue_response (pfcp_msg_t * msg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
u32 id = pfcp_msg_get_index (psm, msg);
upf_debug ("Msg Seq No: %u, idx %u\n", msg->seq_no, id);
mhash_set (&psm->response_q, msg->request_key, id, NULL);
msg->timer =
upf_pfcp_server_start_timer (PFCP_SERVER_RESPONSE, id, RESPONSE_TIMEOUT);
msg->expires_at = psm->now + RESPONSE_TIMEOUT;
}
static void
upf_pfcp_make_response (pfcp_msg_t * resp, pfcp_msg_t * req)
{
resp->timer = ~0;
resp->seq_no = req->seq_no;
resp->session_handle = req->session_handle;
resp->lcl = req->lcl;
resp->rmt = req->rmt;
resp->data = 0;
}
int
upf_pfcp_send_response (pfcp_msg_t * req, pfcp_decoded_msg_t * dmsg)
{
pfcp_server_main_t *psm = &pfcp_server_main;
pfcp_msg_t *resp;
int r;
resp = pfcp_msg_pool_get (psm);
upf_pfcp_make_response (resp, req);
dmsg->seq_no = pfcp_msg_seq (req->data);
if ((r = pfcp_encode_msg (dmsg, &resp->data)) != 0)
pfcp_msg_pool_put (psm, resp);
else
{
upf_pfcp_send_data (resp);
enqueue_response (resp);
}
pfcp_free_dmsg_contents (dmsg);
return r;
}
static int
urr_check_counter (u64 bytes, u64 consumed, u64 threshold, u64 quota)
{
u32 r = 0;
if (quota != 0 && consumed >= quota)
r |= USAGE_REPORT_TRIGGER_VOLUME_QUOTA;
if (threshold != 0 && bytes > threshold)
r |= USAGE_REPORT_TRIGGER_VOLUME_THRESHOLD;
return r;
}
void
upf_pfcp_session_up_deletion_report (upf_session_t * sx)
{
/*
* TS 29.244 clause 5.18.2: UP Function Initiated PFCP Session
* Release FIXME: should include diagnostic information with
* the reason for session termination
*/
pfcp_server_main_t *psm = &pfcp_server_main;
pfcp_decoded_msg_t dmsg = {
.type = PFCP_SESSION_REPORT_REQUEST
};
pfcp_session_report_request_t *req = &dmsg.session_report_request;
struct rules *active;
f64 now = psm->now;
memset (req, 0, sizeof (*req));
SET_BIT (req->grp.fields, SESSION_REPORT_REQUEST_REPORT_TYPE);
active = pfcp_get_rules (sx, PFCP_ACTIVE);
if (vec_len (active->urr) != 0)
{
upf_usage_report_t report;
req->report_type = REPORT_TYPE_USAR;
SET_BIT (req->grp.fields, SESSION_REPORT_REQUEST_USAGE_REPORT);
upf_usage_report_init (&report, vec_len (active->urr));
upf_usage_report_set (&report,
USAGE_REPORT_TRIGGER_TERMINATION_BY_UP_FUNCTION_REPORT,
now);
upf_usage_report_build (sx, NULL, active->urr, now, &report,
&req->usage_report);
upf_usage_report_free (&report);
}
else
req->report_type = REPORT_TYPE_UISR;
SET_BIT (req->grp.fields, SESSION_REPORT_REQUEST_PFCPSRREQ_FLAGS);
/* PSDBU = PFCP Session Deleted By the UP function */
req->pfcpsrreq_flags = PFCPSRREQ_PSDBU;
upf_pfcp_server_send_session_request (sx, &dmsg);
pfcp_free_dmsg_contents (&dmsg);
}
static void
upf_pfcp_session_usage_report (upf_session_t * sx, ip46_address_t * ue,
upf_event_urr_data_t * uev, f64 now)
{
pfcp_decoded_msg_t dmsg = {
.type = PFCP_SESSION_REPORT_REQUEST
};
pfcp_session_report_request_t *req = &dmsg.session_report_request;
upf_main_t *gtm = &upf_main;
u32 si = sx - gtm->sessions;
upf_event_urr_data_t *ev;
upf_usage_report_t report;
struct rules *active;
upf_urr_t *urr;
int send = 0;
active = pfcp_get_rules (sx, PFCP_ACTIVE);
upf_debug ("Active: %p (%d)\n", active, vec_len (active->urr));
if (vec_len (active->urr) == 0)
/* how could that happen? */
return;
memset (req, 0, sizeof (*req));
SET_BIT (req->grp.fields, SESSION_REPORT_REQUEST_REPORT_TYPE);
req->report_type = REPORT_TYPE_USAR;
SET_BIT (req->grp.fields, SESSION_REPORT_REQUEST_USAGE_REPORT);
upf_usage_report_init (&report, vec_len (active->urr));
vec_foreach (ev, uev)
{
urr = pfcp_get_urr_by_id (active, ev->urr_id);
if (!urr)
continue;
if (ev->trigger & URR_START_OF_TRAFFIC)
{
upf_usage_report_trigger (&report, urr - active->urr,
USAGE_REPORT_TRIGGER_START_OF_TRAFFIC,
urr->liusa_bitmap, now);
send = 1;
if (urr->traffic_timer.handle == ~0)
{
upf_pfcp_session_start_stop_urr_time (si, &urr->traffic_timer, 1);
}
}
}
vec_foreach (urr, active->urr)
{
u32 trigger = 0;
upf_debug ("URR: %p\n", urr);
if (urr->status & URR_REPORTED)
/* skip already reported URR */
continue;
#define urr_check(V, D) \
urr_check_counter( \
V.measure.bytes.D, \
V.measure.consumed.D, \
V.threshold.D, \
V.quota.D)
trigger = urr_check (urr->volume, ul);
trigger |= urr_check (urr->volume, dl);
trigger |= urr_check (urr->volume, total);
#undef urr_check
if (trigger != 0)
{
upf_usage_report_trigger (&report, urr - active->urr, trigger,
urr->liusa_bitmap, now);
urr->status |= URR_REPORTED;
send = 1;
}
}
if (send)
{
upf_usage_report_build (sx, ue, active->urr, now, &report,
&req->usage_report);
upf_pfcp_server_send_session_request (sx, &dmsg);
}
pfcp_free_dmsg_contents (&dmsg);
upf_usage_report_free (&report);
}
void
upf_pfcp_session_stop_up_inactivity_timer (urr_time_t * t)
{
pfcp_server_main_t *psm = &pfcp_server_main;
if (t->handle != ~0 && t->expected > vlib_time_now (psm->vlib_main))
TW (tw_timer_stop) (&psm->timer, t->handle);
t->handle = ~0;
t->expected = 0;
}
void
upf_pfcp_session_start_up_inactivity_timer (u32 si, f64 last, urr_time_t * t)
{
pfcp_server_main_t *psm = &pfcp_server_main;
i64 interval;
f64 period;
if (t->handle != ~0 || t->period == 0)
return;
// start timer.....
period = t->period - trunc (vlib_time_now (psm->vlib_main) - last);
t->expected = vlib_time_now (psm->vlib_main) + period;
interval = psm->timer.ticks_per_second * period;
interval = clib_max (interval, 1); /* make sure interval is at least 1 */
t->handle = TW (tw_timer_start) (&psm->timer, si, 0, interval);
upf_debug
("starting UP inactivity timer on sidx %u, handle 0x%08x: "
"now is %.4f, expire in %lu ticks "
" clib_now %.4f, current tick: %u",
si, t->handle, psm->timer.last_run_time, interval,
unix_time_now (), psm->timer.current_tick);
}
void
upf_pfcp_session_stop_urr_time (urr_time_t * t, const f64 now)
{
pfcp_server_main_t *psm = &pfcp_server_main;
if (t->handle != ~0 && t->expected > now)
{
/* The timer wheel stop expired timers automatically. We don't map
* expired timers to their urr_time_t structure, therefore the handle
* might already be reused.
* Only stop the timer if we are sure that it can not possibly have
* expired yet.
* Failing to stop a timer is not a problem. The timer will fire, but
* the URR scan woun't find any expired URRs.
*/
TW (tw_timer_stop) (&psm->timer, t->handle);
}
t->handle = ~0;
t->expected = 0;
}
void
upf_pfcp_session_start_stop_urr_time (u32 si, urr_time_t * t, u8 start_it)
{
pfcp_server_main_t *psm = &pfcp_server_main;
/* the timer interval must be based on tw->current_tick, so for calculating that
* we need to use the now timestamp of that current_tick */
const f64 now = psm->timer.last_run_time;
if (t->handle != ~0)
upf_pfcp_session_stop_urr_time (t, now);
if (t->period != 0 && start_it)
{
i64 interval;
// start timer.....
t->expected = t->base + t->period;
interval = psm->timer.ticks_per_second * (t->expected - now) + 1;
interval = clib_max (interval, 1); /* make sure interval is at least 1 */
t->handle = TW (tw_timer_start) (&psm->timer, si, 0, interval);
upf_debug
("starting URR timer on sidx %u, handle 0x%08x: "
"now is %.4f, base is %.4f, expire in %lu ticks "
" @ %.4f (%U), clib_now %.4f, current tick: %u",
si, t->handle, now, t->base, interval,
t->expected, format_time_float, NULL, t->expected,
unix_time_now (), psm->timer.current_tick);
}
}
static void
upf_pfcp_session_urr_timer (upf_session_t * sx, f64 now)
{
pfcp_decoded_msg_t dmsg = {
.type = PFCP_SESSION_REPORT_REQUEST
};
pfcp_session_report_request_t *req = &dmsg.session_report_request;
upf_main_t *gtm = &upf_main;
u32 si = sx - gtm->sessions;
upf_usage_report_t report;
struct rules *active;
u32 idx;
#if CLIB_DEBUG > 1
f64 vnow = vlib_time_now (gtm->vlib_main);
#endif
active = pfcp_get_rules (sx, PFCP_ACTIVE);
upf_debug ("upf_pfcp_session_urr_timer (%p, 0x%016" PRIx64 " @ %u, %.4f)\n"
" UP Inactivity Timer: %u secs, inactive %12.4f secs (0x%08x)",
sx, sx->cp_seid, sx - gtm->sessions, now,
active->inactivity_timer.period,
vlib_time_now (gtm->vlib_main) - sx->last_ul_traffic,
active->inactivity_timer.handle);
memset (req, 0, sizeof (*req));
SET_BIT (req->grp.fields, SESSION_REPORT_REQUEST_REPORT_TYPE);
if (active->inactivity_timer.handle != ~0 &&
active->inactivity_timer.period != 0)
{
if (ceil (vlib_time_now (gtm->vlib_main) - sx->last_ul_traffic) >=
active->inactivity_timer.period)
{
active->inactivity_timer.handle = ~0;
req->report_type |= REPORT_TYPE_UPIR;
}
else
{
upf_pfcp_session_stop_up_inactivity_timer
(&active->inactivity_timer);
upf_pfcp_session_start_up_inactivity_timer (si, sx->last_ul_traffic,
&active->inactivity_timer);
}
}
upf_usage_report_init (&report, vec_len (active->urr));
vec_foreach_index (idx, active->urr)
{
upf_urr_t *urr = vec_elt_at_index (active->urr, idx);
f64 trigger_now = now;
u32 trigger = 0;
#define urr_check(V, NOW) \
(((V).base != 0) && ((V).period != 0) && \
((V).expected != 0) && (V).expected < (NOW))
#define URR_COND_TIME(t, time) \
(t).period != 0 ? time : 0
#define URR_DEBUG_HEADER \
"Rule | base | period | expire at | in secs | ticks | handle | check result\n"
#define URR_DEUBG_LINE "%-10s | %U (%9.3f) | %8lu | %U | %9.3f, %9.3f | %9.3f | 0x%08x | %u\n"
#define URR_DEUBG_ABS_LINE "%-10s | %U | %12.4f | %U | %12.4f\n"
#define URR_DEBUG_VALUES(Label, t) \
(Label), \
format_time_float, NULL, (t).base, \
URR_COND_TIME(t, ((t).base - now)), \
(t).period, \
format_time_float, NULL, (t).base + (f64)(t).period, \
URR_COND_TIME(t, (((t).base + (f64)(t).period) - now)), \
URR_COND_TIME(t, ((t).expected - now)), \
URR_COND_TIME(t, ((t).expected - now) * TW_CLOCKS_PER_SECOND), \
(t).handle, urr_check(t, now)
#define URR_DEBUG_ABS_VALUES(Label, t) \
(Label), \
format_time_float, NULL, (t).unix_time, \
(t).unix_time - now, \
format_vlib_time, gtm->vlib_main, (t).vlib_time, \
(t).vlib_time - vnow
upf_debug ("URR: %p, Id: %u", urr, urr->id);
urr_debug_out
(URR_DEBUG_HEADER
URR_DEUBG_LINE
URR_DEUBG_LINE
URR_DEUBG_LINE
URR_DEUBG_ABS_LINE,
URR_DEBUG_VALUES ("Period", urr->measurement_period),
URR_DEBUG_VALUES ("Threshold", urr->time_threshold),
URR_DEBUG_VALUES ("Quota", urr->time_quota),
URR_DEBUG_ABS_VALUES ("Monitoring", urr->monitoring_time));
if (urr_check (urr->measurement_period, now))
{
if (urr->triggers & REPORTING_TRIGGER_PERIODIC_REPORTING)
{
trigger |= USAGE_REPORT_TRIGGER_PERIODIC_REPORTING;
trigger_now =
clib_min (trigger_now, urr->measurement_period.expected);
}
urr->measurement_period.base += urr->measurement_period.period;
if ((urr->measurement_period.base + urr->measurement_period.period) <
now)
{
clib_warning
("WARNING: URR %p, Measurement Period wrong, Session 0x%016"
PRIx64 ", URR: %u\n" URR_DEBUG_HEADER URR_DEUBG_LINE, urr,
sx->cp_seid, urr->id, URR_DEBUG_VALUES ("Period",
urr->measurement_period));
#if CLIB_DEBUG > 0
ASSERT ((urr->measurement_period.base +
urr->measurement_period.period) < now);
#endif
while ((urr->measurement_period.base +
urr->measurement_period.period) < now)
{
urr->measurement_period.base +=
urr->measurement_period.period;
}
}
/* rearm Measurement Period */
upf_pfcp_session_start_stop_urr_time
(si, &urr->measurement_period, 1);
}
if (urr_check (urr->time_threshold, now))
{
if (urr->triggers & REPORTING_TRIGGER_TIME_THRESHOLD)
{
trigger |= USAGE_REPORT_TRIGGER_TIME_THRESHOLD;
trigger_now =
clib_min (trigger_now, urr->time_threshold.expected);
}
upf_pfcp_session_stop_urr_time (&urr->time_threshold, now);
}
if (urr_check (urr->time_quota, now))
{
if (urr->triggers & REPORTING_TRIGGER_TIME_QUOTA)
{
trigger |= USAGE_REPORT_TRIGGER_TIME_QUOTA;
trigger_now = clib_min (trigger_now, urr->time_quota.expected);
}
upf_pfcp_session_stop_urr_time (&urr->time_quota, now);
urr->time_quota.period = 0;
urr->status |= URR_OVER_QUOTA;
}
if (urr_check (urr->quota_validity_time, now))
{
if (urr->triggers & REPORTING_TRIGGER_QUOTA_VALIDITY_TIME)
{
trigger |= USAGE_REPORT_TRIGGER_QUOTA_VALIDITY_TIME;
trigger_now =
clib_min (trigger_now, urr->quota_validity_time.expected);
}
upf_pfcp_session_stop_urr_time (&urr->quota_validity_time, now);
urr->quota_validity_time.period = 0;
urr->status |= URR_OVER_QUOTA;
}
if (urr_check (urr->traffic_timer, now))
{
upf_urr_traffic_t **expired = NULL;
upf_urr_traffic_t *tt = NULL;
pool_foreach (tt, urr->traffic)
{
if (tt->first_seen + 60 < now)
vec_add1 (expired, tt);
};
for (int i = 0; i < vec_len (expired); i++)
{
hash_unset_mem_free (&urr->traffic_by_ue, &expired[i]->ip);
pool_put (urr->traffic, expired[i]);
}
vec_free (expired);
if (pool_elts (urr->traffic) != 0)
upf_pfcp_session_start_stop_urr_time (si, &urr->traffic_timer, 1);
}
#undef urr_check
if (trigger != 0)
{
req->report_type |= REPORT_TYPE_USAR;
SET_BIT (req->grp.fields, SESSION_REPORT_REQUEST_USAGE_REPORT);
upf_usage_report_trigger (&report, idx, trigger, urr->liusa_bitmap,
trigger_now);
// clear reporting on the time based triggers, until rearmed by update
urr->triggers &= ~(REPORTING_TRIGGER_TIME_THRESHOLD |
REPORTING_TRIGGER_TIME_QUOTA |
REPORTING_TRIGGER_QUOTA_VALIDITY_TIME);
}
}
if (req->report_type != 0)
{
upf_usage_report_build (sx, NULL, active->urr, now, &report,
&req->usage_report);
upf_pfcp_server_send_session_request (sx, &dmsg);
}
pfcp_free_dmsg_contents (&dmsg);
upf_usage_report_free (&report);
}
#if CLIB_DEBUG > 10
static void
upf_validate_session_timer (upf_session_t * sx)
{
f64 now = unix_time_now ();
struct rules *r;
upf_urr_t *urr;
int error = 0;
#define urr_check(V, NOW) (~0 != (V).handle)
r = pfcp_get_rules (sx, PFCP_PENDING);
vec_foreach (urr, r->urr)
{
if (!urr_check (urr->measurement_period, now) &&
!(urr->monitoring_time.vlib_time != INFINITY) &&
!urr_check (urr->time_threshold, now) &&
!urr_check (urr->time_quota, now))
continue;
error++;
clib_warning
("WARNING: Pending URR %p with active timer handler, Session 0x%016"
PRIx64 ", URR: %u\n" URR_DEBUG_HEADER URR_DEUBG_LINE URR_DEUBG_LINE
URR_DEUBG_LINE URR_DEUBG_ABS_LINE, urr, sx->cp_seid, urr->id,
URR_DEBUG_VALUES ("Period", urr->measurement_period),
URR_DEBUG_VALUES ("Threshold", urr->time_threshold),
URR_DEBUG_VALUES ("Quota", urr->time_quota));