|
| 1 | +/* |
| 2 | +Copyright 2024 The bpfman Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | +) |
| 22 | + |
| 23 | +// BpfApplicationProgram defines the desired state of BpfApplication |
| 24 | +// +union |
| 25 | +// +kubebuilder:validation:XValidation:rule="has(self.type) && self.type == 'XDP' ? has(self.xdp) : !has(self.xdp)",message="xdp configuration is required when type is xdp, and forbidden otherwise" |
| 26 | +// +kubebuilder:validation:XValidation:rule="has(self.type) && self.type == 'TC' ? has(self.tc) : !has(self.tc)",message="tc configuration is required when type is tc, and forbidden otherwise" |
| 27 | +// +kubebuilder:validation:XValidation:rule="has(self.type) && self.type == 'TCX' ? has(self.tcx) : !has(self.tcx)",message="tcx configuration is required when type is TCX, and forbidden otherwise" |
| 28 | +// +kubebuilder:validation:XValidation:rule="has(self.type) && self.type == 'UProbe' ? has(self.uprobe) : !has(self.uprobe)",message="uprobe configuration is required when type is uprobe, and forbidden otherwise" |
| 29 | +// +kubebuilder:validation:XValidation:rule="has(self.type) && self.type == 'URetProbe' ? has(self.uretprobe) : !has(self.uretprobe)",message="uretprobe configuration is required when type is uretprobe, and forbidden otherwise" |
| 30 | +type BpfApplicationProgram struct { |
| 31 | + // name is a required field and is the name of the function that is the entry |
| 32 | + // point for the eBPF program. name must not be an empty string, must not |
| 33 | + // exceed 64 characters in length, must start with alpha characters and must |
| 34 | + // only contain alphanumeric characters. |
| 35 | + // +required |
| 36 | + // +kubebuilder:validation:Pattern="^[a-zA-Z][a-zA-Z0-9_]+." |
| 37 | + // +kubebuilder:validation:MinLength=1 |
| 38 | + // +kubebuilder:validation:MaxLength=64 |
| 39 | + Name string `json:"name"` |
| 40 | + |
| 41 | + // type is a required field used to specify the type of the eBPF program. |
| 42 | + // |
| 43 | + // Allowed values are: |
| 44 | + // TC, TCX, UProbe, URetProbe, XDP |
| 45 | + // |
| 46 | + // When set to TC, the eBPF program can attach to network devices (interfaces). |
| 47 | + // The program can be attached on either packet ingress or egress, so the |
| 48 | + // program will be called on every incoming or outgoing packet seen by the |
| 49 | + // network device. When using the TC program type, the tc field is required. |
| 50 | + // See tc for more details on TC programs. |
| 51 | + // |
| 52 | + // When set to TCX, the eBPF program can attach to network devices |
| 53 | + // (interfaces). The program can be attached on either packet ingress or |
| 54 | + // egress, so the program will be called on every incoming or outgoing packet |
| 55 | + // seen by the network device. When using the TCX program type, the tcx field |
| 56 | + // is required. See tcx for more details on TCX programs. |
| 57 | + // |
| 58 | + // When set to UProbe, the program can attach in user-space. The UProbe is |
| 59 | + // attached to a binary, library or function name, and optionally an offset in |
| 60 | + // the code. When using the UProbe program type, the uprobe field is required. |
| 61 | + // See uprobe for more details on UProbe programs. |
| 62 | + // |
| 63 | + // When set to URetProbe, the program can attach in user-space. |
| 64 | + // The URetProbe is attached to the return of a binary, library or function |
| 65 | + // name, and optionally an offset in the code. When using the URetProbe |
| 66 | + // program type, the uretprobe field is required. See uretprobe for more |
| 67 | + // details on URetProbe programs. |
| 68 | + // |
| 69 | + // When set to XDP, the eBPF program can attach to network devices (interfaces) |
| 70 | + // and will be called on every incoming packet received by the network device. |
| 71 | + // When using the XDP program type, the xdp field is required. See xdp for more |
| 72 | + // details on XDP programs. |
| 73 | + // +unionDiscriminator |
| 74 | + // +required |
| 75 | + // +kubebuilder:validation:Enum:="XDP";"TC";"TCX";"UProbe";"URetProbe" |
| 76 | + Type EBPFProgType `json:"type"` |
| 77 | + |
| 78 | + // xdp is an optional field, but required when the type field is set to XDP. |
| 79 | + // xdp defines the desired state of the application's XDP programs. XDP program |
| 80 | + // can be attached to network devices (interfaces) and will be called on every |
| 81 | + // incoming packet received by the network device. The XDP attachment point is |
| 82 | + // just after the packet has been received off the wire, but before the Linux |
| 83 | + // kernel has allocated an sk_buff, which is used to pass the packet through |
| 84 | + // the kernel networking stack. |
| 85 | + // +unionMember |
| 86 | + // +optional |
| 87 | + XDP *XdpProgramInfo `json:"xdp,omitempty"` |
| 88 | + |
| 89 | + // tc is an optional field, but required when the type field is set to TC. tc |
| 90 | + // defines the desired state of the application's TC programs. TC programs are |
| 91 | + // attached to network devices (interfaces). The program can be attached on |
| 92 | + // either packet ingress or egress, so the program will be called on every |
| 93 | + // incoming or outgoing packet seen by the network device. The TC attachment |
| 94 | + // point is in Linux's Traffic Control (tc) subsystem, which is after the |
| 95 | + // Linux kernel has allocated an sk_buff. TCX is newer implementation of TC |
| 96 | + // with enhanced performance and better support for running multiple programs |
| 97 | + // on a given network device. This makes TC useful for packet classification |
| 98 | + // actions. |
| 99 | + // +unionMember |
| 100 | + // +optional |
| 101 | + TC *TcProgramInfo `json:"tc,omitempty"` |
| 102 | + |
| 103 | + // tcx is an optional field, but required when the type field is set to TCX. |
| 104 | + // tcx defines the desired state of the application's TCX programs. TCX |
| 105 | + // programs are attached to network devices (interfaces). The program can be |
| 106 | + // attached on either packet ingress or egress, so the program will be called |
| 107 | + // on every incoming or outgoing packet seen by the network device. The TCX |
| 108 | + // attachment point is in Linux's Traffic Control (tc) subsystem, which is |
| 109 | + // after the Linux kernel has allocated an sk_buff. This makes TCX useful for |
| 110 | + // packet classification actions. TCX is a newer implementation of TC with |
| 111 | + // enhanced performance and better support for running multiple programs on a |
| 112 | + // given network device. |
| 113 | + // +unionMember |
| 114 | + // +optional |
| 115 | + TCX *TcxProgramInfo `json:"tcx,omitempty"` |
| 116 | + |
| 117 | + // uprobe is an optional field, but required when the type field is set to |
| 118 | + // UProbe. uprobe defines the desired state of the application's UProbe |
| 119 | + // programs. UProbe programs are user-space probes. A target must be provided, |
| 120 | + // which is the library name or absolute path to a binary or library where the |
| 121 | + // probe is attached. Optionally, a function name can also be provided to |
| 122 | + // provide finer granularity on where the probe is attached. They can be |
| 123 | + // attached at any point in the binary, library or function using the optional |
| 124 | + // offset field. However, caution must be taken when using the offset, ensuring |
| 125 | + // the offset is still in the desired bytecode. |
| 126 | + // +unionMember |
| 127 | + // +optional |
| 128 | + UProbe *UprobeProgramInfo `json:"uprobe,omitempty"` |
| 129 | + |
| 130 | + // uretprobe is an optional field, but required when the type field is set to |
| 131 | + // URetProbe. uretprobe defines the desired state of the application's |
| 132 | + // URetProbe programs. URetProbe programs are user-space probes. A target must |
| 133 | + // be provided, which is the library name or absolute path to a binary or |
| 134 | + // library where the probe is attached. Optionally, a function name can also be |
| 135 | + // provided to provide finer granularity on where the probe is attached. They |
| 136 | + // are attached to the return point of the binary, library or function, but can |
| 137 | + // be set anywhere using the optional offset field. However, caution must be |
| 138 | + // taken when using the offset, ensuring the offset is still in the desired |
| 139 | + // bytecode. |
| 140 | + // +unionMember |
| 141 | + // +optional |
| 142 | + URetProbe *UprobeProgramInfo `json:"uretprobe,omitempty"` |
| 143 | +} |
| 144 | + |
| 145 | +// spec defines the desired state of the BpfApplication. The BpfApplication |
| 146 | +// describes the set of one or more namespace scoped eBPF programs that should |
| 147 | +// be loaded for a given application and attributes for how they should be |
| 148 | +// loaded. eBPF programs that are grouped together under the same |
| 149 | +// BpfApplication instance can share maps and global data between the eBPF |
| 150 | +// programs loaded on the same Kubernetes Node. |
| 151 | +type BpfApplicationSpec struct { |
| 152 | + BpfAppCommon `json:",inline"` |
| 153 | + |
| 154 | + // programs is a required field and is the list of eBPF programs in a BPF |
| 155 | + // Application CRD that should be loaded in kernel memory. At least one entry |
| 156 | + // is required. eBPF programs in this list will be loaded on the system based |
| 157 | + // the nodeSelector. Even if an eBPF program is loaded in kernel memory, it |
| 158 | + // cannot be triggered until an attachment point is provided. The different |
| 159 | + // program types have different ways of attaching. The attachment points can be |
| 160 | + // added at creation time or modified (added or removed) at a later time to |
| 161 | + // activate or deactivate the eBPF program as desired. |
| 162 | + // CAUTION: When programs are added or removed from the list, that requires all |
| 163 | + // programs in the list to be reloaded, which could be temporarily service |
| 164 | + // effecting. For this reason, modifying the list is currently not allowed. |
| 165 | + // +required |
| 166 | + // +kubebuilder:validation:MinItems:=1 |
| 167 | + Programs []BpfApplicationProgram `json:"programs,omitempty"` |
| 168 | +} |
| 169 | + |
| 170 | +// +genclient |
| 171 | +// +kubebuilder:object:root=true |
| 172 | +// +kubebuilder:subresource:status |
| 173 | +// +kubebuilder:resource:scope=Namespaced |
| 174 | + |
| 175 | +// BpfApplication is the schema for the namespace scoped BPF Applications API. |
| 176 | +// This API allows applications to use bpfman to load and attach one or more |
| 177 | +// eBPF programs on a Kubernetes cluster. |
| 178 | +// |
| 179 | +// The bpfApplication.status field reports the overall status of the |
| 180 | +// BpfApplication CRD. A given BpfApplication CRD can result in loading and |
| 181 | +// attaching multiple eBPF programs on multiple nodes, so this status is just a |
| 182 | +// summary. More granular per-node status details can be found in the |
| 183 | +// corresponding BpfApplicationState CRD that bpfman creates for each node. |
| 184 | +// +kubebuilder:printcolumn:name="NodeSelector",type=string,JSONPath=`.spec.nodeselector` |
| 185 | +// +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.conditions[0].reason` |
| 186 | +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" |
| 187 | +type BpfApplication struct { |
| 188 | + metav1.TypeMeta `json:",inline"` |
| 189 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 190 | + |
| 191 | + Spec BpfApplicationSpec `json:"spec,omitempty"` |
| 192 | + Status BpfAppStatus `json:"status,omitempty"` |
| 193 | +} |
| 194 | + |
| 195 | +// +kubebuilder:object:root=true |
| 196 | +// BpfApplicationList contains a list of BpfApplications |
| 197 | +type BpfApplicationList struct { |
| 198 | + metav1.TypeMeta `json:",inline"` |
| 199 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 200 | + Items []BpfApplication `json:"items"` |
| 201 | +} |
0 commit comments