Optimizing Routing Table eBPF for Network Performance
Introduction: Navigating the Complexities of Modern Network Performance with eBPF
In the relentlessly evolving landscape of modern computing, network performance stands as a critical determinant of application responsiveness, user experience, and overall system efficiency. The explosive growth of data, the ubiquitous adoption of cloud-native architectures, microservices, and containerization have collectively placed unprecedented demands on underlying network infrastructure. Traditional networking paradigms, often characterized by static configurations and cumbersome kernel-user space interactions, are increasingly struggling to keep pace with the dynamic, high-throughput, and low-latency requirements of contemporary applications. These limitations manifest as bottlenecks in critical areas, particularly within the network's foundational element: the routing table. Efficient traffic management is paramount, especially for services that act as crucial intermediaries, such as a gateway or an API gateway, which handle vast volumes of API requests and responses. The ability to quickly and intelligently forward packets is not merely an performance enhancement; it is a fundamental requirement for maintaining operational integrity and competitive advantage in an increasingly interconnected world.
Enter eBPF (extended Berkeley Packet Filter), a revolutionary technology that has fundamentally reshaped how we interact with and program the Linux kernel. Moving beyond its origins as a packet filtering mechanism, eeBPF has blossomed into a powerful, versatile engine capable of executing sandboxed programs within the kernel without requiring kernel module modifications or changes to the kernel source code. This paradigm shift empowers developers and network engineers to dynamically extend kernel capabilities, observe system events with unparalleled granularity, and, critically, reprogram the network data path at astonishing speeds. For network routing, eBPF presents a transformative opportunity: to bypass traditional performance constraints, implement highly customized forwarding logic, and significantly optimize the routing table lookup process. This article delves into the intricate details of leveraging eBPF to revolutionize routing table management, enhance network performance, and build more resilient, agile, and performant network infrastructures capable of meeting the rigorous demands of the digital era. We will explore its foundational principles, practical applications, and the profound impact it has on the efficiency of services ranging from simple data transfers to sophisticated API gateway operations.
Chapter 1: The Modern Network Performance Landscape and its Challenges
The digital transformation sweeping across industries has fundamentally altered the demands placed upon network infrastructure. What was once a relatively static environment primarily concerned with connectivity has evolved into a dynamic, hyper-converged ecosystem where the network is expected to be an agile, programmable fabric. This evolution is driven by several key factors, each contributing to unique performance challenges that traditional networking models often struggle to address effectively.
Firstly, the sheer exponential growth of data traffic is staggering. Every interaction, from a simple web page load to complex financial transactions and AI model inferences, generates data that must traverse the network. This constant deluge necessitates infrastructure capable of handling massive throughput without introducing undue latency. For businesses relying on timely data processing and real-time analytics, any degradation in network speed can have immediate and significant financial or operational consequences. The raw volume of packets alone can overwhelm conventional processing mechanisms, leading to dropped packets, retransmissions, and a sluggish user experience that directly impacts customer satisfaction and business outcomes.
Secondly, the pervasive adoption of microservices, containers, and cloud-native architectures has splintered monolithic applications into hundreds or even thousands of smaller, independently deployable services. While offering unparalleled agility and scalability for application development, this architectural shift dramatically increases inter-service communication. Instead of a few large connections, the network now handles an explosion of smaller, ephemeral connections between numerous containers, often residing on the same or different hosts within a distributed system. Each of these interactions, many of which are API calls, requires efficient routing, precise load balancing, and robust security policies. The traditional network stack, designed for coarser-grained traffic patterns, often introduces excessive overhead when dealing with this fine-grained, dynamic communication pattern, leading to increased CPU utilization for packet processing and reduced effective throughput.
Thirdly, the relentless demand for low latency and high throughput is no longer confined to niche high-frequency trading or scientific computing domains. Users expect instant responses from their applications, whether they are streaming high-definition video, collaborating in real-time documents, or interacting with AI-powered chatbots. In enterprise settings, mission-critical applications, database transactions, and internal API communication all require predictable and minimal latency to ensure business continuity and operational efficiency. Achieving this balance across vast and complex networks, especially those spanning multiple geographic regions or cloud providers, is an monumental engineering challenge. Every millisecond of delay added by inefficient routing or processing mechanisms contributes to a poorer experience and potentially missed business opportunities.
These factors expose the inherent limitations of traditional network stacks. The fundamental design of the Linux kernel's networking stack, while incredibly robust, involves a significant overhead for each packet. This includes multiple context switches between user space and kernel space, numerous memory copies, and traversing complex data structures for routing and firewall rules. While acceptable for less demanding workloads, this overhead becomes a major bottleneck in high-performance environments. Furthermore, traditional network configurations often rely on static or semi-static rules defined through user-space tools. Modifying these rules typically involves expensive system calls and can introduce latency, making it difficult to adapt rapidly to changing network conditions or application requirements. The rigidity of these traditional methods stands in stark contrast to the dynamic nature of cloud-native applications, where services are spun up and down within seconds, requiring the network to instantly adapt its forwarding paths.
Within this intricate ecosystem, the role of gateway devices and API gateway solutions becomes exceptionally critical. A network gateway acts as an entry and exit point for network traffic, often performing crucial functions like NAT, firewalling, and basic routing between different network segments. An API gateway, specifically, serves as the single entry point for all API calls, abstracting backend services, providing security, rate limiting, monitoring, and request routing to the appropriate microservice. The performance of these gateway components directly dictates the overall performance and reliability of the applications they front. If an API gateway cannot efficiently route API requests to the correct backend service with minimal latency, the entire application stack suffers. Traditional routing table lookups and policy enforcement within these gateway devices, if not highly optimized, can become the primary choke point, degrading the user experience and undermining the very benefits of microservices architectures. Therefore, addressing these fundamental challenges through innovative approaches like eBPF is not just an optimization; it's a necessity for thriving in the modern digital landscape.
Chapter 2: Understanding eBPF: A Paradigm Shift in Kernel Programmability
The concept of running user-defined programs directly within the operating system kernel has historically been fraught with danger. A single misstep could lead to kernel panics, system instability, and security vulnerabilities. This inherent risk has traditionally limited kernel extensibility to highly controlled, compiled kernel modules, which require precise kernel version matching and often kernel recompilation, making them cumbersome to deploy and maintain. However, the advent of eBPF has ushered in a paradigm shift in kernel programmability, offering a safe, efficient, and dynamic mechanism to extend kernel functionality without compromising system stability.
What is eBPF? At its core, eBPF (extended Berkeley Packet Filter) is a revolutionary technology that allows arbitrary user-defined programs to be safely executed within the Linux kernel. It is a highly efficient virtual machine that lives inside the kernel, capable of running programs specifically designed to respond to various kernel events. While its predecessor, cBPF (classic BPF), was primarily limited to packet filtering, eBPF vastly expands this capability, providing a rich set of instructions, larger program sizes, and access to a wide array of kernel data structures. This evolution has transformed BPF from a niche networking tool into a general-purpose, powerful kernel observability and programmability framework.
The essence of eBPF lies in its ability to enable safe, sandboxed execution of user-defined programs in the kernel. When an eBPF program is loaded into the kernel, it first undergoes a rigorous verification process by the BPF verifier. This verifier statically analyzes the program's bytecode to ensure it terminates, does not contain infinite loops, does not access invalid memory locations, and adheres to strict security rules. This crucial step guarantees that an eBPF program, even if maliciously crafted or buggy, cannot crash the kernel or access unauthorized memory. Once verified, the eBPF program is then compiled into native machine code by a JIT (Just-In-Time) compiler. This JIT compilation ensures that eBPF programs run at near-native speed, minimizing any performance overhead compared to interpreted bytecode. This combination of safety and speed is a cornerstone of eBPF's success.
Key components of the eBPF ecosystem are fundamental to its operation. The BPF verifier acts as the gatekeeper, ensuring program safety. The JIT compiler optimizes program execution. Crucially, eBPF programs interact with kernel and user space data through BPF maps. These are generic key-value data structures that can be shared between eBPF programs and user-space applications. Maps enable eBPF programs to store state, share data, and communicate results back to user space. Examples include hash tables, arrays, longest prefix match (LPM) tries, and ring buffers, each optimized for different data access patterns. These maps are essential for implementing dynamic routing policies, storing flow statistics, or maintaining state across multiple packet processing events.
The advantages over traditional kernel modules are profound. Firstly, safety is paramount; the verifier eliminates the risk of kernel crashes inherent with traditional modules. Secondly, dynamic loading means eBPF programs can be loaded and unloaded at runtime without needing to recompile or reboot the kernel, providing unparalleled agility for network configuration changes or debugging. Thirdly, performance is exceptional, thanks to JIT compilation and the avoidance of expensive context switches. Unlike user-space applications that repeatedly cross the kernel boundary to interact with network devices or system calls, eBPF programs execute entirely within the kernel's context, processing data where it resides. This "kernel-space processing" drastically reduces overhead, making eBPF an ideal candidate for high-performance network tasks.
Beyond its foundational role in networking, eBPF's versatility extends to a myriad of applications. In security, it enables fine-grained access control, system call filtering, and advanced intrusion detection by monitoring system events at a very low level. For tracing and monitoring, eBPF can attach to almost any kernel function, providing deep insights into system behavior, process execution, and network flow dynamics without the performance penalties associated with traditional tracing tools. It's used to build sophisticated performance analysis tools, debug complex interactions, and gain visibility into black-box systems. Whether it's enhancing network routing, securing containerized workloads, or diagnosing elusive performance issues, eBPF represents a monumental leap forward in kernel extensibility and programmability, providing a powerful, safe, and efficient mechanism to tailor the Linux kernel to the precise demands of modern computing environments.
Chapter 3: The Fundamentals of Network Routing and its Bottlenecks
Network routing forms the backbone of all digital communication, dictating how data packets travel from a source to their intended destination across diverse and complex network topologies. Understanding the fundamental principles of IP routing and recognizing its inherent bottlenecks is crucial before delving into how eBPF can bring about transformative improvements. For any modern gateway or API gateway to function efficiently, the underlying routing mechanism must be robust and performant.
At its core, basic principles of IP routing revolve around the concept of forwarding tables, often referred to as routing tables. Every network device capable of routing (routers, layer 3 switches, and even host operating systems) maintains such a table. When a packet arrives, the router inspects its destination IP address and compares it against entries in its routing table. Each entry typically contains a network destination (e.g., an IP prefix like 192.168.1.0/24), the next-hop router's IP address, and the outgoing network interface. The crucial mechanism here is the longest prefix match (LPM). If a packet's destination IP matches multiple entries in the routing table, the entry with the most specific (longest) network prefix is chosen. This ensures that traffic destined for a specific subnet is routed precisely, even if a broader, less specific route also exists. For instance, a packet for 192.168.1.100 might match 192.168.0.0/16, but if a 192.168.1.0/24 route also exists, the latter, more specific route takes precedence.
The efficiency of routing lookups is paramount to network performance. In smaller networks, a simple linear scan of the routing table might suffice. However, in large-scale environments with tens of thousands or even hundreds of thousands of routes (e.g., a full Internet routing table), sequential lookups are prohibitively slow. To address this, network devices and operating systems employ sophisticated hierarchical data structures for storing and searching routing information. Common examples include radix trees (also known as Patricia tries) and hash tables. Radix trees are particularly well-suited for LPM lookups, as they organize prefixes hierarchically based on their bits, allowing for efficient traversal to find the longest matching prefix. Hash tables, while offering fast exact match lookups, are less naturally suited for LPM but can be used for specific route caches or direct next-hop lookups once a prefix is identified. The choice and implementation of these data structures significantly impact the speed at which routing decisions can be made.
However, even with optimized data structures, several challenges emerge in large-scale dynamic environments. The Internet routing table is constantly changing due to network events, link failures, or administrative policy updates, propagated through protocols like BGP (Border Gateway Protocol). In modern data centers, SDN (Software-Defined Networking) and NFV (Network Function Virtualization) introduce even greater dynamism, with virtual networks, service chains, and microservices constantly being provisioned and de-provisioned. Each change necessitates updates to routing tables, which can be computationally intensive and introduce transient inconsistencies or delays. The sheer number of routes, combined with their dynamic nature, strains the traditional kernel routing subsystem, leading to increased CPU utilization and potential packet forwarding delays. For a high-performance API gateway, even minor delays in route lookups can aggregate across millions of API calls, severely impacting the overall throughput and responsiveness of the application.
The impact of routing complexity on latency and throughput is direct and significant. Each lookup operation, no matter how optimized, consumes CPU cycles. In a system handling millions of packets per second, even a few extra CPU cycles per packet can translate into substantial performance degradation. Furthermore, changes to routing tables, especially in the context of routing protocols, involve communicating between user space routing daemons (like FRR or BIRD) and the kernel's forwarding information base (FIB). This user space to kernel space communication, along with the kernel's internal processing to update its data structures, introduces latency. During these updates, packets might be misrouted, delayed, or even dropped. This unpredictability and overhead are major bottlenecks for applications demanding predictable, ultra-low latency, and high throughput, which are common requirements for real-time API services and other critical networked applications.
Therefore, understanding the critical role of the Linux kernel's routing subsystem is paramount. It is the gatekeeper for all network traffic flowing through a Linux host, determining where every packet goes. While highly optimized over decades of development, its general-purpose nature and reliance on traditional interaction models (syscalls, fixed data structures) can still become a limiting factor under extreme loads or when highly specialized routing logic is required. Overcoming these bottlenecks necessitates a more agile, programmable, and performant approach to routing table management, which is precisely where eBPF shines, offering a pathway to bypass these traditional constraints and unlock new levels of network performance.
Chapter 4: eBPF's Role in Enhancing Network Routing
Having established the limitations of traditional routing mechanisms, we can now pivot to how eBPF serves as a powerful solution for overcoming these challenges. eBPF provides an unparalleled ability to instrument, observe, and program the Linux kernel's networking stack, offering a flexible and high-performance pathway to revolutionize network routing. This programmatic control at the kernel level empowers engineers to implement custom routing logic, accelerate forwarding decisions, and adapt network behavior dynamically. This flexibility is especially valuable for optimizing the performance of services such as an API gateway that require rapid and intelligent traffic steering.
The strength of eBPF lies in its capacity to interface seamlessly with the kernel's networking stack at various critical points. Unlike traditional user-space routing daemons that rely on slow system calls to interact with the kernel's forwarding tables, eBPF programs execute directly within the kernel context. This eliminates costly context switches and allows for direct manipulation or observation of network data structures and packet processing events. The integration points, or "hooks," are strategically placed throughout the packet's journey through the kernel, enabling eBPF programs to intercept, inspect, and modify packets with minimal overhead.
Specific eBPF hooks for routing are key to its efficacy. Two of the most prominent hooks for network processing that can significantly impact routing are XDP (eXpress Data Path) and TC (Traffic Control). * XDP allows eBPF programs to run at the absolute earliest point in the network driver, even before the kernel's network stack has allocated a sk_buff (socket buffer) structure for the packet. This "pre-network stack" execution environment is incredibly powerful for high-performance packet processing. An XDP program can make a forwarding decision, drop a packet, or redirect it to another interface or CPU core, all before the main networking stack has even processed it. This makes XDP ideal for very high-speed, line-rate routing decisions, especially for stateless forwarding or fastpath processing. * TC (Traffic Control) hooks provide more granular control later in the packet's journey, typically within the ingress and egress queues of network interfaces. eBPF programs attached to TC can inspect and classify packets, apply complex queueing disciplines, implement advanced quality of service (QoS) policies, and perform sophisticated packet steering. While not as early in the path as XDP, TC hooks offer a richer context, including access to a fully formed sk_buff, which contains more metadata. This makes TC hooks suitable for policy-based routing, load balancing across multiple next hops, or implementing dynamic routing adjustments based on application-layer information.
By leveraging these hooks, eBPF enables developers to bypass parts of the traditional kernel routing path. Instead of relying solely on the kernel's default FIB lookup, an eBPF program can implement its own highly optimized lookup mechanism using BPF maps. For instance, an XDP program could use a BPF hash map to store a subset of critical routes, performing a direct lookup for frequently accessed destinations and immediately forwarding the packet. This effectively creates a "fast path" that avoids the more general-purpose, and thus slower, traditional kernel routing logic. This is particularly beneficial for high-volume, repetitive traffic flows common in data centers or at the edge of a network serving an API gateway.
The ability to implement custom routing logic with eBPF is perhaps its most compelling feature. Network engineers are no longer constrained by the fixed capabilities of the kernel's routing subsystem. They can define arbitrary forwarding rules, such as policy-based routing that takes into account not just destination IP but also source IP, port numbers, or even custom packet metadata. For example, an eBPF program can inspect specific headers of an incoming API request and route it to a particular backend service instance based on the API version or user authentication token, a level of granularity difficult to achieve with traditional methods. In the context of service mesh traffic steering, eBPF can enforce granular routing decisions, load balance requests across service instances, and even implement blue/green deployments or canary releases directly at the kernel level, ensuring minimal overhead and maximum performance for microservices communication.
The cumulative benefits of eBPF in network routing are substantial. Firstly, it leads to reduced context switches. By executing logic directly in the kernel, eBPF programs eliminate the frequent and expensive transitions between kernel and user space that plague traditional networking tools. Secondly, it enables accelerated lookups. Custom, highly optimized BPF maps (like LPM tries or hash maps) can often outperform the kernel's general-purpose radix tree for specific routing scenarios, especially when dealing with hot routes or frequently accessed destinations. This direct lookup capability at line rate translates into lower latency for every packet. Finally, eBPF provides greater flexibility. Network administrators can dynamically change routing policies, experiment with new forwarding algorithms, and respond to network events in real-time, all without recompiling the kernel or rebooting systems. This agility is invaluable in modern, dynamic cloud environments where the network needs to adapt continuously to changing application demands and traffic patterns, ensuring that crucial services, including any API gateway handling a multitude of API calls, operate at peak efficiency.
APIPark is a high-performance AI gateway that allows you to securely access the most comprehensive LLM APIs globally on the APIPark platform, including OpenAI, Anthropic, Mistral, Llama2, Google Gemini, and more.Try APIPark now! πππ
Chapter 5: Deep Dive into Routing Table Optimization with eBPF
Optimizing the routing table with eBPF is not a singular task but rather a collection of advanced techniques that leverage eBPF's unique capabilities to improve lookup speeds, enhance policy enforcement, and even offload routing decisions. This chapter explores these techniques in detail, demonstrating how eBPF can transform the performance profile of network routing, which is essential for any high-throughput system like an API gateway.
Technique 1: Accelerated Route Lookups
One of the most immediate and impactful applications of eBPF in routing optimization is the ability to significantly accelerate route lookups. Traditional kernel routing involves traversing complex data structures like radix trees, which, while efficient, still incur overhead for each lookup. eBPF offers alternatives:
- Replacing or augmenting standard kernel lookups with eBPF-optimized maps: eBPF programs, particularly those attached to XDP or TC hooks, can implement their own routing logic using highly specialized BPF maps. For instance, a BPF hash map can store specific destination IP prefixes (keys) and their corresponding next-hop information (values). For frequently accessed routes (known as "hot routes"), an eBPF program can perform a direct hash lookup in this map. If a match is found, the packet can be immediately redirected to the correct next-hop interface, bypassing the entire traditional kernel FIB lookup process. This acts as a high-speed cache or a dedicated fast-path for critical traffic. For routes not found in the BPF map, the program can simply pass the packet to the regular kernel stack for traditional processing, ensuring full routing coverage.
- Direct packet steering based on eBPF logic: Beyond just next-hop determination, eBPF programs can also directly steer packets. For example, an XDP program can identify traffic destined for a specific backend service behind an API gateway and redirect it to a particular CPU core or network queue that is optimized for that service. This minimizes cache contention and ensures workload isolation, leading to more consistent and lower latency API response times.
- Case studies or theoretical examples of performance improvements: Imagine a microservices cluster where an API gateway is handling millions of requests. With traditional routing, each incoming API request's destination IP (the backend service) would undergo a kernel FIB lookup. By pre-populating a BPF map with the IPs of critical backend services and their respective next-hop MAC addresses, an eBPF program can perform these lookups in microseconds at the XDP layer. Benchmarks have shown that XDP with BPF maps can achieve near line-rate forwarding for simple routing decisions, significantly reducing CPU cycles spent per packet compared to the full kernel stack, often leading to a reduction of latency by tens of microseconds per packet and a substantial increase in packets per second (PPS) capacity.
Technique 2: Dynamic Routing Policy Enforcement
eBPF's programmability enables a level of dynamic routing policy enforcement that is challenging to achieve with static configuration tools or even traditional SDN controllers.
- Implementing custom policy routing rules at line speed: Traditional policy-based routing (PBR) often relies on
ip rulesand multiple routing tables, which can add complexity and lookup overhead. With eBPF, a program can inspect any part of a packet (source IP, destination IP, source port, destination port, protocol, even custom headers or metadata) and make routing decisions based on complex, dynamically defined policies. For example, all traffic originating from a specific source IP range and destined for a particular API endpoint can be routed through a dedicated high-bandwidth link, while other traffic uses a default path. This allows for fine-grained traffic engineering directly in the kernel, responding instantly to policy changes. - Microsegmentation and fine-grained access control using eBPF on ingress/egress: In modern data centers, microsegmentation is crucial for security, isolating individual workloads or services. eBPF programs can enforce security policies by inspecting packets at ingress and egress points of virtual machines or containers. For instance, an eBPF program can ensure that only specific microservices are allowed to call certain APIs, or that traffic from unapproved sources is immediately dropped, effectively creating a distributed firewall that scales with the workload. This helps prevent lateral movement of threats and ensures that each API interaction adheres to stringent security protocols.
- Responding to network events in real-time: eBPF can be integrated with external monitoring systems or even internal heuristics to dynamically adjust routing. If a link becomes congested, an eBPF program can be updated in near real-time to re-route traffic around the bottleneck. Similarly, if a backend service instance behind an API gateway becomes unhealthy, an eBPF program can immediately stop forwarding traffic to it and redirect it to a healthy instance, providing faster fault tolerance than traditional health checks and routing updates.
Technique 3: Offloading Routing Decisions to Smart NICs (Hardware Acceleration)
The ultimate optimization for network performance often involves shifting processing away from the main CPU. eBPF, particularly through XDP, facilitates this with hardware offloading.
- Introduction to XDP and its potential for hardware offload: XDP programs are executed directly on the network interface card (NIC) driver, even before the packet is placed into main memory. Some modern Smart NICs or DPUs (Data Processing Units) come with specialized hardware that can directly execute eBPF programs. This means the packet processing, including routing decisions, happens entirely on the NIC.
- How eBPF programs can be loaded onto NICs to perform routing/forwarding: When an XDP program is offloaded to a Smart NIC, the NIC's firmware (or dedicated hardware) runs the eBPF bytecode. This enables line-rate packet processing at wire speed, without consuming any CPU cycles from the host server. For routing, the NIC can perform LPM lookups, direct packet steering, and even encapsulation/decapsulation for tunneling protocols, all in hardware.
- Implications for data centers and high-performance computing: This offloading capability is transformative for environments that demand extreme throughput and minimal latency, such as hyperscale data centers, telecommunication gateway infrastructure, and high-performance computing clusters. It allows the main CPU to focus on application logic, while the NIC handles the heavy lifting of network I/O and routing decisions, dramatically increasing the overall capacity and efficiency of the system.
Technique 4: Integration with Service Meshes and API Gateways
The synergy between eBPF and modern application architectures like service meshes and API gateways is profound, leading to highly optimized traffic management. This is where products like APIPark can shine.
- How eBPF can enhance traffic management within service meshes: In a service mesh, sidecar proxies (like Envoy) are responsible for traffic management, policy enforcement, and observability. While powerful, these user-space proxies introduce latency and CPU overhead due to context switches and memory copies. eBPF can offload many of these functions to the kernel. For example, eBPF can implement kernel-level load balancing, traffic splitting, and even circuit breaking directly, reducing the workload on the sidecar proxy and improving the performance of inter-service API calls. Tools like Cilium, for instance, leverage eBPF extensively to provide a high-performance network fabric for Kubernetes, integrating firewalling, routing, and load balancing directly into the kernel.
- Optimizing traffic flow to backend services behind an API gateway: An API gateway is a critical component for managing and routing external API requests to internal microservices. Its performance directly impacts the responsiveness of an application. By using eBPF, an API gateway can benefit from kernel-level optimizations for its routing decisions. For example, an eBPF program can pre-filter and route API requests based on their destination, protocol, or even specific headers before the request ever reaches the user-space API gateway application. This can accelerate the initial routing decision, reduce the load on the API gateway's main processing logic, and enable more efficient load balancing to backend service instances.
- Example: using eBPF to implement advanced load balancing and traffic splitting logic directly in the kernel, reducing overhead for the API gateway or proxy. Consider a scenario where an API gateway needs to perform a 90/10 traffic split for a new API version or route specific users to a different cluster. An eBPF program can be configured to inspect incoming API requests, identify the relevant criteria (e.g., a specific HTTP header, a cookie, or a URL path), and then probabilistically or deterministically forward the packets to the appropriate backend service IP addresses, all within the kernel. This significantly reduces the overhead on the user-space API gateway which would otherwise perform these complex routing decisions, allowing it to focus on higher-level API management tasks like authentication, rate limiting, and analytics. Platforms like APIPark, an open-source AI gateway and API management platform, can leverage such kernel-level optimizations to deliver superior performance and more resilient API interactions, ensuring that its powerful features like quick integration of AI models, unified API format, and end-to-end API lifecycle management are backed by an ultra-fast and efficient network fabric. The ability to manage and route API traffic with minimal latency and high throughput is a core advantage that eBPF-driven kernel enhancements provide to robust API gateway solutions like APIPark, enabling them to achieve performance rivaling Nginx and support large-scale traffic demands.
This table provides a concise comparison of traditional kernel routing and eBPF-enhanced routing in terms of their key characteristics and capabilities:
| Feature | Traditional Kernel Routing | eBPF-Enhanced Routing |
|---|---|---|
| Execution Context | Primarily kernel space, with user-space daemons for updates. | Kernel space (XDP, TC hooks) with user-space for program loading/map management. |
| Programmability | Static configuration via ip route, ip rules. Limited flexibility. |
Fully programmable via eBPF bytecode. Highly dynamic and flexible. |
| Performance | Good, but subject to context switches, sk_buff allocation, general-purpose FIB lookups. |
Excellent. Near line-rate processing possible (XDP). Avoids context switches, direct packet manipulation. |
| Lookup Mechanism | Radix trees (FIB), hash tables for caches. General-purpose. | BPF maps (hash, LPM trie, array). Highly specialized and optimized for specific use cases. |
| Policy Enforcement | ip rules, iptables/nftables. Often multi-table lookups. |
Direct programmatic enforcement within eBPF program. Fine-grained rules on any packet field. |
| Dynamic Adaptation | Slower, relies on routing protocol updates and kernel FIB refresh. | Real-time adaptation via BPF map updates from user space. Instant response to network changes. |
| Resource Consumption | Moderate CPU for packet processing, higher for complex rules. | Lower CPU per packet (especially XDP). Can offload to Smart NICs. Efficient memory for BPF maps. |
| Debugging Complexity | Well-established tools (ip route, tcpdump). |
Requires specialized eBPF tools (bpftool, bcc). Steeper learning curve. |
| Hardware Offload | Limited to basic L2/L3 forwarding features (e.g., flow steering). | Extensive via XDP offload to Smart NICs for wire-speed processing. |
| Use Cases | General internet routing, basic server forwarding. | High-performance data centers, service meshes, API gateway optimization, real-time traffic engineering, security. |
Chapter 6: Practical Implementations and Development Considerations
Moving from theoretical understanding to practical application, implementing eBPF for routing optimization requires navigating a specific ecosystem of tools, programming models, and development practices. While powerful, eBPF development has its own set of considerations for successful deployment and maintenance.
eBPF Tooling and Ecosystem
The rapid growth of eBPF has fostered a rich and expanding ecosystem of tools that simplify its development and deployment:
- BCC (BPF Compiler Collection): BCC is a toolkit for creating efficient kernel tracing and manipulation programs. It provides Python (and other language) bindings for writing eBPF programs, compiling them, and interacting with BPF maps. BCC significantly lowers the barrier to entry for eBPF development, abstracting away much of the low-level C code and kernel interface details. It's excellent for rapid prototyping, debugging, and simple, high-level eBPF scripts. For routing, BCC can be used to quickly experiment with different packet filtering and redirection logic.
- libbpf: This is a C/C++ library that provides a more robust and lower-level interface for loading and managing eBPF programs and maps. Unlike BCC,
libbpffocuses on "CO-RE" (Compile Once β Run Everywhere), meaning eBPF programs compiled withlibbpfcan run on different kernel versions without recompilation, provided the kernel supports the necessary eBPF features. This makeslibbpfthe preferred choice for production-grade eBPF applications, including those enhancing routing in complex environments. - bpftool: A powerful command-line utility for inspecting and managing eBPF programs, maps, and objects within the kernel.
bpftoolallows developers to list loaded programs, inspect their bytecode, view map contents, and attach/detach programs to various hooks. It's an indispensable tool for debugging and monitoring running eBPF-based routing solutions. - Cilium: While a full-fledged CNI (Container Network Interface) for Kubernetes, Cilium is built entirely on eBPF. It uses eBPF for network connectivity, load balancing, security policies, and observability for container workloads. Cilium exemplifies how eBPF can be used to implement advanced routing logic, service mesh features, and network policies directly in the kernel, providing high-performance and secure networking for microservices, including optimized traffic flow for APIs managed by an API gateway. Its approach to service routing and load balancing within Kubernetes is a prime example of eBPF's capabilities.
Programming eBPF
Developing eBPF programs typically involves two distinct parts:
- C for the kernel part: The actual eBPF program that runs in the kernel is written in a restricted subset of C. This C code is then compiled into eBPF bytecode using a specialized LLVM backend. This kernel-side code contains the logic for inspecting packets, performing lookups in BPF maps, and issuing actions like
BPF_OK,BPF_DROP, orBPF_REDIRECT. Developers must be familiar with kernel network programming concepts and the specific eBPF helper functions available for their chosen hook (e.g.,bpf_map_lookup_elem,bpf_redirect). - Python/Go for user-space control: A user-space application is responsible for loading the eBPF program into the kernel, creating and populating BPF maps, and potentially interacting with the eBPF program by reading map statistics or dynamically updating map entries. Languages like Python (with BCC) or Go (with
libbpfbindings) are commonly used for this control plane. This user-space component might receive routing updates from a routing daemon, translate them into BPF map updates, or implement high-level routing policies. For instance, an API gateway's control plane could dynamically update an eBPF map with new backend service IPs as they scale up or down, ensuring immediate routing adjustments.
Deployment Strategies
Deploying eBPF programs for routing optimization involves careful planning:
- Loading programs: eBPF programs are typically loaded into the kernel using the
bpf()system call from a user-space application. The program bytecode is passed to the kernel, which then verifies and JIT-compiles it. - Attaching to hooks: Once loaded, the eBPF program must be attached to a specific kernel hook. For routing, this commonly involves
XDP(viaip link set dev <iface> xdp obj <bpf_prog_path>) orTC(viatc filter add dev <iface> ingress bpf object-file <bpf_prog_path>). The choice of hook depends on the desired point of intervention and the required packet context. - Managing BPF maps: BPF maps are crucial for stateful eBPF programs. They must be created (or "pinned" in the BPF filesystem for persistence) and then populated with data by the user-space application. For routing, this means filling maps with IP prefixes, next-hop MACs, or policy rules. Updates to routing policies or changes in network topology require corresponding updates to these maps by the user-space controller.
Testing and Debugging eBPF programs
Testing and debugging eBPF programs can be challenging due to their kernel-level execution:
- Challenges: Debugging inside the kernel is inherently more difficult than in user space. Traditional debuggers cannot directly attach to eBPF programs. Errors in eBPF programs, though prevented from crashing the kernel by the verifier, can still lead to incorrect packet forwarding, performance regressions, or security vulnerabilities if the logic is flawed. The verifier itself can sometimes be overly strict, making program development tricky.
- Techniques:
bpf_printk: A kernel helper function that allows eBPF programs to print messages to the kernel log (dmesg), invaluable for understanding program flow and variable values.- BPF map introspection: Reading the contents of BPF maps from user space (
bpftool map dump) can help verify that routing tables are correctly populated and updated. - Tracing tools: Tools like
perf,bpftool prog tracelog, andBCC'straceandargdistcan be used to observe eBPF program execution, helper function calls, and performance metrics. - Test environments: Running eBPF programs in isolated virtual machines or containerized environments allows for safe experimentation and robust testing before deployment to production.
Security Implications
While eBPF is designed with security in mind, there are still considerations:
- The verifier: The BPF verifier is the primary security mechanism, ensuring programs are safe and memory-access compliant. Understanding its rules is crucial.
- Sandboxing: eBPF programs operate in a sandboxed environment, limiting their access to arbitrary kernel memory.
- Privilege requirements: Loading eBPF programs typically requires
CAP_BPForCAP_SYS_ADMINcapabilities, meaning only privileged processes can interact directly with eBPF. This makes controlling who can load eBPF programs a critical security measure. - Side-channel attacks: As with any kernel-level code, there's a theoretical risk of side-channel attacks, though practical exploits are complex. Keeping the kernel and eBPF tooling updated is essential.
Performance Measurement
Accurately measuring the impact of eBPF routing optimizations is critical:
- Benchmarking tools: Tools like
iperf3,pktgen, andflentcan generate various types of network traffic to stress the routing subsystem. - Methodologies:
- Throughput (PPS): Measure packets per second with and without eBPF optimization. XDP is particularly effective here.
- Latency: Measure round-trip time for packets, focusing on jitter and tail latency.
- CPU utilization: Compare CPU cycles consumed by network processing. eBPF should ideally reduce this, freeing up CPU for application tasks, especially for an API gateway.
- Memory usage: Monitor memory consumption by BPF maps.
- Baseline comparison: Always establish a solid baseline performance with traditional routing before introducing eBPF, to accurately quantify the gains. This ensures that the optimizations truly deliver the expected benefits for
apitraffic andgatewayperformance.
Chapter 7: Real-World Use Cases and Future Trends
The theoretical advantages of eBPF in routing are not merely academic; they are being rigorously applied and continuously evolved in some of the most demanding network environments globally. From hyperscale cloud infrastructure to enterprise data centers, eBPF is reshaping how networks are built, managed, and optimized, profoundly impacting critical components like an API gateway and the entire lifecycle of an API.
Hyperscale Cloud Providers
The leading hyperscale cloud providers are at the forefront of eBPF adoption, leveraging its capabilities to build their colossal network infrastructures. * How they leverage eBPF for networking: Cloud providers face immense challenges in providing elastic, secure, and high-performance networking to millions of tenants. They use eBPF to implement software-defined networking (SDN) solutions that manage virtual networks, load balancing, and network security policies directly in the kernel. For instance, eBPF is used to encapsulate/decapsulate packets for overlay networks (like VXLAN or Geneve), perform efficient load balancing across thousands of virtual machines or containers, and enforce tenant isolation through fine-grained firewall rules. This allows them to achieve incredible network agility and scale while maintaining high performance for all tenant workloads, including critical API services hosted on their platforms. The ability of eBPF to offload routing and forwarding decisions to Smart NICs is particularly appealing for these providers, as it frees up host CPU cycles, reduces operational costs, and increases the overall throughput of their virtualized networks.
Telcos and ISPs
Telecommunication companies and Internet Service Providers (ISPs) are exploring and deploying eBPF to enhance their core and edge network functions. * Enhancing edge routing and subscriber management: At the edge of the network, where millions of subscribers connect, efficient traffic management and policy enforcement are paramount. eBPF can be used to implement highly customized subscriber management policies, such as per-user bandwidth limits, QoS guarantees for specific services (e.g., VoIP), or sophisticated traffic steering based on application type. It can accelerate routing decisions for vast numbers of subscriber routes, ensuring low latency for critical services and efficient utilization of network resources. For example, an eBPF program could identify specific types of api traffic and prioritize it over bulk data transfers, ensuring a smooth experience for interactive applications.
Enterprises
Enterprises of all sizes are increasingly adopting eBPF to improve their internal network operations, especially within their private clouds and data centers. * Improving intra-datacenter communication, securing API traffic: In modern enterprise data centers, microservices communicate extensively, often via API calls. eBPF is used to optimize this intra-datacenter communication by providing high-performance load balancing for internal services, implementing granular network policies for microsegmentation, and accelerating traffic flow between different application tiers. Critically, eBPF enhances the security of API traffic by enforcing fine-grained access controls directly in the kernel, ensuring that only authorized services can communicate and that sensitive api data is protected. This makes eBPF an invaluable tool for securing the backend of an API gateway and ensuring compliance with regulatory requirements.
Emerging Trends
The journey of eBPF is far from over, with several exciting trends pointing towards its continued evolution and broader impact.
- Further hardware offloading: The trend of offloading eBPF programs to Smart NICs and DPUs will continue to accelerate. As these specialized hardware platforms become more powerful and ubiquitous, an increasing portion of the network stack, including complex routing, firewalling, and even application-layer protocol parsing for APIs, will execute directly on the NIC, pushing network performance to unprecedented levels and freeing up general-purpose CPUs for application logic. This will be transformative for any high-performance gateway solution.
- Integration with AI/ML for adaptive routing: The combination of eBPF's real-time kernel insights and the analytical power of Artificial Intelligence and Machine Learning (AI/ML) promises truly adaptive routing. AI/ML models could analyze network telemetry (collected via eBPF) to predict congestion, identify anomalous traffic patterns, or optimize routing paths dynamically. An eBPF program could then receive real-time updates from an AI/ML controller, adjusting routing tables or traffic steering policies on the fly, offering unprecedented levels of network optimization and resilience. This could enable an API gateway to intelligently route traffic based on predicted backend load or user behavior.
- The convergence of eBPF with network function virtualization (NFV): eBPF is becoming a cornerstone for highly optimized Network Function Virtualization. Many traditionally hardware-based network functions (e.g., routers, firewalls, load balancers, VPN gateways) can be implemented as virtual network functions (VNFs) running on commodity servers. eBPF provides the necessary performance and programmability to make these VNFs incredibly efficient, rivaling or even surpassing hardware solutions by executing core data plane logic directly in the kernel. This convergence will lead to more flexible, scalable, and cost-effective network infrastructures.
- The role of eBPF in future generations of API gateway and microservices architectures: As microservices architectures continue to evolve, the demand for highly efficient, secure, and observable inter-service communication will only intensify. eBPF is poised to play an even more central role in future API gateway designs, potentially abstracting away network complexity even further from application developers. It could enable direct service-to-service communication with kernel-level policies, dynamic traffic shaping for APIs, and advanced security mechanisms that are transparent to the application layer. This will allow platforms like APIPark to further enhance their "Performance Rivaling Nginx" claim by leveraging these deeper kernel integrations, ensuring that every
apicall is handled with optimal speed and security. The robust data analysis and detailed API call logging features of APIPark, for example, could be significantly empowered by the granular insights and control provided by eBPF, enabling more sophisticated predictive maintenance and real-time operational intelligence. The continuous evolution of eBPF will undoubtedly continue to push the boundaries of what's possible in network performance, making it an indispensable technology for the digital future.
Chapter 8: Challenges and Limitations of eBPF in Routing
Despite its transformative potential, implementing eBPF for routing optimization is not without its challenges and limitations. Understanding these aspects is crucial for realistic planning and successful deployment, ensuring that the technology is applied judiciously and effectively within complex network environments, including those involving an API gateway.
Complexity of Development and Debugging
One of the primary hurdles for wider eBPF adoption is the inherent complexity of development and debugging. * Steep learning curve: Writing eBPF programs, particularly for networking, requires a deep understanding of the Linux kernel's internal workings, network stack architecture, and the specific eBPF instruction set and helper functions. Developers need to be proficient in a restricted subset of C, understand concepts like BPF maps, and be familiar with the various kernel hooks (XDP, TC, sockets, etc.). This steep learning curve can deter traditional network engineers who are accustomed to declarative configuration languages or high-level scripting. * Limited debugging tools: While tools like bpftool and bpf_printk are invaluable, debugging eBPF programs remains more challenging than debugging user-space applications. Tracing and pinpointing issues in kernel-level code, especially within a highly optimized and sandboxed environment, requires specialized knowledge and can be time-consuming. Misconfigurations or logical errors in an eBPF program can lead to subtle network issues that are hard to diagnose without deep eBPF expertise.
Kernel Compatibility and Versioning
eBPF's evolution is tightly coupled with the Linux kernel's development, leading to kernel compatibility and versioning challenges. * Feature availability: Newer eBPF features, helper functions, and map types are introduced with newer kernel versions. This means that an eBPF program written for a bleeding-edge kernel might not run on an older, production-hardened kernel. Organizations often run older, stable kernel versions, which can limit the adoption of the latest eBPF capabilities. * ABI stability: While the eBPF bytecode itself is generally stable, the kernel's internal data structures and function prototypes (which eBPF programs might need to read or call) can change between kernel versions. This necessitates the use of "CO-RE" (Compile Once β Run Everywhere) techniques and tools like libbpf to ensure programs can adapt to different kernel layouts without recompilation, but it still adds a layer of complexity for developers. Maintaining eBPF programs across a fleet of servers with heterogeneous kernel versions requires careful management and testing.
Resource Consumption (CPU, Memory for Maps)
While eBPF is lauded for its efficiency, it's not without its own resource consumption. * CPU for eBPF execution: While an eBPF program reduces context switches, its execution still consumes CPU cycles. For very complex eBPF programs or extremely high packet rates, the CPU overhead of executing the eBPF bytecode, even after JIT compilation, can become noticeable. The balance lies in ensuring the complexity of the eBPF logic is justified by the performance gains. * Memory for BPF maps: BPF maps, especially large hash tables or LPM tries used for routing, consume kernel memory. In environments with hundreds of thousands of routes, these maps can become substantial. Careful design is needed to manage memory footprint, avoid memory leaks (though the kernel manages map lifecycle, improper use can lead to bloat), and ensure maps are sized appropriately for the routing table scale. An inefficiently designed eBPF map for an API gateway's backend services could consume excessive memory, impacting overall system stability.
The Learning Curve for Traditional Network Engineers
As mentioned earlier, the learning curve for traditional network engineers is significant. * Shift from configuration to programming: Network engineers are typically accustomed to declarative configuration files, command-line interfaces, and network protocols. eBPF, on the other hand, demands a programming mindset, requiring knowledge of C, data structures, algorithms, and low-level kernel interactions. This paradigm shift can be a major barrier to adoption within network operations teams. * Need for cross-domain expertise: Effectively utilizing eBPF for routing requires a blend of networking, systems programming, and kernel internals expertise. This necessitates either upskilling existing teams or hiring individuals with a rare combination of skills. Bridging this knowledge gap is a critical factor for successful eBPF integration in any organization.
The Balance Between Flexibility and Maintainability
eBPF offers immense flexibility, but this comes with an implicit trade-off with maintainability. * Increased complexity of custom logic: While custom routing logic can unlock significant performance gains, it also introduces custom code into the kernel's data path. This bespoke code is unique to the deployment, meaning it needs to be thoroughly documented, tested, and maintained by the organization. Unlike off-the-shelf routing daemons, there's no widespread community support for highly specialized eBPF routing programs. * Long-term support and upgrades: Upgrading kernel versions or changing underlying network hardware might necessitate modifications to eBPF programs if they rely on specific kernel internals or NIC features. This adds to the operational overhead compared to relying on well-established, kernel-supported routing mechanisms. Organizations need to weigh the immediate performance benefits against the long-term maintenance burden, especially for critical infrastructure like an API gateway which requires high stability and availability.
In conclusion, while eBPF provides powerful tools for routing optimization, successful implementation requires a clear understanding of its technical demands, careful resource management, and a commitment to continuous learning and maintenance. It is a tool best wielded by those prepared to invest in its intricacies, reaping substantial benefits in performance and flexibility for environments that truly demand it.
Conclusion: eBPF β The Future of High-Performance Routing
The journey through the intricate world of network routing and the revolutionary capabilities of eBPF reveals a clear trajectory: eBPF is fundamentally transforming the landscape of network performance optimization. We have explored the pressing challenges of modern networks, driven by unprecedented data growth and the architectural shifts towards microservices and cloud-native paradigms. These challenges highlight the inherent limitations of traditional kernel networking stacks, which often struggle to deliver the low-latency, high-throughput, and dynamic adaptability required by contemporary applications, especially those relying heavily on an efficient API gateway for managing myriad API calls.
eBPF emerges as a powerful antidote to these limitations, acting as a programmable, safe, and highly efficient engine within the Linux kernel. Its ability to execute user-defined programs directly within kernel context, leverage specialized data structures (BPF maps), and interface at critical network hooks like XDP and TC, represents a transformative power for routing optimization. By enabling accelerated route lookups, dynamic policy enforcement, and even hardware offloading of routing decisions, eBPF allows network engineers to bypass traditional bottlenecks and engineer bespoke routing solutions that precisely match the demands of their workloads.
The benefits are profound and far-reaching. Enterprises can achieve reduced context switches, leading to more efficient CPU utilization and lower latency for every packet. Accelerated lookups ensure that routing decisions are made at near line-rate speeds, critical for high-volume API traffic flowing through an API gateway. Most importantly, eBPF provides greater flexibility, allowing networks to adapt in real-time to changing conditions, implement sophisticated traffic engineering, and enhance security policies with unparalleled granularity. From hyperscale cloud environments achieving extreme throughput to enterprise data centers securing their API communication, eBPF is proving its mettle in diverse, demanding scenarios. Products like APIPark, an open-source AI gateway and API management platform, are prime examples of solutions that can significantly benefit from these kernel-level optimizations to deliver superior performance and resilience in handling modern API workloads, demonstrating how eBPF underpins the next generation of network-aware application infrastructure.
Looking ahead, the future outlook for eBPF is incredibly bright. Further advancements in hardware offloading, deeper integration with AI/ML for truly adaptive networks, and its expanding role in network function virtualization will continue to push the boundaries of what's possible. As networks become increasingly complex and applications demand even greater performance, eBPF will solidify its position as an indispensable technology for building high-performance, resilient, and agile network infrastructures. Its continued impact on networking, particularly for critical API gateway infrastructures and microservices communication, will be a defining characteristic of the digital networks of tomorrow.
In essence, eBPF is not just an incremental improvement; it is a fundamental shift in how we conceive and manage network operations. It empowers us to build networks that are not only faster and more efficient but also more intelligent, secure, and adaptable β truly ready for the challenges and opportunities of the future. The optimization of routing tables with eBPF is a cornerstone of this revolution, enabling systems to deliver data with unparalleled precision and speed, fueling the next wave of innovation in cloud computing, AI, and distributed applications.
Frequently Asked Questions (FAQs)
1. What is eBPF, and how does it relate to network routing? eBPF (extended Berkeley Packet Filter) is a revolutionary technology that allows user-defined programs to run safely within the Linux kernel. For network routing, it enables developers to implement custom packet processing and forwarding logic directly in the kernel's data path, bypassing parts of the traditional network stack. This allows for accelerated route lookups, dynamic policy enforcement, and significantly enhanced network performance compared to conventional methods, especially beneficial for high-traffic environments like those managed by an API gateway.
2. What are the main advantages of using eBPF for routing table optimization? The primary advantages include reduced context switches between kernel and user space, leading to lower CPU utilization per packet; accelerated route lookups through highly optimized BPF maps (like hash tables or LPM tries); and increased flexibility to implement sophisticated, dynamic routing policies at line speed. These benefits translate into lower latency, higher throughput, and more agile network behavior, crucial for applications that rely on fast API responses.
3. Can eBPF replace traditional routing protocols like BGP or OSPF? No, eBPF typically complements, rather than replaces, traditional routing protocols. Routing protocols like BGP or OSPF are responsible for distributing routing information and building the overall routing table. eBPF then takes this information (or derived policies) and applies it to the kernel's data path for accelerated packet forwarding. An eBPF program might receive updates from a user-space routing daemon (which implements BGP/OSPF) and dynamically update its BPF maps to reflect changes in the network topology or policy, thereby enhancing the efficiency of the established routing decisions, not overriding the protocol itself.
4. What are the key challenges when implementing eBPF for network routing? Key challenges include a steep learning curve due to the need for deep kernel knowledge and C programming expertise, limited debugging tools compared to user-space applications, and managing kernel compatibility across different system versions. There's also the need to carefully balance the immense flexibility eBPF offers with the long-term maintainability of custom kernel-level code. For critical services like an API gateway, these challenges must be meticulously addressed to ensure stability and reliability.
5. How does eBPF impact API Gateway performance and security? eBPF significantly enhances API gateway performance by allowing for kernel-level optimization of traffic flow to backend services. It can accelerate routing decisions for incoming API requests, perform advanced load balancing, and implement granular traffic splitting logic directly in the kernel, reducing the overhead on the user-space API gateway application. From a security perspective, eBPF can enforce fine-grained access control policies and microsegmentation for API traffic at the earliest possible point, strengthening the overall security posture and ensuring that sensitive API interactions are protected against unauthorized access or breaches.
πYou can securely and efficiently call the OpenAI API on APIPark in just two steps:
Step 1: Deploy the APIPark AI gateway in 5 minutes.
APIPark is developed based on Golang, offering strong product performance and low development and maintenance costs. You can deploy APIPark with a single command line.
curl -sSO https://download.apipark.com/install/quick-start.sh; bash quick-start.sh

In my experience, you can see the successful deployment interface within 5 to 10 minutes. Then, you can log in to APIPark using your account.

Step 2: Call the OpenAI API.
