-
Notifications
You must be signed in to change notification settings - Fork 46
/
flow_span.cc
896 lines (740 loc) · 28.3 KB
/
flow_span.cc
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <reducer/matching/component.h>
#include <reducer/matching/flow_span.h>
#include <reducer/matching/matching_core.h>
#include <reducer/constants.h>
#include <reducer/copy_metrics.h>
#include <reducer/uid_key.h>
#include <generated/ebpf_net/matching/containers.inl>
#include <generated/ebpf_net/matching/index.h>
#include <generated/ebpf_net/matching/modifiers.h>
#include <generated/ebpf_net/matching/spans.h>
#include <generated/ebpf_net/metrics.h>
#include <common/client_server_type.h>
#include <util/cgroup_parser.h>
#include <util/error_handling.h>
#include <util/ip_address.h>
#include <util/log.h>
#include <util/string_view.h>
#include <cassert>
#include <optional>
#include <string>
namespace reducer::matching {
namespace {
constexpr std::string_view kNoAgentEnvironmentName = "(no agent)";
static const IPv6Address kAddrInstanceMetadata = IPv6Address::from_host_hextets({0, 0, 0, 0, 0, 0xffff, 0xa9fe, 0xa9fe});
// Returns the underlying value if it is set, otherwise returns a
// default-constructed object.
template <typename T> const T &get_or_default(const std::optional<T> &value)
{
static const auto *const kDefault = new T;
return value.has_value() ? *value : *kDefault;
}
} // namespace
bool FlowSpan::aws_enrichment_enabled_ = false;
void FlowSpan::enable_aws_enrichment(bool enabled)
{
aws_enrichment_enabled_ = enabled;
}
FlowSpan::FlowSpan() {}
FlowSpan::~FlowSpan() {}
void FlowSpan::agent_info(::ebpf_net::matching::weak_refs::flow span_ref, u64 timestamp, jsrv_matching__agent_info *msg)
{
std::string id{msg->id.buf, msg->id.len};
std::string az{msg->az.buf, msg->az.len};
std::string env{msg->env.buf, msg->env.len};
std::string role{msg->role.buf, msg->role.len};
std::string ns{msg->ns.buf, msg->ns.len};
LOG::trace_in(
Component::flow, "matching::FlowSpan::agent_info: side={} id={} az={} env={} ns={}", msg->side, id, az, env, ns);
auto const side = u8_to_side(msg->side);
agent_info_[+side] = {
.id = std::move(id),
.az = std::move(az),
.env = std::move(env),
.role = std::move(role),
.ns = std::move(ns),
};
n_received_info_messages_++;
}
void FlowSpan::task_info(::ebpf_net::matching::weak_refs::flow span_ref, u64 timestamp, jsrv_matching__task_info *msg)
{
std::string comm{msg->comm.buf, msg->comm.len};
std::string cgroup_name{msg->cgroup_name.buf, msg->cgroup_name.len};
LOG::trace_in(Component::flow, "matching::FlowSpan::task_info: side={} comm='{}' cgroup='{}'", msg->side, comm, cgroup_name);
auto const side = u8_to_side(msg->side);
task_info_[+side] = {
.comm = comm,
.cgroup_name = cgroup_name,
};
n_received_info_messages_++;
}
void FlowSpan::socket_info(::ebpf_net::matching::weak_refs::flow span_ref, u64 timestamp, jsrv_matching__socket_info *msg)
{
auto local_addr = IPv6Address::from(msg->local_addr);
auto remote_addr = IPv6Address::from(msg->remote_addr);
std::string remote_dns_name{msg->remote_dns_name.buf, msg->remote_dns_name.len};
LOG::trace_in(
Component::flow,
"matching::FlowSpan::socket_info: side={}"
" local_ip={}:{} remote_ip={}:{}",
msg->side,
local_addr,
msg->local_port,
remote_addr,
msg->remote_port);
auto const side = u8_to_side(msg->side);
socket_info_[+side] = SocketInfo{
.local_addr = local_addr,
.local_port = msg->local_port,
.remote_addr = remote_addr,
.remote_port = msg->remote_port,
.is_connector = msg->is_connector,
.remote_dns_name = std::move(remote_dns_name),
};
n_received_info_messages_++;
}
void FlowSpan::k8s_info(::ebpf_net::matching::weak_refs::flow span_ref, u64 timestamp, jsrv_matching__k8s_info *msg)
{
auto const side = u8_to_side(msg->side);
std::array<u8, 64> pod_uid_suffix;
std::copy_n(msg->pod_uid_suffix, std::min(sizeof(msg->pod_uid_suffix), pod_uid_suffix.max_size()), pod_uid_suffix.begin());
LOG::trace_in(
Component::flow,
"matching::FlowSpan::k8s_info: side={} pod_uid_suffix='{}'"
" pod_uid_hash={}",
msg->side,
std::string_view((char *)pod_uid_suffix.data(), pod_uid_suffix.size()),
msg->pod_uid_hash);
k8s_info_[+side] = K8sInfo{
.pod_uid_suffix = std::move(pod_uid_suffix),
.pod_uid_hash = msg->pod_uid_hash,
};
n_received_info_messages_++;
}
void FlowSpan::container_info(
::ebpf_net::matching::weak_refs::flow span_ref, const u64 timestamp, jsrv_matching__container_info *const msg)
{
auto const side = u8_to_side(msg->side);
std::string name{msg->name.buf, msg->name.len};
std::string pod{msg->pod.buf, msg->pod.len};
std::string role{msg->role.buf, msg->role.len};
std::string version{msg->version.buf, msg->version.len};
std::string ns{msg->ns.buf, msg->ns.len};
auto const type = static_cast<NodeResolutionType>(msg->node_type);
container_info_[+side] = ContainerInfo{
.name = std::move(name),
.pod = std::move(pod),
.role = std::move(role),
.version = std::move(version),
.ns = std::move(ns),
.type = (sanitize_enum(type) == type ? type : NodeResolutionType::CONTAINER)};
n_received_info_messages_++;
}
void FlowSpan::service_info(
::ebpf_net::matching::weak_refs::flow span_ref, const u64 timestamp, jsrv_matching__service_info *const msg)
{
auto const side = u8_to_side(msg->side);
std::string name{msg->name.buf, msg->name.len};
service_info_[+side] = ServiceInfo{
.name = std::move(name),
};
n_received_info_messages_++;
}
////////////////////////////////////////////////////////////////////////////////
void FlowSpan::update_nodes_if_required(::ebpf_net::matching::weak_refs::flow flow)
{
bool got_messages = (n_received_info_messages_ != message_count_on_last_update_);
bool update_needed = got_messages || should_attempt_k8s_enrichment(flow, FlowSide::SIDE_A) ||
should_attempt_k8s_enrichment(flow, FlowSide::SIDE_B);
if (!update_needed) {
return;
}
update_nodes(flow, resolve_node(flow, FlowSide::SIDE_A), resolve_node(flow, FlowSide::SIDE_B));
// set the last update value to avoid refreshing if there are no messages
message_count_on_last_update_ = n_received_info_messages_;
}
void FlowSpan::update_nodes(::ebpf_net::matching::weak_refs::flow flow, NodeData const &node_a, NodeData const &node_b)
{
create_agg_root(flow, node_a, node_b);
if (!flow.agg_root().valid()) {
return;
}
update_node(flow.agg_root(), FlowSide::SIDE_A, node_a);
update_node(flow.agg_root(), FlowSide::SIDE_B, node_b);
}
void FlowSpan::update_node(::ebpf_net::matching::weak_refs::agg_root agg_root, FlowSide side, NodeData const &n)
{
agg_root.update_node(
static_cast<u8>(side),
jb_blob(n.id),
jb_blob(n.az),
jb_blob(n.role),
jb_blob(n.version),
jb_blob(n.env),
jb_blob(n.ns),
static_cast<u8>(n.node_type),
jb_blob(n.address),
jb_blob(n.comm),
jb_blob(n.container_name),
jb_blob(n.pod_name),
jb_blob(n.role_uid));
}
void FlowSpan::create_agg_root(::ebpf_net::matching::weak_refs::flow flow, NodeData const &node_a, NodeData const &node_b)
{
using agg_root = ::ebpf_net::matching::spans::agg_root;
if (node_a.role.empty() || node_b.role.empty()) {
// can't do anything yet
return;
}
std::string_view role_a = node_a.role;
std::string_view role_b = node_b.role;
std::string_view az_a;
std::string_view az_b;
// HACK ALERT!
// We will only shard by (role,az,role,az) if one of the nodes is an IP node,
// which means that its role is (unknown) or (internet). Not a good thing to
// shard on. If neither of nodes is IP, we shard only on (role,role).
//
if ((node_a.node_type == NodeResolutionType::IP) || (node_b.node_type == NodeResolutionType::IP)) {
az_a = node_a.az;
az_b = node_b.az;
}
agg_root::role1_t role1;
agg_root::role2_t role2;
agg_root::az1_t az1;
agg_root::az2_t az2;
if (std::tie(role_a, az_a) <= std::tie(role_b, az_b)) {
role1 = agg_root::role1_t::truncate(role_a);
role2 = agg_root::role2_t::truncate(role_b);
az1 = agg_root::az1_t::truncate(az_a);
az2 = agg_root::az2_t::truncate(az_b);
} else {
role1 = agg_root::role1_t::truncate(role_b);
role2 = agg_root::role2_t::truncate(role_a);
az1 = agg_root::az1_t::truncate(az_b);
az2 = agg_root::az2_t::truncate(az_a);
}
if ((flow.agg_root().valid() == false) || (role1 != flow.agg_root().role1()) || (role2 != flow.agg_root().role2()) ||
(az1 != flow.agg_root().az1()) || (az2 != flow.agg_root().az2())) {
flow.modify().agg_root(flow.index().agg_root.alloc(role1, az1, role2, az2));
}
}
bool FlowSpan::should_attempt_k8s_enrichment(::ebpf_net::matching::weak_refs::flow flow, FlowSide side) const
{
// should we retry to enrich using kubernetes pods?
//
// this can happen for a side if:
// 1. we haven't already resolved kubernetes
// 2. the side has k8s info
// get the k8s pod info
auto k8s_pod = (side == FlowSide::SIDE_A) ? flow.k8s_pod1() : flow.k8s_pod2();
if (k8s_pod.valid()) {
// already resolved and enriched as kubernetes
return false;
} else {
// continue attempting if we have k8s info
return k8s_info_[+side].has_value();
}
}
////////////////////////////////////////////////////////////////////////////////
FlowSpan::NodeData FlowSpan::resolve_node(::ebpf_net::matching::weak_refs::flow span_ref, FlowSide side)
{
const std::optional<AddrPort> addr_port = get_addr_port(side);
if (unlikely(!addr_port.has_value())) {
debug_state("address/port missing");
return NodeData{};
}
// Textual representation of the IP address.
std::string address = addr_port->addr.tidy_string();
auto [id, az, is_autonomous_system] = get_id_az(side);
std::string role;
std::string role_uid;
std::string version;
std::string env;
std::string ns;
std::string container_name;
auto node_type = NodeResolutionType::NONE;
if (auto &agent_info = agent_info_[+side]; agent_info.has_value()) {
env = agent_info->env;
ns = agent_info->ns;
} else {
env = kNoAgentEnvironmentName;
}
const std::string &pod_name = get_or_default(container_info_[+side]).pod;
// sanity checks on messages:
// we get agent_info_ if and only if task_info and socket info
if (unlikely(agent_info_[+side].has_value() != task_info_[+side].has_value())) {
debug_state("agent_info not iff task_info");
};
if (unlikely(agent_info_[+side].has_value() != socket_info_[+side].has_value())) {
debug_state("agent info not iff socket_info");
}
// Enrich, in order of data richness.
//
// First, try Kubernetes
if (auto pod = get_k8s_pod(side, span_ref.index()); pod.valid()) {
// got a k8s pod!
LOG::trace_in(
NodeResolutionType::K8S_CONTAINER,
"matching::FlowSpan::update_node: enriching k8s"
" pod_uid_suffix='{}' pod_uid_hash={} pod_owner='{}' ns='{}'",
std::string_view((char *)pod.uid_suffix().data(), pod.uid_suffix().size()),
pod.uid_hash(),
pod.owner_name(),
pod.ns());
// set our state that we're enriched
if (side == FlowSide::SIDE_A) {
span_ref.modify().k8s_pod1(pod.get());
} else {
span_ref.modify().k8s_pod2(pod.get());
}
// enrich
node_type = NodeResolutionType::K8S_CONTAINER;
role = pod.owner_name();
role_uid = pod.owner_uid();
version = pod.version();
ns = pod.ns();
if (auto &task_info = task_info_[+side]; task_info.has_value()) {
auto info = CGroupParser{task_info->cgroup_name}.get();
auto container_id = info.container_id;
if (!container_id.empty()) {
auto container_key = make_uid_key<ebpf_net::matching::keys::k8s_container>(container_id);
auto k8s_container = span_ref.index().k8s_container.by_key(container_key);
if (k8s_container.valid()) {
container_name = k8s_container.name();
if (!k8s_container.version().empty()) {
version = k8s_container.version();
}
}
}
}
} else if (auto const &container = container_info_[+side]; container.has_value()) {
// if we have container info, use that
node_type = sanitize_enum(container->type) == container->type ? container->type : NodeResolutionType::CONTAINER;
role = container->role;
version = container->version;
ns = container->ns;
} else if (agent_info_[+side].has_value()) {
// if we at least have an agent, use that
node_type = NodeResolutionType::PROCESS;
if (service_info_[+side].has_value()) {
role = service_info_[+side]->name;
} else if (task_info_[+side].has_value()) {
role = task_info_[+side]->comm;
} else {
// Shouldn't happen :( need to fix
role = agent_info_[+side]->role;
}
ns = agent_info_[+side]->ns;
} else if (auto const &flipside_socket_info = socket_info_[+(~side)]; flipside_socket_info.has_value()) {
// no agent: try AWS -> DNS -> IP
auto const &ipv6 = flipside_socket_info->remote_addr;
AwsEnrichmentInfo const *aws_info = nullptr;
if (aws_enrichment_enabled_) {
auto aws_enrichment_span = span_ref.index().aws_enrichment.by_key({.ip = ipv6.as_int()}, false);
if (aws_enrichment_span.valid()) {
aws_info = aws_enrichment_span.impl().info();
}
if (aws_info) {
LOG::trace_in(
NodeResolutionType::AWS,
"matching::FlowSpan::update_node: found AWS metadata"
" for: ipv6={} role={} az={} id={}",
ipv6,
aws_info->role,
aws_info->az,
aws_info->id);
} else {
LOG::trace_in(
NodeResolutionType::AWS,
"matching::FlowSpan::update_node: found no AWS metadata"
" for: ipv6={}",
ipv6);
}
}
if (aws_info && !aws_info->role.empty() && !aws_info->az.empty()) {
// AWS enrichment exists
node_type = NodeResolutionType::AWS;
role = aws_info->role;
az = aws_info->az;
if (!aws_info->id.empty()) {
id = aws_info->id + "/" + id;
}
} else {
if (!flipside_socket_info->remote_dns_name.empty()) {
node_type = NodeResolutionType::DNS;
role = flipside_socket_info->remote_dns_name;
} else {
// no agent and no DNS, fall back on the IP address
node_type = NodeResolutionType::IP;
role = is_autonomous_system ? "(internet)" : "(unknown)";
}
}
}
if (get_comm(side) == kCommKubelet) {
role = "kubelet";
} else if (addr_port->port == kPortDNS) {
role = "DNS";
} else if (addr_port->addr == kAddrInstanceMetadata) {
role = "instance metadata";
node_type = NodeResolutionType::INSTANCE_METADATA;
if (auto &agent_info = agent_info_[+(~side)]; agent_info.has_value()) {
id = agent_info->id;
az = agent_info->az;
}
}
if (is_autonomous_system && (node_type == NodeResolutionType::IP) && !MatchingCore::autonomous_system_ip_enabled()) {
id = address = "AS";
}
if (container_name.empty()) {
container_name = get_or_default(container_info_[+side]).name;
}
return NodeData{
.id = id,
.az = az,
.role = role,
.role_uid = role_uid,
.version = version,
.env = env,
.ns = ns,
.node_type = node_type,
.address = address,
.comm = get_comm(side),
.container_name = container_name,
.pod_name = pod_name,
};
}
std::string FlowSpan::get_comm(FlowSide side) const
{
return get_or_default(task_info_[+side]).comm;
}
std::optional<FlowSpan::AddrPort> FlowSpan::get_addr_port(FlowSide side) const
{
if (auto &local_info = socket_info_[+side]; local_info.has_value()) {
return AddrPort{local_info->local_addr, local_info->local_port};
}
FlowSide other_side = ~side;
if (auto &remote_info = socket_info_[+other_side]; remote_info.has_value()) {
return AddrPort{remote_info->remote_addr, remote_info->remote_port};
}
return std::nullopt;
}
std::tuple<std::string, std::string, bool> FlowSpan::get_id_az(FlowSide side) const
{
if (auto &agent_info = agent_info_[+side]; agent_info.has_value()) {
// use ID and AZ obtained from this side's agent info
return std::make_tuple(agent_info->id, agent_info->az, false);
}
std::string id;
std::string az = kUnknown;
bool is_autonomous_system = false;
FlowSide other_side = ~side;
if (auto &socket_info = socket_info_[+other_side]; socket_info.has_value()) {
// use the remote IP address from other side's socket info for ID
id = socket_info->remote_addr.tidy_string();
if (auto &an_db = local_core<MatchingCore>().an_db; an_db) {
auto addr = reinterpret_cast<in6_addr const *>(&socket_info->remote_addr);
if (auto entry = an_db.lookup(addr)) {
is_autonomous_system = geoip::well_known_data::try_autonomous_system_organization(az, entry);
}
}
}
return std::make_tuple(id, az, is_autonomous_system);
}
::ebpf_net::matching::auto_handles::k8s_pod FlowSpan::get_k8s_pod(FlowSide side, ::ebpf_net::matching::Index &index)
{
if (auto &k8s_info = k8s_info_[+side]; k8s_info.has_value()) {
::ebpf_net::matching::keys::k8s_pod pod_key{k8s_info->pod_uid_suffix, k8s_info->pod_uid_hash};
LOG::trace_in(
NodeResolutionType::K8S_CONTAINER,
"matching::FlowSpan::get_k8s_pod: trying to find pod"
" uid_suffix='{}' uid_hash={}",
std::string_view((char *)pod_key.uid_suffix.data(), pod_key.uid_suffix.size()),
pod_key.uid_hash);
auto pod = index.k8s_pod.by_key(pod_key);
if (pod.valid() && !pod.owner_name().empty()) {
return pod;
}
}
if (auto &task_info = task_info_[+side]; task_info.has_value()) {
auto info = CGroupParser{task_info->cgroup_name}.get();
auto container_id = info.container_id;
if (!container_id.empty()) {
auto container_key = make_uid_key<ebpf_net::matching::keys::k8s_container>(container_id);
LOG::trace_in(
NodeResolutionType::K8S_CONTAINER,
"matching::FlowSpan::update_node: trying to find container"
" id='{}' uid_suffix='{}' uid_hash={}",
container_id,
std::string_view((char *)container_key.uid_suffix.data(), container_key.uid_suffix.size()),
container_key.uid_hash);
auto k8s_container = index.k8s_container.by_key(container_key);
if (k8s_container.valid() && k8s_container.pod().valid()) {
return k8s_container.pod().get();
}
}
}
return ::ebpf_net::matching::auto_handles::k8s_pod(index);
}
////////////////////////////////////////////////////////////////////////////////
UpdateDirection FlowSpan::metrics_update_direction(FlowSide side, int is_rx, bool force_both_sides)
{
if (!force_both_sides) {
if (!metrics_update_side_.has_value()) {
// first side to try gets to send metric updates
metrics_update_side_ = side;
} else if (*metrics_update_side_ != side) {
// the other side is sending metric updates
return UpdateDirection::NONE;
}
}
if (side == FlowSide::SIDE_A) {
return (is_rx == 0) ? UpdateDirection::A_TO_B : UpdateDirection::B_TO_A;
} else { // side == FlowSide::SIDE_B
return (is_rx == 0) ? UpdateDirection::B_TO_A : UpdateDirection::A_TO_B;
}
}
void FlowSpan::tcp_update(::ebpf_net::matching::weak_refs::flow span_ref, u64 timestamp, jsrv_matching__tcp_update *msg)
{
auto const side = u8_to_side(msg->side);
auto const direction = metrics_update_direction(side, msg->is_rx);
::ebpf_net::metrics::tcp_metrics_point metrics_point;
copy_tcp_metrics(metrics_point, *msg);
// skip RX RTT measurements
bool skip_rtts = msg->is_rx;
if (skip_rtts) {
metrics_point.sum_srtt = 0;
metrics_point.active_rtts = 0;
}
if (direction == UpdateDirection::A_TO_B) {
span_ref.tcp_a_to_b_update(timestamp, metrics_point);
} else if (direction == UpdateDirection::B_TO_A) {
span_ref.tcp_b_to_a_update(timestamp, metrics_point);
}
if (!skip_rtts) {
// Apply RTT-only metrics to side(s) that full metrics have not
// been applied above.
::ebpf_net::metrics::tcp_metrics_point rtt_metrics = {
.sum_srtt = metrics_point.sum_srtt,
.active_rtts = metrics_point.active_rtts,
};
if (direction == UpdateDirection::A_TO_B) {
span_ref.tcp_b_to_a_update(timestamp, rtt_metrics);
} else if (direction == UpdateDirection::B_TO_A) {
span_ref.tcp_a_to_b_update(timestamp, rtt_metrics);
} else {
span_ref.tcp_a_to_b_update(timestamp, rtt_metrics);
span_ref.tcp_b_to_a_update(timestamp, rtt_metrics);
}
}
}
void FlowSpan::udp_update(::ebpf_net::matching::weak_refs::flow span_ref, u64 timestamp, jsrv_matching__udp_update *msg)
{
auto const side = u8_to_side(msg->side);
auto const direction = metrics_update_direction(side, msg->is_rx);
::ebpf_net::metrics::udp_metrics_point metrics_point;
copy_udp_metrics(metrics_point, *msg);
if (direction == UpdateDirection::A_TO_B) {
span_ref.udp_a_to_b_update(timestamp, metrics_point);
} else if (direction == UpdateDirection::B_TO_A) {
span_ref.udp_b_to_a_update(timestamp, metrics_point);
}
}
void FlowSpan::http_update(::ebpf_net::matching::weak_refs::flow span_ref, u64 timestamp, jsrv_matching__http_update *msg)
{
auto const side = u8_to_side(msg->side);
auto const is_rx = msg->client_server == (u8)SC_SERVER; // client(0) is 'tx', server(1) is 'rx'
auto direction = metrics_update_direction(side, is_rx);
::ebpf_net::metrics::http_metrics_point metrics_point;
copy_http_metrics(metrics_point, *msg);
// Ensure metrics are always reported on the correct side
if (!is_rx) {
// Client never gets processing time
metrics_point.sum_processing_time_ns = 0;
} else {
// Server never gets total time
metrics_point.sum_total_time_ns = 0;
}
// If we would duplicate metrics, still send a 'single-sided' update
// but leave out metrics that would contribute twice
if (direction == UpdateDirection::NONE) {
// Leave only fields that are not duplicates (this excludes active_sockets)
metrics_point = {
.sum_total_time_ns = metrics_point.sum_total_time_ns,
.sum_processing_time_ns = metrics_point.sum_processing_time_ns,
};
// And allow the update regardless of if there's another agent on the other side
direction = metrics_update_direction(side, is_rx, true);
}
if (direction == UpdateDirection::A_TO_B) {
span_ref.http_a_to_b_update(timestamp, metrics_point);
} else if (direction == UpdateDirection::B_TO_A) {
span_ref.http_b_to_a_update(timestamp, metrics_point);
}
}
void FlowSpan::dns_update(::ebpf_net::matching::weak_refs::flow span_ref, u64 timestamp, jsrv_matching__dns_update *msg)
{
ASSUME(msg->side <= 1).else_log("side must be either 0 or 1, instead got {}", msg->side);
auto const side = u8_to_side(msg->side);
auto const is_rx = msg->client_server == (u8)SC_SERVER; // client(0) is 'tx', server(1) is 'rx'
auto direction = metrics_update_direction(side, is_rx);
::ebpf_net::metrics::dns_metrics_point metrics_point;
copy_dns_metrics(metrics_point, *msg);
// Ensure metrics are always reported on the correct side
if (!is_rx) {
// Client never gets processing time
metrics_point.sum_processing_time_ns = 0;
} else {
// Server never gets total time
metrics_point.sum_total_time_ns = 0;
}
// If we would duplicate metrics, still send a 'single-sided' update
// but leave out metrics that would contribute twice
if (direction == UpdateDirection::NONE) {
// Leave only fields that are not duplicates (this excludes active_sockets)
metrics_point = {
.sum_total_time_ns = metrics_point.sum_total_time_ns,
.sum_processing_time_ns = metrics_point.sum_processing_time_ns,
};
// And allow the update regardless of if there's another agent on the other side
direction = metrics_update_direction(side, is_rx, true);
}
if (direction == UpdateDirection::A_TO_B) {
span_ref.dns_a_to_b_update(timestamp, metrics_point);
} else if (direction == UpdateDirection::B_TO_A) {
span_ref.dns_b_to_a_update(timestamp, metrics_point);
}
}
////////////////////////////////////////////////////////////////////////////////
namespace {
template <UpdateDirection Dir>
void send_tcp_metrics(
u64 t, ::ebpf_net::matching::weak_refs::flow span_ref, ::ebpf_net::metrics::tcp_metrics const &m, u64 interval)
{
span_ref.impl().update_nodes_if_required(span_ref);
auto agg_root = span_ref.agg_root();
if (!agg_root.valid()) {
return;
}
agg_root.update_tcp_metrics(
(u8)Dir,
m.active_sockets,
m.sum_retrans,
m.sum_bytes,
m.sum_srtt,
m.sum_delivered,
m.active_rtts,
m.syn_timeouts,
m.new_sockets,
m.tcp_resets);
}
template <UpdateDirection Dir>
void send_udp_metrics(
u64 t, ::ebpf_net::matching::weak_refs::flow span_ref, ::ebpf_net::metrics::udp_metrics const &m, u64 interval)
{
span_ref.impl().update_nodes_if_required(span_ref);
auto agg_root = span_ref.agg_root();
if (!agg_root.valid()) {
return;
}
agg_root.update_udp_metrics((u8)Dir, m.active_sockets, m.addr_changes, m.packets, m.bytes, m.drops);
}
template <UpdateDirection Dir>
void send_dns_metrics(
u64 t, ::ebpf_net::matching::weak_refs::flow span_ref, ::ebpf_net::metrics::dns_metrics const &m, u64 interval)
{
span_ref.impl().update_nodes_if_required(span_ref);
auto agg_root = span_ref.agg_root();
if (!agg_root.valid()) {
return;
}
agg_root.update_dns_metrics(
(u8)Dir,
m.active_sockets,
m.requests_a,
m.requests_aaaa,
m.responses,
m.timeouts,
m.sum_total_time_ns,
m.sum_processing_time_ns);
}
template <UpdateDirection Dir>
void send_http_metrics(
u64 t, ::ebpf_net::matching::weak_refs::flow span_ref, ::ebpf_net::metrics::http_metrics const &m, u64 interval)
{
span_ref.impl().update_nodes_if_required(span_ref);
auto agg_root = span_ref.agg_root();
if (!agg_root.valid()) {
return;
}
agg_root.update_http_metrics(
(u8)Dir,
m.active_sockets,
m.sum_code_200,
m.sum_code_400,
m.sum_code_500,
m.sum_code_other,
m.sum_total_time_ns,
m.sum_processing_time_ns);
}
} // namespace
void FlowSpan::send_metrics_to_aggregation(::ebpf_net::matching::containers::flow &flows, u64 ts)
{
flows.tcp_a_to_b_foreach(ts, send_tcp_metrics<UpdateDirection::A_TO_B>);
flows.tcp_b_to_a_foreach(ts, send_tcp_metrics<UpdateDirection::B_TO_A>);
flows.udp_a_to_b_foreach(ts, send_udp_metrics<UpdateDirection::A_TO_B>);
flows.udp_b_to_a_foreach(ts, send_udp_metrics<UpdateDirection::B_TO_A>);
flows.dns_a_to_b_foreach(ts, send_dns_metrics<UpdateDirection::A_TO_B>);
flows.dns_b_to_a_foreach(ts, send_dns_metrics<UpdateDirection::B_TO_A>);
flows.http_a_to_b_foreach(ts, send_http_metrics<UpdateDirection::A_TO_B>);
flows.http_b_to_a_foreach(ts, send_http_metrics<UpdateDirection::B_TO_A>);
}
////////////////////////////////////////////////////////////////////////////////
void FlowSpan::debug_state(const std::string_view &reason)
{
LOG::debug_in(
NodeResolutionType::NONE,
"FlowSpan - n_messages={} last_update={} - '{}'",
n_received_info_messages_,
message_count_on_last_update_,
reason);
for (int i = 0; i < 2; i++) {
LOG::debug_in(NodeResolutionType::NONE, "side[{}]:", i);
if (agent_info_[i].has_value()) {
LOG::debug_in(
NodeResolutionType::NONE,
" agent: id='{}' az='{}' env='{}'",
agent_info_[i]->id,
agent_info_[i]->az,
agent_info_[i]->env);
} else {
LOG::debug_in(NodeResolutionType::NONE, " agent: null");
}
if (task_info_[i].has_value()) {
LOG::debug_in(NodeResolutionType::NONE, " task: comm='{}'", task_info_[i]->comm);
} else {
LOG::debug_in(NodeResolutionType::NONE, " task: null");
}
if (socket_info_[i].has_value()) {
LOG::debug_in(
NodeResolutionType::NONE,
" socket: local_addr='{}' local_port={} remote_addr='{}' "
"remote_port={}",
socket_info_[i]->local_addr,
socket_info_[i]->local_port,
socket_info_[i]->remote_addr,
socket_info_[i]->remote_port);
} else {
LOG::debug_in(NodeResolutionType::NONE, " socket: null");
}
if (container_info_[i].has_value()) {
LOG::debug_in(
NodeResolutionType::NONE, " container: name='{}' pod='{}'", container_info_[i]->name, container_info_[i]->pod);
} else {
LOG::debug_in(NodeResolutionType::NONE, " container: null");
}
}
}
} // namespace reducer::matching