actions.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/skbuff.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <linux/openvswitch.h>
  23. #include <linux/netfilter_ipv6.h>
  24. #include <linux/sctp.h>
  25. #include <linux/tcp.h>
  26. #include <linux/udp.h>
  27. #include <linux/in6.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/if_vlan.h>
  30. #include <net/dst.h>
  31. #include <net/ip.h>
  32. #include <net/ipv6.h>
  33. #include <net/ip6_fib.h>
  34. #include <net/checksum.h>
  35. #include <net/dsfield.h>
  36. #include <net/mpls.h>
  37. #include <net/sctp/checksum.h>
  38. #include "datapath.h"
  39. #include "flow.h"
  40. #include "conntrack.h"
  41. #include "vport.h"
  42. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  43. struct sw_flow_key *key,
  44. const struct nlattr *attr, int len);
  45. struct deferred_action {
  46. struct sk_buff *skb;
  47. const struct nlattr *actions;
  48. /* Store pkt_key clone when creating deferred action. */
  49. struct sw_flow_key pkt_key;
  50. };
  51. #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
  52. struct ovs_frag_data {
  53. unsigned long dst;
  54. struct vport *vport;
  55. struct ovs_skb_cb cb;
  56. __be16 inner_protocol;
  57. u16 network_offset; /* valid only for MPLS */
  58. u16 vlan_tci;
  59. __be16 vlan_proto;
  60. unsigned int l2_len;
  61. u8 l2_data[MAX_L2_LEN];
  62. };
  63. static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
  64. #define DEFERRED_ACTION_FIFO_SIZE 10
  65. #define OVS_RECURSION_LIMIT 5
  66. #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
  67. struct action_fifo {
  68. int head;
  69. int tail;
  70. /* Deferred action fifo queue storage. */
  71. struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
  72. };
  73. struct recirc_keys {
  74. struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
  75. };
  76. static struct action_fifo __percpu *action_fifos;
  77. static struct recirc_keys __percpu *recirc_keys;
  78. static DEFINE_PER_CPU(int, exec_actions_level);
  79. static void action_fifo_init(struct action_fifo *fifo)
  80. {
  81. fifo->head = 0;
  82. fifo->tail = 0;
  83. }
  84. static bool action_fifo_is_empty(const struct action_fifo *fifo)
  85. {
  86. return (fifo->head == fifo->tail);
  87. }
  88. static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
  89. {
  90. if (action_fifo_is_empty(fifo))
  91. return NULL;
  92. return &fifo->fifo[fifo->tail++];
  93. }
  94. static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
  95. {
  96. if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
  97. return NULL;
  98. return &fifo->fifo[fifo->head++];
  99. }
  100. /* Return true if fifo is not full */
  101. static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
  102. const struct sw_flow_key *key,
  103. const struct nlattr *attr)
  104. {
  105. struct action_fifo *fifo;
  106. struct deferred_action *da;
  107. fifo = this_cpu_ptr(action_fifos);
  108. da = action_fifo_put(fifo);
  109. if (da) {
  110. da->skb = skb;
  111. da->actions = attr;
  112. da->pkt_key = *key;
  113. }
  114. return da;
  115. }
  116. static void invalidate_flow_key(struct sw_flow_key *key)
  117. {
  118. key->eth.type = htons(0);
  119. }
  120. static bool is_flow_key_valid(const struct sw_flow_key *key)
  121. {
  122. return !!key->eth.type;
  123. }
  124. static void update_ethertype(struct sk_buff *skb, struct ethhdr *hdr,
  125. __be16 ethertype)
  126. {
  127. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  128. __be16 diff[] = { ~(hdr->h_proto), ethertype };
  129. skb->csum = ~csum_partial((char *)diff, sizeof(diff),
  130. ~skb->csum);
  131. }
  132. hdr->h_proto = ethertype;
  133. }
  134. static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  135. const struct ovs_action_push_mpls *mpls)
  136. {
  137. struct mpls_shim_hdr *new_mpls_lse;
  138. /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
  139. if (skb->encapsulation)
  140. return -ENOTSUPP;
  141. if (skb_cow_head(skb, MPLS_HLEN) < 0)
  142. return -ENOMEM;
  143. if (!skb->inner_protocol) {
  144. skb_set_inner_network_header(skb, skb->mac_len);
  145. skb_set_inner_protocol(skb, skb->protocol);
  146. }
  147. skb_push(skb, MPLS_HLEN);
  148. memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
  149. skb->mac_len);
  150. skb_reset_mac_header(skb);
  151. skb_set_network_header(skb, skb->mac_len);
  152. new_mpls_lse = mpls_hdr(skb);
  153. new_mpls_lse->label_stack_entry = mpls->mpls_lse;
  154. skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
  155. update_ethertype(skb, eth_hdr(skb), mpls->mpls_ethertype);
  156. skb->protocol = mpls->mpls_ethertype;
  157. invalidate_flow_key(key);
  158. return 0;
  159. }
  160. static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  161. const __be16 ethertype)
  162. {
  163. struct ethhdr *hdr;
  164. int err;
  165. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  166. if (unlikely(err))
  167. return err;
  168. skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
  169. memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
  170. skb->mac_len);
  171. __skb_pull(skb, MPLS_HLEN);
  172. skb_reset_mac_header(skb);
  173. skb_set_network_header(skb, skb->mac_len);
  174. /* mpls_hdr() is used to locate the ethertype field correctly in the
  175. * presence of VLAN tags.
  176. */
  177. hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
  178. update_ethertype(skb, hdr, ethertype);
  179. if (eth_p_mpls(skb->protocol))
  180. skb->protocol = ethertype;
  181. invalidate_flow_key(key);
  182. return 0;
  183. }
  184. static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
  185. const __be32 *mpls_lse, const __be32 *mask)
  186. {
  187. struct mpls_shim_hdr *stack;
  188. __be32 lse;
  189. int err;
  190. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  191. if (unlikely(err))
  192. return err;
  193. stack = mpls_hdr(skb);
  194. lse = OVS_MASKED(stack->label_stack_entry, *mpls_lse, *mask);
  195. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  196. __be32 diff[] = { ~(stack->label_stack_entry), lse };
  197. skb->csum = ~csum_partial((char *)diff, sizeof(diff),
  198. ~skb->csum);
  199. }
  200. stack->label_stack_entry = lse;
  201. flow_key->mpls.top_lse = lse;
  202. return 0;
  203. }
  204. static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  205. {
  206. int err;
  207. err = skb_vlan_pop(skb);
  208. if (skb_vlan_tag_present(skb)) {
  209. invalidate_flow_key(key);
  210. } else {
  211. key->eth.vlan.tci = 0;
  212. key->eth.vlan.tpid = 0;
  213. }
  214. return err;
  215. }
  216. static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
  217. const struct ovs_action_push_vlan *vlan)
  218. {
  219. if (skb_vlan_tag_present(skb)) {
  220. invalidate_flow_key(key);
  221. } else {
  222. key->eth.vlan.tci = vlan->vlan_tci;
  223. key->eth.vlan.tpid = vlan->vlan_tpid;
  224. }
  225. return skb_vlan_push(skb, vlan->vlan_tpid,
  226. ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
  227. }
  228. /* 'src' is already properly masked. */
  229. static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
  230. {
  231. u16 *dst = (u16 *)dst_;
  232. const u16 *src = (const u16 *)src_;
  233. const u16 *mask = (const u16 *)mask_;
  234. OVS_SET_MASKED(dst[0], src[0], mask[0]);
  235. OVS_SET_MASKED(dst[1], src[1], mask[1]);
  236. OVS_SET_MASKED(dst[2], src[2], mask[2]);
  237. }
  238. static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
  239. const struct ovs_key_ethernet *key,
  240. const struct ovs_key_ethernet *mask)
  241. {
  242. int err;
  243. err = skb_ensure_writable(skb, ETH_HLEN);
  244. if (unlikely(err))
  245. return err;
  246. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  247. ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
  248. mask->eth_src);
  249. ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
  250. mask->eth_dst);
  251. skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  252. ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
  253. ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
  254. return 0;
  255. }
  256. static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
  257. __be32 addr, __be32 new_addr)
  258. {
  259. int transport_len = skb->len - skb_transport_offset(skb);
  260. if (nh->frag_off & htons(IP_OFFSET))
  261. return;
  262. if (nh->protocol == IPPROTO_TCP) {
  263. if (likely(transport_len >= sizeof(struct tcphdr)))
  264. inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
  265. addr, new_addr, true);
  266. } else if (nh->protocol == IPPROTO_UDP) {
  267. if (likely(transport_len >= sizeof(struct udphdr))) {
  268. struct udphdr *uh = udp_hdr(skb);
  269. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  270. inet_proto_csum_replace4(&uh->check, skb,
  271. addr, new_addr, true);
  272. if (!uh->check)
  273. uh->check = CSUM_MANGLED_0;
  274. }
  275. }
  276. }
  277. }
  278. static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
  279. __be32 *addr, __be32 new_addr)
  280. {
  281. update_ip_l4_checksum(skb, nh, *addr, new_addr);
  282. csum_replace4(&nh->check, *addr, new_addr);
  283. skb_clear_hash(skb);
  284. *addr = new_addr;
  285. }
  286. static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
  287. __be32 addr[4], const __be32 new_addr[4])
  288. {
  289. int transport_len = skb->len - skb_transport_offset(skb);
  290. if (l4_proto == NEXTHDR_TCP) {
  291. if (likely(transport_len >= sizeof(struct tcphdr)))
  292. inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
  293. addr, new_addr, true);
  294. } else if (l4_proto == NEXTHDR_UDP) {
  295. if (likely(transport_len >= sizeof(struct udphdr))) {
  296. struct udphdr *uh = udp_hdr(skb);
  297. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  298. inet_proto_csum_replace16(&uh->check, skb,
  299. addr, new_addr, true);
  300. if (!uh->check)
  301. uh->check = CSUM_MANGLED_0;
  302. }
  303. }
  304. } else if (l4_proto == NEXTHDR_ICMP) {
  305. if (likely(transport_len >= sizeof(struct icmp6hdr)))
  306. inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
  307. skb, addr, new_addr, true);
  308. }
  309. }
  310. static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
  311. const __be32 mask[4], __be32 masked[4])
  312. {
  313. masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
  314. masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
  315. masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
  316. masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
  317. }
  318. static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
  319. __be32 addr[4], const __be32 new_addr[4],
  320. bool recalculate_csum)
  321. {
  322. if (recalculate_csum)
  323. update_ipv6_checksum(skb, l4_proto, addr, new_addr);
  324. skb_clear_hash(skb);
  325. memcpy(addr, new_addr, sizeof(__be32[4]));
  326. }
  327. static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
  328. {
  329. /* Bits 21-24 are always unmasked, so this retains their values. */
  330. OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
  331. OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
  332. OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
  333. }
  334. static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
  335. u8 mask)
  336. {
  337. new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
  338. csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
  339. nh->ttl = new_ttl;
  340. }
  341. static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
  342. const struct ovs_key_ipv4 *key,
  343. const struct ovs_key_ipv4 *mask)
  344. {
  345. struct iphdr *nh;
  346. __be32 new_addr;
  347. int err;
  348. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  349. sizeof(struct iphdr));
  350. if (unlikely(err))
  351. return err;
  352. nh = ip_hdr(skb);
  353. /* Setting an IP addresses is typically only a side effect of
  354. * matching on them in the current userspace implementation, so it
  355. * makes sense to check if the value actually changed.
  356. */
  357. if (mask->ipv4_src) {
  358. new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
  359. if (unlikely(new_addr != nh->saddr)) {
  360. set_ip_addr(skb, nh, &nh->saddr, new_addr);
  361. flow_key->ipv4.addr.src = new_addr;
  362. }
  363. }
  364. if (mask->ipv4_dst) {
  365. new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
  366. if (unlikely(new_addr != nh->daddr)) {
  367. set_ip_addr(skb, nh, &nh->daddr, new_addr);
  368. flow_key->ipv4.addr.dst = new_addr;
  369. }
  370. }
  371. if (mask->ipv4_tos) {
  372. ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
  373. flow_key->ip.tos = nh->tos;
  374. }
  375. if (mask->ipv4_ttl) {
  376. set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
  377. flow_key->ip.ttl = nh->ttl;
  378. }
  379. return 0;
  380. }
  381. static bool is_ipv6_mask_nonzero(const __be32 addr[4])
  382. {
  383. return !!(addr[0] | addr[1] | addr[2] | addr[3]);
  384. }
  385. static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
  386. const struct ovs_key_ipv6 *key,
  387. const struct ovs_key_ipv6 *mask)
  388. {
  389. struct ipv6hdr *nh;
  390. int err;
  391. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  392. sizeof(struct ipv6hdr));
  393. if (unlikely(err))
  394. return err;
  395. nh = ipv6_hdr(skb);
  396. /* Setting an IP addresses is typically only a side effect of
  397. * matching on them in the current userspace implementation, so it
  398. * makes sense to check if the value actually changed.
  399. */
  400. if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
  401. __be32 *saddr = (__be32 *)&nh->saddr;
  402. __be32 masked[4];
  403. mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
  404. if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
  405. set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
  406. true);
  407. memcpy(&flow_key->ipv6.addr.src, masked,
  408. sizeof(flow_key->ipv6.addr.src));
  409. }
  410. }
  411. if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
  412. unsigned int offset = 0;
  413. int flags = IP6_FH_F_SKIP_RH;
  414. bool recalc_csum = true;
  415. __be32 *daddr = (__be32 *)&nh->daddr;
  416. __be32 masked[4];
  417. mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
  418. if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
  419. if (ipv6_ext_hdr(nh->nexthdr))
  420. recalc_csum = (ipv6_find_hdr(skb, &offset,
  421. NEXTHDR_ROUTING,
  422. NULL, &flags)
  423. != NEXTHDR_ROUTING);
  424. set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
  425. recalc_csum);
  426. memcpy(&flow_key->ipv6.addr.dst, masked,
  427. sizeof(flow_key->ipv6.addr.dst));
  428. }
  429. }
  430. if (mask->ipv6_tclass) {
  431. ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
  432. flow_key->ip.tos = ipv6_get_dsfield(nh);
  433. }
  434. if (mask->ipv6_label) {
  435. set_ipv6_fl(nh, ntohl(key->ipv6_label),
  436. ntohl(mask->ipv6_label));
  437. flow_key->ipv6.label =
  438. *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  439. }
  440. if (mask->ipv6_hlimit) {
  441. OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
  442. mask->ipv6_hlimit);
  443. flow_key->ip.ttl = nh->hop_limit;
  444. }
  445. return 0;
  446. }
  447. /* Must follow skb_ensure_writable() since that can move the skb data. */
  448. static void set_tp_port(struct sk_buff *skb, __be16 *port,
  449. __be16 new_port, __sum16 *check)
  450. {
  451. inet_proto_csum_replace2(check, skb, *port, new_port, false);
  452. *port = new_port;
  453. }
  454. static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  455. const struct ovs_key_udp *key,
  456. const struct ovs_key_udp *mask)
  457. {
  458. struct udphdr *uh;
  459. __be16 src, dst;
  460. int err;
  461. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  462. sizeof(struct udphdr));
  463. if (unlikely(err))
  464. return err;
  465. uh = udp_hdr(skb);
  466. /* Either of the masks is non-zero, so do not bother checking them. */
  467. src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
  468. dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
  469. if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
  470. if (likely(src != uh->source)) {
  471. set_tp_port(skb, &uh->source, src, &uh->check);
  472. flow_key->tp.src = src;
  473. }
  474. if (likely(dst != uh->dest)) {
  475. set_tp_port(skb, &uh->dest, dst, &uh->check);
  476. flow_key->tp.dst = dst;
  477. }
  478. if (unlikely(!uh->check))
  479. uh->check = CSUM_MANGLED_0;
  480. } else {
  481. uh->source = src;
  482. uh->dest = dst;
  483. flow_key->tp.src = src;
  484. flow_key->tp.dst = dst;
  485. }
  486. skb_clear_hash(skb);
  487. return 0;
  488. }
  489. static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  490. const struct ovs_key_tcp *key,
  491. const struct ovs_key_tcp *mask)
  492. {
  493. struct tcphdr *th;
  494. __be16 src, dst;
  495. int err;
  496. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  497. sizeof(struct tcphdr));
  498. if (unlikely(err))
  499. return err;
  500. th = tcp_hdr(skb);
  501. src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
  502. if (likely(src != th->source)) {
  503. set_tp_port(skb, &th->source, src, &th->check);
  504. flow_key->tp.src = src;
  505. }
  506. dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
  507. if (likely(dst != th->dest)) {
  508. set_tp_port(skb, &th->dest, dst, &th->check);
  509. flow_key->tp.dst = dst;
  510. }
  511. skb_clear_hash(skb);
  512. return 0;
  513. }
  514. static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  515. const struct ovs_key_sctp *key,
  516. const struct ovs_key_sctp *mask)
  517. {
  518. unsigned int sctphoff = skb_transport_offset(skb);
  519. struct sctphdr *sh;
  520. __le32 old_correct_csum, new_csum, old_csum;
  521. int err;
  522. err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
  523. if (unlikely(err))
  524. return err;
  525. sh = sctp_hdr(skb);
  526. old_csum = sh->checksum;
  527. old_correct_csum = sctp_compute_cksum(skb, sctphoff);
  528. sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
  529. sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
  530. new_csum = sctp_compute_cksum(skb, sctphoff);
  531. /* Carry any checksum errors through. */
  532. sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
  533. skb_clear_hash(skb);
  534. flow_key->tp.src = sh->source;
  535. flow_key->tp.dst = sh->dest;
  536. return 0;
  537. }
  538. static int ovs_vport_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  539. {
  540. struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
  541. struct vport *vport = data->vport;
  542. if (skb_cow_head(skb, data->l2_len) < 0) {
  543. kfree_skb(skb);
  544. return -ENOMEM;
  545. }
  546. __skb_dst_copy(skb, data->dst);
  547. *OVS_CB(skb) = data->cb;
  548. skb->inner_protocol = data->inner_protocol;
  549. skb->vlan_tci = data->vlan_tci;
  550. skb->vlan_proto = data->vlan_proto;
  551. /* Reconstruct the MAC header. */
  552. skb_push(skb, data->l2_len);
  553. memcpy(skb->data, &data->l2_data, data->l2_len);
  554. skb_postpush_rcsum(skb, skb->data, data->l2_len);
  555. skb_reset_mac_header(skb);
  556. if (eth_p_mpls(skb->protocol)) {
  557. skb->inner_network_header = skb->network_header;
  558. skb_set_network_header(skb, data->network_offset);
  559. skb_reset_mac_len(skb);
  560. }
  561. ovs_vport_send(vport, skb);
  562. return 0;
  563. }
  564. static unsigned int
  565. ovs_dst_get_mtu(const struct dst_entry *dst)
  566. {
  567. return dst->dev->mtu;
  568. }
  569. static struct dst_ops ovs_dst_ops = {
  570. .family = AF_UNSPEC,
  571. .mtu = ovs_dst_get_mtu,
  572. };
  573. /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
  574. * ovs_vport_output(), which is called once per fragmented packet.
  575. */
  576. static void prepare_frag(struct vport *vport, struct sk_buff *skb,
  577. u16 orig_network_offset)
  578. {
  579. unsigned int hlen = skb_network_offset(skb);
  580. struct ovs_frag_data *data;
  581. data = this_cpu_ptr(&ovs_frag_data_storage);
  582. data->dst = skb->_skb_refdst;
  583. data->vport = vport;
  584. data->cb = *OVS_CB(skb);
  585. data->inner_protocol = skb->inner_protocol;
  586. data->network_offset = orig_network_offset;
  587. data->vlan_tci = skb->vlan_tci;
  588. data->vlan_proto = skb->vlan_proto;
  589. data->l2_len = hlen;
  590. memcpy(&data->l2_data, skb->data, hlen);
  591. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  592. skb_pull(skb, hlen);
  593. }
  594. static void ovs_fragment(struct net *net, struct vport *vport,
  595. struct sk_buff *skb, u16 mru, __be16 ethertype)
  596. {
  597. u16 orig_network_offset = 0;
  598. if (eth_p_mpls(skb->protocol)) {
  599. orig_network_offset = skb_network_offset(skb);
  600. skb->network_header = skb->inner_network_header;
  601. }
  602. if (skb_network_offset(skb) > MAX_L2_LEN) {
  603. OVS_NLERR(1, "L2 header too long to fragment");
  604. goto err;
  605. }
  606. if (ethertype == htons(ETH_P_IP)) {
  607. struct dst_entry ovs_dst;
  608. unsigned long orig_dst;
  609. prepare_frag(vport, skb, orig_network_offset);
  610. dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
  611. DST_OBSOLETE_NONE, DST_NOCOUNT);
  612. ovs_dst.dev = vport->dev;
  613. orig_dst = skb->_skb_refdst;
  614. skb_dst_set_noref(skb, &ovs_dst);
  615. IPCB(skb)->frag_max_size = mru;
  616. ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
  617. refdst_drop(orig_dst);
  618. } else if (ethertype == htons(ETH_P_IPV6)) {
  619. const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
  620. unsigned long orig_dst;
  621. struct rt6_info ovs_rt;
  622. if (!v6ops) {
  623. goto err;
  624. }
  625. prepare_frag(vport, skb, orig_network_offset);
  626. memset(&ovs_rt, 0, sizeof(ovs_rt));
  627. dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
  628. DST_OBSOLETE_NONE, DST_NOCOUNT);
  629. ovs_rt.dst.dev = vport->dev;
  630. orig_dst = skb->_skb_refdst;
  631. skb_dst_set_noref(skb, &ovs_rt.dst);
  632. IP6CB(skb)->frag_max_size = mru;
  633. v6ops->fragment(net, skb->sk, skb, ovs_vport_output);
  634. refdst_drop(orig_dst);
  635. } else {
  636. WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
  637. ovs_vport_name(vport), ntohs(ethertype), mru,
  638. vport->dev->mtu);
  639. goto err;
  640. }
  641. return;
  642. err:
  643. kfree_skb(skb);
  644. }
  645. static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
  646. struct sw_flow_key *key)
  647. {
  648. struct vport *vport = ovs_vport_rcu(dp, out_port);
  649. if (likely(vport)) {
  650. u16 mru = OVS_CB(skb)->mru;
  651. u32 cutlen = OVS_CB(skb)->cutlen;
  652. if (unlikely(cutlen > 0)) {
  653. if (skb->len - cutlen > ETH_HLEN)
  654. pskb_trim(skb, skb->len - cutlen);
  655. else
  656. pskb_trim(skb, ETH_HLEN);
  657. }
  658. if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
  659. ovs_vport_send(vport, skb);
  660. } else if (mru <= vport->dev->mtu) {
  661. struct net *net = read_pnet(&dp->net);
  662. __be16 ethertype = key->eth.type;
  663. if (!is_flow_key_valid(key)) {
  664. if (eth_p_mpls(skb->protocol))
  665. ethertype = skb->inner_protocol;
  666. else
  667. ethertype = vlan_get_protocol(skb);
  668. }
  669. ovs_fragment(net, vport, skb, mru, ethertype);
  670. } else {
  671. kfree_skb(skb);
  672. }
  673. } else {
  674. kfree_skb(skb);
  675. }
  676. }
  677. static int output_userspace(struct datapath *dp, struct sk_buff *skb,
  678. struct sw_flow_key *key, const struct nlattr *attr,
  679. const struct nlattr *actions, int actions_len,
  680. uint32_t cutlen)
  681. {
  682. struct dp_upcall_info upcall;
  683. const struct nlattr *a;
  684. int rem;
  685. memset(&upcall, 0, sizeof(upcall));
  686. upcall.cmd = OVS_PACKET_CMD_ACTION;
  687. upcall.mru = OVS_CB(skb)->mru;
  688. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  689. a = nla_next(a, &rem)) {
  690. switch (nla_type(a)) {
  691. case OVS_USERSPACE_ATTR_USERDATA:
  692. upcall.userdata = a;
  693. break;
  694. case OVS_USERSPACE_ATTR_PID:
  695. upcall.portid = nla_get_u32(a);
  696. break;
  697. case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
  698. /* Get out tunnel info. */
  699. struct vport *vport;
  700. vport = ovs_vport_rcu(dp, nla_get_u32(a));
  701. if (vport) {
  702. int err;
  703. err = dev_fill_metadata_dst(vport->dev, skb);
  704. if (!err)
  705. upcall.egress_tun_info = skb_tunnel_info(skb);
  706. }
  707. break;
  708. }
  709. case OVS_USERSPACE_ATTR_ACTIONS: {
  710. /* Include actions. */
  711. upcall.actions = actions;
  712. upcall.actions_len = actions_len;
  713. break;
  714. }
  715. } /* End of switch. */
  716. }
  717. return ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
  718. }
  719. static int sample(struct datapath *dp, struct sk_buff *skb,
  720. struct sw_flow_key *key, const struct nlattr *attr,
  721. const struct nlattr *actions, int actions_len)
  722. {
  723. const struct nlattr *acts_list = NULL;
  724. const struct nlattr *a;
  725. int rem;
  726. u32 cutlen = 0;
  727. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  728. a = nla_next(a, &rem)) {
  729. u32 probability;
  730. switch (nla_type(a)) {
  731. case OVS_SAMPLE_ATTR_PROBABILITY:
  732. probability = nla_get_u32(a);
  733. if (!probability || prandom_u32() > probability)
  734. return 0;
  735. break;
  736. case OVS_SAMPLE_ATTR_ACTIONS:
  737. acts_list = a;
  738. break;
  739. }
  740. }
  741. rem = nla_len(acts_list);
  742. a = nla_data(acts_list);
  743. /* Actions list is empty, do nothing */
  744. if (unlikely(!rem))
  745. return 0;
  746. /* The only known usage of sample action is having a single user-space
  747. * action, or having a truncate action followed by a single user-space
  748. * action. Treat this usage as a special case.
  749. * The output_userspace() should clone the skb to be sent to the
  750. * user space. This skb will be consumed by its caller.
  751. */
  752. if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
  753. struct ovs_action_trunc *trunc = nla_data(a);
  754. if (skb->len > trunc->max_len)
  755. cutlen = skb->len - trunc->max_len;
  756. a = nla_next(a, &rem);
  757. }
  758. if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
  759. nla_is_last(a, rem)))
  760. return output_userspace(dp, skb, key, a, actions,
  761. actions_len, cutlen);
  762. skb = skb_clone(skb, GFP_ATOMIC);
  763. if (!skb)
  764. /* Skip the sample action when out of memory. */
  765. return 0;
  766. if (!add_deferred_actions(skb, key, a)) {
  767. if (net_ratelimit())
  768. pr_warn("%s: deferred actions limit reached, dropping sample action\n",
  769. ovs_dp_name(dp));
  770. kfree_skb(skb);
  771. }
  772. return 0;
  773. }
  774. static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
  775. const struct nlattr *attr)
  776. {
  777. struct ovs_action_hash *hash_act = nla_data(attr);
  778. u32 hash = 0;
  779. /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
  780. hash = skb_get_hash(skb);
  781. hash = jhash_1word(hash, hash_act->hash_basis);
  782. if (!hash)
  783. hash = 0x1;
  784. key->ovs_flow_hash = hash;
  785. }
  786. static int execute_set_action(struct sk_buff *skb,
  787. struct sw_flow_key *flow_key,
  788. const struct nlattr *a)
  789. {
  790. /* Only tunnel set execution is supported without a mask. */
  791. if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
  792. struct ovs_tunnel_info *tun = nla_data(a);
  793. skb_dst_drop(skb);
  794. dst_hold((struct dst_entry *)tun->tun_dst);
  795. skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
  796. return 0;
  797. }
  798. return -EINVAL;
  799. }
  800. /* Mask is at the midpoint of the data. */
  801. #define get_mask(a, type) ((const type)nla_data(a) + 1)
  802. static int execute_masked_set_action(struct sk_buff *skb,
  803. struct sw_flow_key *flow_key,
  804. const struct nlattr *a)
  805. {
  806. int err = 0;
  807. switch (nla_type(a)) {
  808. case OVS_KEY_ATTR_PRIORITY:
  809. OVS_SET_MASKED(skb->priority, nla_get_u32(a),
  810. *get_mask(a, u32 *));
  811. flow_key->phy.priority = skb->priority;
  812. break;
  813. case OVS_KEY_ATTR_SKB_MARK:
  814. OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
  815. flow_key->phy.skb_mark = skb->mark;
  816. break;
  817. case OVS_KEY_ATTR_TUNNEL_INFO:
  818. /* Masked data not supported for tunnel. */
  819. err = -EINVAL;
  820. break;
  821. case OVS_KEY_ATTR_ETHERNET:
  822. err = set_eth_addr(skb, flow_key, nla_data(a),
  823. get_mask(a, struct ovs_key_ethernet *));
  824. break;
  825. case OVS_KEY_ATTR_IPV4:
  826. err = set_ipv4(skb, flow_key, nla_data(a),
  827. get_mask(a, struct ovs_key_ipv4 *));
  828. break;
  829. case OVS_KEY_ATTR_IPV6:
  830. err = set_ipv6(skb, flow_key, nla_data(a),
  831. get_mask(a, struct ovs_key_ipv6 *));
  832. break;
  833. case OVS_KEY_ATTR_TCP:
  834. err = set_tcp(skb, flow_key, nla_data(a),
  835. get_mask(a, struct ovs_key_tcp *));
  836. break;
  837. case OVS_KEY_ATTR_UDP:
  838. err = set_udp(skb, flow_key, nla_data(a),
  839. get_mask(a, struct ovs_key_udp *));
  840. break;
  841. case OVS_KEY_ATTR_SCTP:
  842. err = set_sctp(skb, flow_key, nla_data(a),
  843. get_mask(a, struct ovs_key_sctp *));
  844. break;
  845. case OVS_KEY_ATTR_MPLS:
  846. err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
  847. __be32 *));
  848. break;
  849. case OVS_KEY_ATTR_CT_STATE:
  850. case OVS_KEY_ATTR_CT_ZONE:
  851. case OVS_KEY_ATTR_CT_MARK:
  852. case OVS_KEY_ATTR_CT_LABELS:
  853. err = -EINVAL;
  854. break;
  855. }
  856. return err;
  857. }
  858. static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
  859. struct sw_flow_key *key,
  860. const struct nlattr *a, int rem)
  861. {
  862. struct deferred_action *da;
  863. int level;
  864. if (!is_flow_key_valid(key)) {
  865. int err;
  866. err = ovs_flow_key_update(skb, key);
  867. if (err)
  868. return err;
  869. }
  870. BUG_ON(!is_flow_key_valid(key));
  871. if (!nla_is_last(a, rem)) {
  872. /* Recirc action is the not the last action
  873. * of the action list, need to clone the skb.
  874. */
  875. skb = skb_clone(skb, GFP_ATOMIC);
  876. /* Skip the recirc action when out of memory, but
  877. * continue on with the rest of the action list.
  878. */
  879. if (!skb)
  880. return 0;
  881. }
  882. level = this_cpu_read(exec_actions_level);
  883. if (level <= OVS_DEFERRED_ACTION_THRESHOLD) {
  884. struct recirc_keys *rks = this_cpu_ptr(recirc_keys);
  885. struct sw_flow_key *recirc_key = &rks->key[level - 1];
  886. *recirc_key = *key;
  887. recirc_key->recirc_id = nla_get_u32(a);
  888. ovs_dp_process_packet(skb, recirc_key);
  889. return 0;
  890. }
  891. da = add_deferred_actions(skb, key, NULL);
  892. if (da) {
  893. da->pkt_key.recirc_id = nla_get_u32(a);
  894. } else {
  895. kfree_skb(skb);
  896. if (net_ratelimit())
  897. pr_warn("%s: deferred action limit reached, drop recirc action\n",
  898. ovs_dp_name(dp));
  899. }
  900. return 0;
  901. }
  902. /* Execute a list of actions against 'skb'. */
  903. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  904. struct sw_flow_key *key,
  905. const struct nlattr *attr, int len)
  906. {
  907. /* Every output action needs a separate clone of 'skb', but the common
  908. * case is just a single output action, so that doing a clone and
  909. * then freeing the original skbuff is wasteful. So the following code
  910. * is slightly obscure just to avoid that.
  911. */
  912. int prev_port = -1;
  913. const struct nlattr *a;
  914. int rem;
  915. for (a = attr, rem = len; rem > 0;
  916. a = nla_next(a, &rem)) {
  917. int err = 0;
  918. if (unlikely(prev_port != -1)) {
  919. struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
  920. if (out_skb)
  921. do_output(dp, out_skb, prev_port, key);
  922. OVS_CB(skb)->cutlen = 0;
  923. prev_port = -1;
  924. }
  925. switch (nla_type(a)) {
  926. case OVS_ACTION_ATTR_OUTPUT:
  927. prev_port = nla_get_u32(a);
  928. break;
  929. case OVS_ACTION_ATTR_TRUNC: {
  930. struct ovs_action_trunc *trunc = nla_data(a);
  931. if (skb->len > trunc->max_len)
  932. OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
  933. break;
  934. }
  935. case OVS_ACTION_ATTR_USERSPACE:
  936. output_userspace(dp, skb, key, a, attr,
  937. len, OVS_CB(skb)->cutlen);
  938. OVS_CB(skb)->cutlen = 0;
  939. break;
  940. case OVS_ACTION_ATTR_HASH:
  941. execute_hash(skb, key, a);
  942. break;
  943. case OVS_ACTION_ATTR_PUSH_MPLS:
  944. err = push_mpls(skb, key, nla_data(a));
  945. break;
  946. case OVS_ACTION_ATTR_POP_MPLS:
  947. err = pop_mpls(skb, key, nla_get_be16(a));
  948. break;
  949. case OVS_ACTION_ATTR_PUSH_VLAN:
  950. err = push_vlan(skb, key, nla_data(a));
  951. break;
  952. case OVS_ACTION_ATTR_POP_VLAN:
  953. err = pop_vlan(skb, key);
  954. break;
  955. case OVS_ACTION_ATTR_RECIRC:
  956. err = execute_recirc(dp, skb, key, a, rem);
  957. if (nla_is_last(a, rem)) {
  958. /* If this is the last action, the skb has
  959. * been consumed or freed.
  960. * Return immediately.
  961. */
  962. return err;
  963. }
  964. break;
  965. case OVS_ACTION_ATTR_SET:
  966. err = execute_set_action(skb, key, nla_data(a));
  967. break;
  968. case OVS_ACTION_ATTR_SET_MASKED:
  969. case OVS_ACTION_ATTR_SET_TO_MASKED:
  970. err = execute_masked_set_action(skb, key, nla_data(a));
  971. break;
  972. case OVS_ACTION_ATTR_SAMPLE:
  973. err = sample(dp, skb, key, a, attr, len);
  974. break;
  975. case OVS_ACTION_ATTR_CT:
  976. if (!is_flow_key_valid(key)) {
  977. err = ovs_flow_key_update(skb, key);
  978. if (err)
  979. return err;
  980. }
  981. err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
  982. nla_data(a));
  983. /* Hide stolen IP fragments from user space. */
  984. if (err)
  985. return err == -EINPROGRESS ? 0 : err;
  986. break;
  987. }
  988. if (unlikely(err)) {
  989. kfree_skb(skb);
  990. return err;
  991. }
  992. }
  993. if (prev_port != -1)
  994. do_output(dp, skb, prev_port, key);
  995. else
  996. consume_skb(skb);
  997. return 0;
  998. }
  999. static void process_deferred_actions(struct datapath *dp)
  1000. {
  1001. struct action_fifo *fifo = this_cpu_ptr(action_fifos);
  1002. /* Do not touch the FIFO in case there is no deferred actions. */
  1003. if (action_fifo_is_empty(fifo))
  1004. return;
  1005. /* Finishing executing all deferred actions. */
  1006. do {
  1007. struct deferred_action *da = action_fifo_get(fifo);
  1008. struct sk_buff *skb = da->skb;
  1009. struct sw_flow_key *key = &da->pkt_key;
  1010. const struct nlattr *actions = da->actions;
  1011. if (actions)
  1012. do_execute_actions(dp, skb, key, actions,
  1013. nla_len(actions));
  1014. else
  1015. ovs_dp_process_packet(skb, key);
  1016. } while (!action_fifo_is_empty(fifo));
  1017. /* Reset FIFO for the next packet. */
  1018. action_fifo_init(fifo);
  1019. }
  1020. /* Execute a list of actions against 'skb'. */
  1021. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  1022. const struct sw_flow_actions *acts,
  1023. struct sw_flow_key *key)
  1024. {
  1025. int err, level;
  1026. level = __this_cpu_inc_return(exec_actions_level);
  1027. if (unlikely(level > OVS_RECURSION_LIMIT)) {
  1028. net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
  1029. ovs_dp_name(dp));
  1030. kfree_skb(skb);
  1031. err = -ENETDOWN;
  1032. goto out;
  1033. }
  1034. err = do_execute_actions(dp, skb, key,
  1035. acts->actions, acts->actions_len);
  1036. if (level == 1)
  1037. process_deferred_actions(dp);
  1038. out:
  1039. __this_cpu_dec(exec_actions_level);
  1040. return err;
  1041. }
  1042. int action_fifos_init(void)
  1043. {
  1044. action_fifos = alloc_percpu(struct action_fifo);
  1045. if (!action_fifos)
  1046. return -ENOMEM;
  1047. recirc_keys = alloc_percpu(struct recirc_keys);
  1048. if (!recirc_keys) {
  1049. free_percpu(action_fifos);
  1050. return -ENOMEM;
  1051. }
  1052. return 0;
  1053. }
  1054. void action_fifos_exit(void)
  1055. {
  1056. free_percpu(action_fifos);
  1057. free_percpu(recirc_keys);
  1058. }