actions.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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/sctp.h>
  24. #include <linux/tcp.h>
  25. #include <linux/udp.h>
  26. #include <linux/in6.h>
  27. #include <linux/if_arp.h>
  28. #include <linux/if_vlan.h>
  29. #include <net/ip.h>
  30. #include <net/ipv6.h>
  31. #include <net/checksum.h>
  32. #include <net/dsfield.h>
  33. #include <net/mpls.h>
  34. #include <net/sctp/checksum.h>
  35. #include "datapath.h"
  36. #include "flow.h"
  37. #include "vport.h"
  38. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  39. struct sw_flow_key *key,
  40. const struct nlattr *attr, int len);
  41. struct deferred_action {
  42. struct sk_buff *skb;
  43. const struct nlattr *actions;
  44. /* Store pkt_key clone when creating deferred action. */
  45. struct sw_flow_key pkt_key;
  46. };
  47. #define DEFERRED_ACTION_FIFO_SIZE 10
  48. struct action_fifo {
  49. int head;
  50. int tail;
  51. /* Deferred action fifo queue storage. */
  52. struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
  53. };
  54. static struct action_fifo __percpu *action_fifos;
  55. static DEFINE_PER_CPU(int, exec_actions_level);
  56. static void action_fifo_init(struct action_fifo *fifo)
  57. {
  58. fifo->head = 0;
  59. fifo->tail = 0;
  60. }
  61. static bool action_fifo_is_empty(const struct action_fifo *fifo)
  62. {
  63. return (fifo->head == fifo->tail);
  64. }
  65. static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
  66. {
  67. if (action_fifo_is_empty(fifo))
  68. return NULL;
  69. return &fifo->fifo[fifo->tail++];
  70. }
  71. static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
  72. {
  73. if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
  74. return NULL;
  75. return &fifo->fifo[fifo->head++];
  76. }
  77. /* Return true if fifo is not full */
  78. static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
  79. const struct sw_flow_key *key,
  80. const struct nlattr *attr)
  81. {
  82. struct action_fifo *fifo;
  83. struct deferred_action *da;
  84. fifo = this_cpu_ptr(action_fifos);
  85. da = action_fifo_put(fifo);
  86. if (da) {
  87. da->skb = skb;
  88. da->actions = attr;
  89. da->pkt_key = *key;
  90. }
  91. return da;
  92. }
  93. static void invalidate_flow_key(struct sw_flow_key *key)
  94. {
  95. key->eth.type = htons(0);
  96. }
  97. static bool is_flow_key_valid(const struct sw_flow_key *key)
  98. {
  99. return !!key->eth.type;
  100. }
  101. static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  102. const struct ovs_action_push_mpls *mpls)
  103. {
  104. __be32 *new_mpls_lse;
  105. struct ethhdr *hdr;
  106. /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
  107. if (skb->encapsulation)
  108. return -ENOTSUPP;
  109. if (skb_cow_head(skb, MPLS_HLEN) < 0)
  110. return -ENOMEM;
  111. skb_push(skb, MPLS_HLEN);
  112. memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
  113. skb->mac_len);
  114. skb_reset_mac_header(skb);
  115. new_mpls_lse = (__be32 *)skb_mpls_header(skb);
  116. *new_mpls_lse = mpls->mpls_lse;
  117. if (skb->ip_summed == CHECKSUM_COMPLETE)
  118. skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
  119. MPLS_HLEN, 0));
  120. hdr = eth_hdr(skb);
  121. hdr->h_proto = mpls->mpls_ethertype;
  122. if (!skb->inner_protocol)
  123. skb_set_inner_protocol(skb, skb->protocol);
  124. skb->protocol = mpls->mpls_ethertype;
  125. invalidate_flow_key(key);
  126. return 0;
  127. }
  128. static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  129. const __be16 ethertype)
  130. {
  131. struct ethhdr *hdr;
  132. int err;
  133. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  134. if (unlikely(err))
  135. return err;
  136. skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
  137. memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
  138. skb->mac_len);
  139. __skb_pull(skb, MPLS_HLEN);
  140. skb_reset_mac_header(skb);
  141. /* skb_mpls_header() is used to locate the ethertype
  142. * field correctly in the presence of VLAN tags.
  143. */
  144. hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
  145. hdr->h_proto = ethertype;
  146. if (eth_p_mpls(skb->protocol))
  147. skb->protocol = ethertype;
  148. invalidate_flow_key(key);
  149. return 0;
  150. }
  151. /* 'KEY' must not have any bits set outside of the 'MASK' */
  152. #define MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
  153. #define SET_MASKED(OLD, KEY, MASK) ((OLD) = MASKED(OLD, KEY, MASK))
  154. static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
  155. const __be32 *mpls_lse, const __be32 *mask)
  156. {
  157. __be32 *stack;
  158. __be32 lse;
  159. int err;
  160. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  161. if (unlikely(err))
  162. return err;
  163. stack = (__be32 *)skb_mpls_header(skb);
  164. lse = MASKED(*stack, *mpls_lse, *mask);
  165. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  166. __be32 diff[] = { ~(*stack), lse };
  167. skb->csum = ~csum_partial((char *)diff, sizeof(diff),
  168. ~skb->csum);
  169. }
  170. *stack = lse;
  171. flow_key->mpls.top_lse = lse;
  172. return 0;
  173. }
  174. static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  175. {
  176. int err;
  177. err = skb_vlan_pop(skb);
  178. if (skb_vlan_tag_present(skb))
  179. invalidate_flow_key(key);
  180. else
  181. key->eth.tci = 0;
  182. return err;
  183. }
  184. static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
  185. const struct ovs_action_push_vlan *vlan)
  186. {
  187. if (skb_vlan_tag_present(skb))
  188. invalidate_flow_key(key);
  189. else
  190. key->eth.tci = vlan->vlan_tci;
  191. return skb_vlan_push(skb, vlan->vlan_tpid,
  192. ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
  193. }
  194. /* 'src' is already properly masked. */
  195. static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
  196. {
  197. u16 *dst = (u16 *)dst_;
  198. const u16 *src = (const u16 *)src_;
  199. const u16 *mask = (const u16 *)mask_;
  200. SET_MASKED(dst[0], src[0], mask[0]);
  201. SET_MASKED(dst[1], src[1], mask[1]);
  202. SET_MASKED(dst[2], src[2], mask[2]);
  203. }
  204. static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
  205. const struct ovs_key_ethernet *key,
  206. const struct ovs_key_ethernet *mask)
  207. {
  208. int err;
  209. err = skb_ensure_writable(skb, ETH_HLEN);
  210. if (unlikely(err))
  211. return err;
  212. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  213. ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
  214. mask->eth_src);
  215. ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
  216. mask->eth_dst);
  217. ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  218. ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
  219. ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
  220. return 0;
  221. }
  222. static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
  223. __be32 *addr, __be32 new_addr)
  224. {
  225. int transport_len = skb->len - skb_transport_offset(skb);
  226. if (nh->protocol == IPPROTO_TCP) {
  227. if (likely(transport_len >= sizeof(struct tcphdr)))
  228. inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
  229. *addr, new_addr, 1);
  230. } else if (nh->protocol == IPPROTO_UDP) {
  231. if (likely(transport_len >= sizeof(struct udphdr))) {
  232. struct udphdr *uh = udp_hdr(skb);
  233. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  234. inet_proto_csum_replace4(&uh->check, skb,
  235. *addr, new_addr, 1);
  236. if (!uh->check)
  237. uh->check = CSUM_MANGLED_0;
  238. }
  239. }
  240. }
  241. csum_replace4(&nh->check, *addr, new_addr);
  242. skb_clear_hash(skb);
  243. *addr = new_addr;
  244. }
  245. static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
  246. __be32 addr[4], const __be32 new_addr[4])
  247. {
  248. int transport_len = skb->len - skb_transport_offset(skb);
  249. if (l4_proto == NEXTHDR_TCP) {
  250. if (likely(transport_len >= sizeof(struct tcphdr)))
  251. inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
  252. addr, new_addr, 1);
  253. } else if (l4_proto == NEXTHDR_UDP) {
  254. if (likely(transport_len >= sizeof(struct udphdr))) {
  255. struct udphdr *uh = udp_hdr(skb);
  256. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  257. inet_proto_csum_replace16(&uh->check, skb,
  258. addr, new_addr, 1);
  259. if (!uh->check)
  260. uh->check = CSUM_MANGLED_0;
  261. }
  262. }
  263. } else if (l4_proto == NEXTHDR_ICMP) {
  264. if (likely(transport_len >= sizeof(struct icmp6hdr)))
  265. inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
  266. skb, addr, new_addr, 1);
  267. }
  268. }
  269. static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
  270. const __be32 mask[4], __be32 masked[4])
  271. {
  272. masked[0] = MASKED(old[0], addr[0], mask[0]);
  273. masked[1] = MASKED(old[1], addr[1], mask[1]);
  274. masked[2] = MASKED(old[2], addr[2], mask[2]);
  275. masked[3] = MASKED(old[3], addr[3], mask[3]);
  276. }
  277. static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
  278. __be32 addr[4], const __be32 new_addr[4],
  279. bool recalculate_csum)
  280. {
  281. if (recalculate_csum)
  282. update_ipv6_checksum(skb, l4_proto, addr, new_addr);
  283. skb_clear_hash(skb);
  284. memcpy(addr, new_addr, sizeof(__be32[4]));
  285. }
  286. static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
  287. {
  288. /* Bits 21-24 are always unmasked, so this retains their values. */
  289. SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
  290. SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
  291. SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
  292. }
  293. static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
  294. u8 mask)
  295. {
  296. new_ttl = MASKED(nh->ttl, new_ttl, mask);
  297. csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
  298. nh->ttl = new_ttl;
  299. }
  300. static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
  301. const struct ovs_key_ipv4 *key,
  302. const struct ovs_key_ipv4 *mask)
  303. {
  304. struct iphdr *nh;
  305. __be32 new_addr;
  306. int err;
  307. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  308. sizeof(struct iphdr));
  309. if (unlikely(err))
  310. return err;
  311. nh = ip_hdr(skb);
  312. /* Setting an IP addresses is typically only a side effect of
  313. * matching on them in the current userspace implementation, so it
  314. * makes sense to check if the value actually changed.
  315. */
  316. if (mask->ipv4_src) {
  317. new_addr = MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
  318. if (unlikely(new_addr != nh->saddr)) {
  319. set_ip_addr(skb, nh, &nh->saddr, new_addr);
  320. flow_key->ipv4.addr.src = new_addr;
  321. }
  322. }
  323. if (mask->ipv4_dst) {
  324. new_addr = MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
  325. if (unlikely(new_addr != nh->daddr)) {
  326. set_ip_addr(skb, nh, &nh->daddr, new_addr);
  327. flow_key->ipv4.addr.dst = new_addr;
  328. }
  329. }
  330. if (mask->ipv4_tos) {
  331. ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
  332. flow_key->ip.tos = nh->tos;
  333. }
  334. if (mask->ipv4_ttl) {
  335. set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
  336. flow_key->ip.ttl = nh->ttl;
  337. }
  338. return 0;
  339. }
  340. static bool is_ipv6_mask_nonzero(const __be32 addr[4])
  341. {
  342. return !!(addr[0] | addr[1] | addr[2] | addr[3]);
  343. }
  344. static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
  345. const struct ovs_key_ipv6 *key,
  346. const struct ovs_key_ipv6 *mask)
  347. {
  348. struct ipv6hdr *nh;
  349. int err;
  350. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  351. sizeof(struct ipv6hdr));
  352. if (unlikely(err))
  353. return err;
  354. nh = ipv6_hdr(skb);
  355. /* Setting an IP addresses is typically only a side effect of
  356. * matching on them in the current userspace implementation, so it
  357. * makes sense to check if the value actually changed.
  358. */
  359. if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
  360. __be32 *saddr = (__be32 *)&nh->saddr;
  361. __be32 masked[4];
  362. mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
  363. if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
  364. set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
  365. true);
  366. memcpy(&flow_key->ipv6.addr.src, masked,
  367. sizeof(flow_key->ipv6.addr.src));
  368. }
  369. }
  370. if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
  371. unsigned int offset = 0;
  372. int flags = IP6_FH_F_SKIP_RH;
  373. bool recalc_csum = true;
  374. __be32 *daddr = (__be32 *)&nh->daddr;
  375. __be32 masked[4];
  376. mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
  377. if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
  378. if (ipv6_ext_hdr(nh->nexthdr))
  379. recalc_csum = (ipv6_find_hdr(skb, &offset,
  380. NEXTHDR_ROUTING,
  381. NULL, &flags)
  382. != NEXTHDR_ROUTING);
  383. set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
  384. recalc_csum);
  385. memcpy(&flow_key->ipv6.addr.dst, masked,
  386. sizeof(flow_key->ipv6.addr.dst));
  387. }
  388. }
  389. if (mask->ipv6_tclass) {
  390. ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
  391. flow_key->ip.tos = ipv6_get_dsfield(nh);
  392. }
  393. if (mask->ipv6_label) {
  394. set_ipv6_fl(nh, ntohl(key->ipv6_label),
  395. ntohl(mask->ipv6_label));
  396. flow_key->ipv6.label =
  397. *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  398. }
  399. if (mask->ipv6_hlimit) {
  400. SET_MASKED(nh->hop_limit, key->ipv6_hlimit, mask->ipv6_hlimit);
  401. flow_key->ip.ttl = nh->hop_limit;
  402. }
  403. return 0;
  404. }
  405. /* Must follow skb_ensure_writable() since that can move the skb data. */
  406. static void set_tp_port(struct sk_buff *skb, __be16 *port,
  407. __be16 new_port, __sum16 *check)
  408. {
  409. inet_proto_csum_replace2(check, skb, *port, new_port, 0);
  410. *port = new_port;
  411. }
  412. static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  413. const struct ovs_key_udp *key,
  414. const struct ovs_key_udp *mask)
  415. {
  416. struct udphdr *uh;
  417. __be16 src, dst;
  418. int err;
  419. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  420. sizeof(struct udphdr));
  421. if (unlikely(err))
  422. return err;
  423. uh = udp_hdr(skb);
  424. /* Either of the masks is non-zero, so do not bother checking them. */
  425. src = MASKED(uh->source, key->udp_src, mask->udp_src);
  426. dst = MASKED(uh->dest, key->udp_dst, mask->udp_dst);
  427. if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
  428. if (likely(src != uh->source)) {
  429. set_tp_port(skb, &uh->source, src, &uh->check);
  430. flow_key->tp.src = src;
  431. }
  432. if (likely(dst != uh->dest)) {
  433. set_tp_port(skb, &uh->dest, dst, &uh->check);
  434. flow_key->tp.dst = dst;
  435. }
  436. if (unlikely(!uh->check))
  437. uh->check = CSUM_MANGLED_0;
  438. } else {
  439. uh->source = src;
  440. uh->dest = dst;
  441. flow_key->tp.src = src;
  442. flow_key->tp.dst = dst;
  443. }
  444. skb_clear_hash(skb);
  445. return 0;
  446. }
  447. static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  448. const struct ovs_key_tcp *key,
  449. const struct ovs_key_tcp *mask)
  450. {
  451. struct tcphdr *th;
  452. __be16 src, dst;
  453. int err;
  454. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  455. sizeof(struct tcphdr));
  456. if (unlikely(err))
  457. return err;
  458. th = tcp_hdr(skb);
  459. src = MASKED(th->source, key->tcp_src, mask->tcp_src);
  460. if (likely(src != th->source)) {
  461. set_tp_port(skb, &th->source, src, &th->check);
  462. flow_key->tp.src = src;
  463. }
  464. dst = MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
  465. if (likely(dst != th->dest)) {
  466. set_tp_port(skb, &th->dest, dst, &th->check);
  467. flow_key->tp.dst = dst;
  468. }
  469. skb_clear_hash(skb);
  470. return 0;
  471. }
  472. static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  473. const struct ovs_key_sctp *key,
  474. const struct ovs_key_sctp *mask)
  475. {
  476. unsigned int sctphoff = skb_transport_offset(skb);
  477. struct sctphdr *sh;
  478. __le32 old_correct_csum, new_csum, old_csum;
  479. int err;
  480. err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
  481. if (unlikely(err))
  482. return err;
  483. sh = sctp_hdr(skb);
  484. old_csum = sh->checksum;
  485. old_correct_csum = sctp_compute_cksum(skb, sctphoff);
  486. sh->source = MASKED(sh->source, key->sctp_src, mask->sctp_src);
  487. sh->dest = MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
  488. new_csum = sctp_compute_cksum(skb, sctphoff);
  489. /* Carry any checksum errors through. */
  490. sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
  491. skb_clear_hash(skb);
  492. flow_key->tp.src = sh->source;
  493. flow_key->tp.dst = sh->dest;
  494. return 0;
  495. }
  496. static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
  497. {
  498. struct vport *vport = ovs_vport_rcu(dp, out_port);
  499. if (likely(vport))
  500. ovs_vport_send(vport, skb);
  501. else
  502. kfree_skb(skb);
  503. }
  504. static int output_userspace(struct datapath *dp, struct sk_buff *skb,
  505. struct sw_flow_key *key, const struct nlattr *attr,
  506. const struct nlattr *actions, int actions_len)
  507. {
  508. struct ip_tunnel_info info;
  509. struct dp_upcall_info upcall;
  510. const struct nlattr *a;
  511. int rem;
  512. memset(&upcall, 0, sizeof(upcall));
  513. upcall.cmd = OVS_PACKET_CMD_ACTION;
  514. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  515. a = nla_next(a, &rem)) {
  516. switch (nla_type(a)) {
  517. case OVS_USERSPACE_ATTR_USERDATA:
  518. upcall.userdata = a;
  519. break;
  520. case OVS_USERSPACE_ATTR_PID:
  521. upcall.portid = nla_get_u32(a);
  522. break;
  523. case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
  524. /* Get out tunnel info. */
  525. struct vport *vport;
  526. vport = ovs_vport_rcu(dp, nla_get_u32(a));
  527. if (vport) {
  528. int err;
  529. err = ovs_vport_get_egress_tun_info(vport, skb,
  530. &info);
  531. if (!err)
  532. upcall.egress_tun_info = &info;
  533. }
  534. break;
  535. }
  536. case OVS_USERSPACE_ATTR_ACTIONS: {
  537. /* Include actions. */
  538. upcall.actions = actions;
  539. upcall.actions_len = actions_len;
  540. break;
  541. }
  542. } /* End of switch. */
  543. }
  544. return ovs_dp_upcall(dp, skb, key, &upcall);
  545. }
  546. static int sample(struct datapath *dp, struct sk_buff *skb,
  547. struct sw_flow_key *key, const struct nlattr *attr,
  548. const struct nlattr *actions, int actions_len)
  549. {
  550. const struct nlattr *acts_list = NULL;
  551. const struct nlattr *a;
  552. int rem;
  553. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  554. a = nla_next(a, &rem)) {
  555. switch (nla_type(a)) {
  556. case OVS_SAMPLE_ATTR_PROBABILITY:
  557. if (prandom_u32() >= nla_get_u32(a))
  558. return 0;
  559. break;
  560. case OVS_SAMPLE_ATTR_ACTIONS:
  561. acts_list = a;
  562. break;
  563. }
  564. }
  565. rem = nla_len(acts_list);
  566. a = nla_data(acts_list);
  567. /* Actions list is empty, do nothing */
  568. if (unlikely(!rem))
  569. return 0;
  570. /* The only known usage of sample action is having a single user-space
  571. * action. Treat this usage as a special case.
  572. * The output_userspace() should clone the skb to be sent to the
  573. * user space. This skb will be consumed by its caller.
  574. */
  575. if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
  576. nla_is_last(a, rem)))
  577. return output_userspace(dp, skb, key, a, actions, actions_len);
  578. skb = skb_clone(skb, GFP_ATOMIC);
  579. if (!skb)
  580. /* Skip the sample action when out of memory. */
  581. return 0;
  582. if (!add_deferred_actions(skb, key, a)) {
  583. if (net_ratelimit())
  584. pr_warn("%s: deferred actions limit reached, dropping sample action\n",
  585. ovs_dp_name(dp));
  586. kfree_skb(skb);
  587. }
  588. return 0;
  589. }
  590. static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
  591. const struct nlattr *attr)
  592. {
  593. struct ovs_action_hash *hash_act = nla_data(attr);
  594. u32 hash = 0;
  595. /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
  596. hash = skb_get_hash(skb);
  597. hash = jhash_1word(hash, hash_act->hash_basis);
  598. if (!hash)
  599. hash = 0x1;
  600. key->ovs_flow_hash = hash;
  601. }
  602. static int execute_set_action(struct sk_buff *skb,
  603. struct sw_flow_key *flow_key,
  604. const struct nlattr *a)
  605. {
  606. /* Only tunnel set execution is supported without a mask. */
  607. if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
  608. struct ovs_tunnel_info *tun = nla_data(a);
  609. skb_dst_drop(skb);
  610. dst_hold((struct dst_entry *)tun->tun_dst);
  611. skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
  612. /* FIXME: Remove when all vports have been converted */
  613. OVS_CB(skb)->egress_tun_info = &tun->tun_dst->u.tun_info;
  614. return 0;
  615. }
  616. return -EINVAL;
  617. }
  618. /* Mask is at the midpoint of the data. */
  619. #define get_mask(a, type) ((const type)nla_data(a) + 1)
  620. static int execute_masked_set_action(struct sk_buff *skb,
  621. struct sw_flow_key *flow_key,
  622. const struct nlattr *a)
  623. {
  624. int err = 0;
  625. switch (nla_type(a)) {
  626. case OVS_KEY_ATTR_PRIORITY:
  627. SET_MASKED(skb->priority, nla_get_u32(a), *get_mask(a, u32 *));
  628. flow_key->phy.priority = skb->priority;
  629. break;
  630. case OVS_KEY_ATTR_SKB_MARK:
  631. SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
  632. flow_key->phy.skb_mark = skb->mark;
  633. break;
  634. case OVS_KEY_ATTR_TUNNEL_INFO:
  635. /* Masked data not supported for tunnel. */
  636. err = -EINVAL;
  637. break;
  638. case OVS_KEY_ATTR_ETHERNET:
  639. err = set_eth_addr(skb, flow_key, nla_data(a),
  640. get_mask(a, struct ovs_key_ethernet *));
  641. break;
  642. case OVS_KEY_ATTR_IPV4:
  643. err = set_ipv4(skb, flow_key, nla_data(a),
  644. get_mask(a, struct ovs_key_ipv4 *));
  645. break;
  646. case OVS_KEY_ATTR_IPV6:
  647. err = set_ipv6(skb, flow_key, nla_data(a),
  648. get_mask(a, struct ovs_key_ipv6 *));
  649. break;
  650. case OVS_KEY_ATTR_TCP:
  651. err = set_tcp(skb, flow_key, nla_data(a),
  652. get_mask(a, struct ovs_key_tcp *));
  653. break;
  654. case OVS_KEY_ATTR_UDP:
  655. err = set_udp(skb, flow_key, nla_data(a),
  656. get_mask(a, struct ovs_key_udp *));
  657. break;
  658. case OVS_KEY_ATTR_SCTP:
  659. err = set_sctp(skb, flow_key, nla_data(a),
  660. get_mask(a, struct ovs_key_sctp *));
  661. break;
  662. case OVS_KEY_ATTR_MPLS:
  663. err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
  664. __be32 *));
  665. break;
  666. }
  667. return err;
  668. }
  669. static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
  670. struct sw_flow_key *key,
  671. const struct nlattr *a, int rem)
  672. {
  673. struct deferred_action *da;
  674. if (!is_flow_key_valid(key)) {
  675. int err;
  676. err = ovs_flow_key_update(skb, key);
  677. if (err)
  678. return err;
  679. }
  680. BUG_ON(!is_flow_key_valid(key));
  681. if (!nla_is_last(a, rem)) {
  682. /* Recirc action is the not the last action
  683. * of the action list, need to clone the skb.
  684. */
  685. skb = skb_clone(skb, GFP_ATOMIC);
  686. /* Skip the recirc action when out of memory, but
  687. * continue on with the rest of the action list.
  688. */
  689. if (!skb)
  690. return 0;
  691. }
  692. da = add_deferred_actions(skb, key, NULL);
  693. if (da) {
  694. da->pkt_key.recirc_id = nla_get_u32(a);
  695. } else {
  696. kfree_skb(skb);
  697. if (net_ratelimit())
  698. pr_warn("%s: deferred action limit reached, drop recirc action\n",
  699. ovs_dp_name(dp));
  700. }
  701. return 0;
  702. }
  703. /* Execute a list of actions against 'skb'. */
  704. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  705. struct sw_flow_key *key,
  706. const struct nlattr *attr, int len)
  707. {
  708. /* Every output action needs a separate clone of 'skb', but the common
  709. * case is just a single output action, so that doing a clone and
  710. * then freeing the original skbuff is wasteful. So the following code
  711. * is slightly obscure just to avoid that.
  712. */
  713. int prev_port = -1;
  714. const struct nlattr *a;
  715. int rem;
  716. for (a = attr, rem = len; rem > 0;
  717. a = nla_next(a, &rem)) {
  718. int err = 0;
  719. if (unlikely(prev_port != -1)) {
  720. struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
  721. if (out_skb)
  722. do_output(dp, out_skb, prev_port);
  723. prev_port = -1;
  724. }
  725. switch (nla_type(a)) {
  726. case OVS_ACTION_ATTR_OUTPUT:
  727. prev_port = nla_get_u32(a);
  728. break;
  729. case OVS_ACTION_ATTR_USERSPACE:
  730. output_userspace(dp, skb, key, a, attr, len);
  731. break;
  732. case OVS_ACTION_ATTR_HASH:
  733. execute_hash(skb, key, a);
  734. break;
  735. case OVS_ACTION_ATTR_PUSH_MPLS:
  736. err = push_mpls(skb, key, nla_data(a));
  737. break;
  738. case OVS_ACTION_ATTR_POP_MPLS:
  739. err = pop_mpls(skb, key, nla_get_be16(a));
  740. break;
  741. case OVS_ACTION_ATTR_PUSH_VLAN:
  742. err = push_vlan(skb, key, nla_data(a));
  743. break;
  744. case OVS_ACTION_ATTR_POP_VLAN:
  745. err = pop_vlan(skb, key);
  746. break;
  747. case OVS_ACTION_ATTR_RECIRC:
  748. err = execute_recirc(dp, skb, key, a, rem);
  749. if (nla_is_last(a, rem)) {
  750. /* If this is the last action, the skb has
  751. * been consumed or freed.
  752. * Return immediately.
  753. */
  754. return err;
  755. }
  756. break;
  757. case OVS_ACTION_ATTR_SET:
  758. err = execute_set_action(skb, key, nla_data(a));
  759. break;
  760. case OVS_ACTION_ATTR_SET_MASKED:
  761. case OVS_ACTION_ATTR_SET_TO_MASKED:
  762. err = execute_masked_set_action(skb, key, nla_data(a));
  763. break;
  764. case OVS_ACTION_ATTR_SAMPLE:
  765. err = sample(dp, skb, key, a, attr, len);
  766. break;
  767. }
  768. if (unlikely(err)) {
  769. kfree_skb(skb);
  770. return err;
  771. }
  772. }
  773. if (prev_port != -1)
  774. do_output(dp, skb, prev_port);
  775. else
  776. consume_skb(skb);
  777. return 0;
  778. }
  779. static void process_deferred_actions(struct datapath *dp)
  780. {
  781. struct action_fifo *fifo = this_cpu_ptr(action_fifos);
  782. /* Do not touch the FIFO in case there is no deferred actions. */
  783. if (action_fifo_is_empty(fifo))
  784. return;
  785. /* Finishing executing all deferred actions. */
  786. do {
  787. struct deferred_action *da = action_fifo_get(fifo);
  788. struct sk_buff *skb = da->skb;
  789. struct sw_flow_key *key = &da->pkt_key;
  790. const struct nlattr *actions = da->actions;
  791. if (actions)
  792. do_execute_actions(dp, skb, key, actions,
  793. nla_len(actions));
  794. else
  795. ovs_dp_process_packet(skb, key);
  796. } while (!action_fifo_is_empty(fifo));
  797. /* Reset FIFO for the next packet. */
  798. action_fifo_init(fifo);
  799. }
  800. /* Execute a list of actions against 'skb'. */
  801. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  802. const struct sw_flow_actions *acts,
  803. struct sw_flow_key *key)
  804. {
  805. int level = this_cpu_read(exec_actions_level);
  806. int err;
  807. this_cpu_inc(exec_actions_level);
  808. OVS_CB(skb)->egress_tun_info = NULL;
  809. err = do_execute_actions(dp, skb, key,
  810. acts->actions, acts->actions_len);
  811. if (!level)
  812. process_deferred_actions(dp);
  813. this_cpu_dec(exec_actions_level);
  814. return err;
  815. }
  816. int action_fifos_init(void)
  817. {
  818. action_fifos = alloc_percpu(struct action_fifo);
  819. if (!action_fifos)
  820. return -ENOMEM;
  821. return 0;
  822. }
  823. void action_fifos_exit(void)
  824. {
  825. free_percpu(action_fifos);
  826. }