History

bpf: add documentation for eBPF helpers (33-41)

Add documentation for eBPF helper functions to bpf.h user header file. This documentation can be parsed with the Python script provided in another commit of the patch series, in order to provide a RST document that can later be converted into a man page. The objective is to make the documentation easily understandable and accessible to all eBPF developers, including beginners. This patch contains descriptions for the following helper functions, all written by Daniel: - bpf_get_hash_recalc() - bpf_skb_change_tail() - bpf_skb_pull_data() - bpf_csum_update() - bpf_set_hash_invalid() - bpf_get_numa_node_id() - bpf_set_hash() - bpf_skb_adjust_room() - bpf_xdp_adjust_meta() v4: - bpf_skb_change_tail(): Clarify comment about invalidated verifier checks. - bpf_skb_pull_data(): Clarify the motivation for using this helper or bpf_skb_load_bytes(), on non-linear buffers. Fix RST formatting for *skb*. Clarify comment about invalidated verifier checks. - bpf_csum_update(): Fix description of checksum (entire packet, not IP checksum). Fix a typo: "header" instead of "helper". - bpf_set_hash_invalid(): Mention bpf_get_hash_recalc(). - bpf_get_numa_node_id(): State that the helper is not restricted to programs attached to sockets. - bpf_skb_adjust_room(): Clarify comment about invalidated verifier checks. - bpf_xdp_adjust_meta(): Clarify comment about invalidated verifier checks.

Quentin Monnet committed 8 years ago

Commit fa156011 parent 1fdd08b1 file · +164 −0Browse files

Showing the changes to include/uapi/linux (1 of 1 file). Show all

Unified · Split

include/uapi/linux/bpf.h+164−0
@@ -1071,9 +1071,173 @@ union bpf_attr { · 1070 unmodified lines
10711071 * Return
10721072 * 0 on success, or a negative error in case of failure.
10731073 *
1074+ * u32 bpf_get_hash_recalc(struct sk_buff *skb)
1075+ * Description
1076+ * Retrieve the hash of the packet, *skb*\ **->hash**. If it is
1077+ * not set, in particular if the hash was cleared due to mangling,
1078+ * recompute this hash. Later accesses to the hash can be done
1079+ * directly with *skb*\ **->hash**.
1080+ *
1081+ * Calling **bpf_set_hash_invalid**\ (), changing a packet
1082+ * prototype with **bpf_skb_change_proto**\ (), or calling
1083+ * **bpf_skb_store_bytes**\ () with the
1084+ * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear
1085+ * the hash and to trigger a new computation for the next call to
1086+ * **bpf_get_hash_recalc**\ ().
1087+ * Return
1088+ * The 32-bit hash.
1089+ *
10741090 * u64 bpf_get_current_task(void)
10751091 * Return
10761092 * A pointer to the current task struct.
1093+ *
1094+ * int bpf_skb_change_tail(struct sk_buff *skb, u32 len, u64 flags)
1095+ * Description
1096+ * Resize (trim or grow) the packet associated to *skb* to the
1097+ * new *len*. The *flags* are reserved for future usage, and must
1098+ * be left at zero.
1099+ *
1100+ * The basic idea is that the helper performs the needed work to
1101+ * change the size of the packet, then the eBPF program rewrites
1102+ * the rest via helpers like **bpf_skb_store_bytes**\ (),
1103+ * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ ()
1104+ * and others. This helper is a slow path utility intended for
1105+ * replies with control messages. And because it is targeted for
1106+ * slow path, the helper itself can afford to be slow: it
1107+ * implicitly linearizes, unclones and drops offloads from the
1108+ * *skb*.
1109+ *
1110+ * A call to this helper is susceptible to change the underlaying
1111+ * packet buffer. Therefore, at load time, all checks on pointers
1112+ * previously done by the verifier are invalidated and must be
1113+ * performed again, if the helper is used in combination with
1114+ * direct packet access.
1115+ * Return
1116+ * 0 on success, or a negative error in case of failure.
1117+ *
1118+ * int bpf_skb_pull_data(struct sk_buff *skb, u32 len)
1119+ * Description
1120+ * Pull in non-linear data in case the *skb* is non-linear and not
1121+ * all of *len* are part of the linear section. Make *len* bytes
1122+ * from *skb* readable and writable. If a zero value is passed for
1123+ * *len*, then the whole length of the *skb* is pulled.
1124+ *
1125+ * This helper is only needed for reading and writing with direct
1126+ * packet access.
1127+ *
1128+ * For direct packet access, testing that offsets to access
1129+ * are within packet boundaries (test on *skb*\ **->data_end**) is
1130+ * susceptible to fail if offsets are invalid, or if the requested
1131+ * data is in non-linear parts of the *skb*. On failure the
1132+ * program can just bail out, or in the case of a non-linear
1133+ * buffer, use a helper to make the data available. The
1134+ * **bpf_skb_load_bytes**\ () helper is a first solution to access
1135+ * the data. Another one consists in using **bpf_skb_pull_data**
1136+ * to pull in once the non-linear parts, then retesting and
1137+ * eventually access the data.
1138+ *
1139+ * At the same time, this also makes sure the *skb* is uncloned,
1140+ * which is a necessary condition for direct write. As this needs
1141+ * to be an invariant for the write part only, the verifier
1142+ * detects writes and adds a prologue that is calling
1143+ * **bpf_skb_pull_data()** to effectively unclone the *skb* from
1144+ * the very beginning in case it is indeed cloned.
1145+ *
1146+ * A call to this helper is susceptible to change the underlaying
1147+ * packet buffer. Therefore, at load time, all checks on pointers
1148+ * previously done by the verifier are invalidated and must be
1149+ * performed again, if the helper is used in combination with
1150+ * direct packet access.
1151+ * Return
1152+ * 0 on success, or a negative error in case of failure.
1153+ *
1154+ * s64 bpf_csum_update(struct sk_buff *skb, __wsum csum)
1155+ * Description
1156+ * Add the checksum *csum* into *skb*\ **->csum** in case the
1157+ * driver has supplied a checksum for the entire packet into that
1158+ * field. Return an error otherwise. This helper is intended to be
1159+ * used in combination with **bpf_csum_diff**\ (), in particular
1160+ * when the checksum needs to be updated after data has been
1161+ * written into the packet through direct packet access.
1162+ * Return
1163+ * The checksum on success, or a negative error code in case of
1164+ * failure.
1165+ *
1166+ * void bpf_set_hash_invalid(struct sk_buff *skb)
1167+ * Description
1168+ * Invalidate the current *skb*\ **->hash**. It can be used after
1169+ * mangling on headers through direct packet access, in order to
1170+ * indicate that the hash is outdated and to trigger a
1171+ * recalculation the next time the kernel tries to access this
1172+ * hash or when the **bpf_get_hash_recalc**\ () helper is called.
1173+ *
1174+ * int bpf_get_numa_node_id(void)
1175+ * Description
1176+ * Return the id of the current NUMA node. The primary use case
1177+ * for this helper is the selection of sockets for the local NUMA
1178+ * node, when the program is attached to sockets using the
1179+ * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**),
1180+ * but the helper is also available to other eBPF program types,
1181+ * similarly to **bpf_get_smp_processor_id**\ ().
1182+ * Return
1183+ * The id of current NUMA node.
1184+ *
1185+ * u32 bpf_set_hash(struct sk_buff *skb, u32 hash)
1186+ * Description
1187+ * Set the full hash for *skb* (set the field *skb*\ **->hash**)
1188+ * to value *hash*.
1189+ * Return
1190+ * 0
1191+ *
1192+ * int bpf_skb_adjust_room(struct sk_buff *skb, u32 len_diff, u32 mode, u64 flags)
1193+ * Description
1194+ * Grow or shrink the room for data in the packet associated to
1195+ * *skb* by *len_diff*, and according to the selected *mode*.
1196+ *
1197+ * There is a single supported mode at this time:
1198+ *
1199+ * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer
1200+ * (room space is added or removed below the layer 3 header).
1201+ *
1202+ * All values for *flags* are reserved for future usage, and must
1203+ * be left at zero.
1204+ *
1205+ * A call to this helper is susceptible to change the underlaying
1206+ * packet buffer. Therefore, at load time, all checks on pointers
1207+ * previously done by the verifier are invalidated and must be
1208+ * performed again, if the helper is used in combination with
1209+ * direct packet access.
1210+ * Return
1211+ * 0 on success, or a negative error in case of failure.
1212+ *
1213+ * int bpf_xdp_adjust_meta(struct xdp_buff *xdp_md, int delta)
1214+ * Description
1215+ * Adjust the address pointed by *xdp_md*\ **->data_meta** by
1216+ * *delta* (which can be positive or negative). Note that this
1217+ * operation modifies the address stored in *xdp_md*\ **->data**,
1218+ * so the latter must be loaded only after the helper has been
1219+ * called.
1220+ *
1221+ * The use of *xdp_md*\ **->data_meta** is optional and programs
1222+ * are not required to use it. The rationale is that when the
1223+ * packet is processed with XDP (e.g. as DoS filter), it is
1224+ * possible to push further meta data along with it before passing
1225+ * to the stack, and to give the guarantee that an ingress eBPF
1226+ * program attached as a TC classifier on the same device can pick
1227+ * this up for further post-processing. Since TC works with socket
1228+ * buffers, it remains possible to set from XDP the **mark** or
1229+ * **priority** pointers, or other pointers for the socket buffer.
1230+ * Having this scratch space generic and programmable allows for
1231+ * more flexibility as the user is free to store whatever meta
1232+ * data they need.
1233+ *
1234+ * A call to this helper is susceptible to change the underlaying
1235+ * packet buffer. Therefore, at load time, all checks on pointers
1236+ * previously done by the verifier are invalidated and must be
1237+ * performed again, if the helper is used in combination with
1238+ * direct packet access.
1239+ * Return
1240+ * 0 on success, or a negative error in case of failure.
10771241 */
10781242 #define __BPF_FUNC_MAPPER(FN) \
10791243 FN(unspec), \