actions.c 34 KB

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