actions.c 33 KB

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