1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2010-2014 Intel Corporation. 3 4IPv4 Multicast Sample Application 5================================= 6 7The IPv4 Multicast application is a simple example of packet processing 8using the Data Plane Development Kit (DPDK). 9The application performs L3 multicasting. 10 11Overview 12-------- 13 14The application demonstrates the use of zero-copy buffers for packet forwarding. 15The initialization and run-time paths are very similar to those of the :doc:`l2_forward_real_virtual`. 16This guide highlights the differences between the two applications. 17There are two key differences from the L2 Forwarding sample application: 18 19* The IPv4 Multicast sample application makes use of indirect buffers. 20 21* The forwarding decision is taken based on information read from the input packet's IPv4 header. 22 23The lookup method is the Four-byte Key (FBK) hash-based method. 24The lookup table is composed of pairs of destination IPv4 address (the FBK) 25and a port mask associated with that IPv4 address. 26By default, the following IP addresses and their respective port masks are added: 27 28.. literalinclude:: ../../../examples/ipv4_multicast/main.c 29 :language: c 30 :start-after: mcast_group_table 31 :end-before: }; 32 33.. note:: 34 35 The max port mask supported in the given hash table is 0xf, so only first 36 four ports can be supported. 37 If using non-consecutive ports, use the destination IPv4 address accordingly. 38 39For convenience and simplicity, this sample application does not take IANA-assigned multicast addresses into account, 40but instead equates the last four bytes of the multicast group (that is, the last four bytes of the destination IP address) 41with the mask of ports to multicast packets to. 42Also, the application does not consider the Ethernet addresses; 43it looks only at the IPv4 destination address for any given packet. 44 45Compiling the Application 46------------------------- 47 48To compile the sample application see :doc:`compiling`. 49 50The application is located in the ``ipv4_multicast`` sub-directory. 51 52Running the Application 53----------------------- 54 55The application has a number of command line options: 56 57.. code-block:: console 58 59 ./<build_dir>/examples/dpdk-ipv4_multicast [EAL options] -- -p PORTMASK [-q NQ] 60 61where, 62 63* -p PORTMASK: Hexadecimal bitmask of ports to configure 64 65* -q NQ: determines the number of queues per lcore 66 67.. note:: 68 69 Unlike the basic L2/L3 Forwarding sample applications, 70 NUMA support is not provided in the IPv4 Multicast sample application. 71 72Typically, to run the IPv4 Multicast sample application, issue the following command (as root): 73 74.. code-block:: console 75 76 ./<build_dir>/examples/dpdk-ipv4_multicast -l 0-3 -n 3 -- -p 0x3 -q 1 77 78In this command: 79 80* The -l option enables cores 0, 1, 2 and 3 81 82* The -n option specifies 3 memory channels 83 84* The -p option enables ports 0 and 1 85 86* The -q option assigns 1 queue to each lcore 87 88Refer to the *DPDK Getting Started Guide* for general information on running applications 89and the Environment Abstraction Layer (EAL) options. 90 91Explanation 92----------- 93 94The following sections provide some explanation of the code. 95As mentioned in the overview section, 96the initialization and run-time paths are very similar to those of the :doc:`l2_forward_real_virtual`. 97The following sections describe aspects that are specific to the IPv4 Multicast sample application. 98 99Memory Pool Initialization 100~~~~~~~~~~~~~~~~~~~~~~~~~~ 101 102The IPv4 Multicast sample application uses three memory pools. 103Two of the pools are for indirect buffers used for packet duplication purposes. 104Memory pools for indirect buffers are initialized differently from the memory pool for direct buffers: 105 106.. literalinclude:: ../../../examples/ipv4_multicast/main.c 107 :language: c 108 :start-after: Create the mbuf pools. 8< 109 :end-before: >8 End of create mbuf pools. 110 :dedent: 1 111 112The reason for this is because indirect buffers are not supposed to hold any packet data and 113therefore can be initialized with lower amount of reserved memory for each buffer. 114 115Hash Initialization 116~~~~~~~~~~~~~~~~~~~ 117 118The hash object is created and loaded with the pre-configured entries read from a global array: 119 120.. literalinclude:: ../../../examples/ipv4_multicast/main.c 121 :language: c 122 :start-after: Hash object is created and loaded. 8< 123 :end-before: >8 End of hash object is created and loaded. 124 125Forwarding 126~~~~~~~~~~ 127 128All forwarding is done inside the mcast_forward() function. 129Firstly, the Ethernet* header is removed from the packet and the IPv4 address is extracted from the IPv4 header: 130 131.. literalinclude:: ../../../examples/ipv4_multicast/main.c 132 :language: c 133 :start-after: Remove the Ethernet header from the input packet. 8< 134 :end-before: >8 End of removing the Ethernet header from the input packet. 135 :dedent: 1 136 137Then, the packet is checked to see if it has a multicast destination address and 138if the routing table has any ports assigned to the destination address: 139 140.. literalinclude:: ../../../examples/ipv4_multicast/main.c 141 :language: c 142 :start-after: Check valid multicast address. 8< 143 :end-before: >8 End of valid multicast address check. 144 :dedent: 1 145 146Then, the number of ports in the destination portmask is calculated with the help of the bitcnt() function: 147 148.. literalinclude:: ../../../examples/ipv4_multicast/main.c 149 :language: c 150 :start-after: Get number of bits set. 8< 151 :end-before: >8 End of getting number of bits set. 152 153This is done to determine which forwarding algorithm to use. 154This is explained in more detail in the next section. 155 156Thereafter, a destination Ethernet address is constructed: 157 158.. literalinclude:: ../../../examples/ipv4_multicast/main.c 159 :language: c 160 :start-after: Construct destination ethernet address. 8< 161 :end-before: >8 End of constructing destination ethernet address. 162 :dedent: 1 163 164Since Ethernet addresses are also part of the multicast process, each outgoing packet carries the same destination Ethernet address. 165The destination Ethernet address is constructed from the lower 23 bits of the multicast group OR-ed 166with the Ethernet address 01:00:5e:00:00:00, as per RFC 1112: 167 168.. literalinclude:: ../../../examples/ipv4_multicast/main.c 169 :language: c 170 :start-after: Construct Ethernet multicast address from IPv4 multicast Address. 8< 171 :end-before: >8 End of Construction of multicast address from IPv4 multicast address. 172 173Then, packets are dispatched to the destination ports according to the portmask associated with a multicast group: 174 175.. literalinclude:: ../../../examples/ipv4_multicast/main.c 176 :language: c 177 :start-after: Packets dispatched to destination ports. 8< 178 :end-before: >8 End of packets dispatched to destination ports. 179 :dedent: 1 180 181The actual packet transmission is done in the mcast_send_pkt() function: 182 183.. literalinclude:: ../../../examples/ipv4_multicast/main.c 184 :language: c 185 :start-after: Write new Ethernet header to outgoing packets. 8< 186 :end-before: >8 End of writing new Ethernet headers. 187 188Buffer Cloning 189~~~~~~~~~~~~~~ 190 191This is the most important part of the application since it demonstrates the use of zero- copy buffer cloning. 192There are two approaches for creating the outgoing packet and although both are based on the data zero-copy idea, 193there are some differences in the detail. 194 195The first approach creates a clone of the input packet, for example, 196walk though all segments of the input packet and for each of segment, 197create a new buffer and attach that new buffer to the segment 198(refer to rte_pktmbuf_clone() in the rte_mbuf library for more details). 199A new buffer is then allocated for the packet header and is prepended to the cloned buffer. 200 201The second approach does not make a clone, it just increments the reference counter for all input packet segment, 202allocates a new buffer for the packet header and prepends it to the input packet. 203 204Basically, the first approach reuses only the input packet's data, but creates its own copy of packet's metadata. 205The second approach reuses both input packet's data and metadata. 206 207The advantage of first approach is that each outgoing packet has its own copy of the metadata, 208so we can safely modify the data pointer of the input packet. 209That allows us to skip creation if the output packet is for the last destination port 210and instead modify input packet's header in place. 211For example, for N destination ports, we need to invoke mcast_out_pkt() (N-1) times. 212 213The advantage of the second approach is that there is less work to be done for each outgoing packet, 214that is, the "clone" operation is skipped completely. 215However, there is a price to pay. 216The input packet's metadata must remain intact, so for N destination ports, 217we need to invoke mcast_out_pkt() (N) times. 218 219Therefore, for a small number of outgoing ports (and segments in the input packet), 220first approach is faster. 221As the number of outgoing ports (and/or input segments) grows, the second approach becomes more preferable. 222 223Depending on the number of segments or the number of ports in the outgoing portmask, 224either the first (with cloning) or the second (without cloning) approach is taken: 225 226.. literalinclude:: ../../../examples/ipv4_multicast/main.c 227 :language: c 228 :start-after: Should we use rte_pktmbuf_clone() or not. 8< 229 :end-before: >8 End of using rte_pktmbuf_clone(). 230 :dedent: 1 231 232It is the mcast_out_pkt() function that performs the packet duplication (either with or without actually cloning the buffers): 233 234.. literalinclude:: ../../../examples/ipv4_multicast/main.c 235 :language: c 236 :start-after: mcast_out_pkt 8< 237 :end-before: >8 End of mcast_out_kt. 238