datapath.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  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/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. int ovs_net_id __read_mostly;
  58. EXPORT_SYMBOL_GPL(ovs_net_id);
  59. static struct genl_family dp_packet_genl_family;
  60. static struct genl_family dp_flow_genl_family;
  61. static struct genl_family dp_datapath_genl_family;
  62. static const struct nla_policy flow_policy[];
  63. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  64. .name = OVS_FLOW_MCGROUP,
  65. };
  66. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  67. .name = OVS_DATAPATH_MCGROUP,
  68. };
  69. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  70. .name = OVS_VPORT_MCGROUP,
  71. };
  72. /* Check if need to build a reply message.
  73. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  74. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  75. unsigned int group)
  76. {
  77. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  78. genl_has_listeners(family, genl_info_net(info), group);
  79. }
  80. static void ovs_notify(struct genl_family *family,
  81. struct sk_buff *skb, struct genl_info *info)
  82. {
  83. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  84. 0, info->nlhdr, GFP_KERNEL);
  85. }
  86. /**
  87. * DOC: Locking:
  88. *
  89. * All writes e.g. Writes to device state (add/remove datapath, port, set
  90. * operations on vports, etc.), Writes to other state (flow table
  91. * modifications, set miscellaneous datapath parameters, etc.) are protected
  92. * by ovs_lock.
  93. *
  94. * Reads are protected by RCU.
  95. *
  96. * There are a few special cases (mostly stats) that have their own
  97. * synchronization but they nest under all of above and don't interact with
  98. * each other.
  99. *
  100. * The RTNL lock nests inside ovs_mutex.
  101. */
  102. static DEFINE_MUTEX(ovs_mutex);
  103. void ovs_lock(void)
  104. {
  105. mutex_lock(&ovs_mutex);
  106. }
  107. void ovs_unlock(void)
  108. {
  109. mutex_unlock(&ovs_mutex);
  110. }
  111. #ifdef CONFIG_LOCKDEP
  112. int lockdep_ovsl_is_held(void)
  113. {
  114. if (debug_locks)
  115. return lockdep_is_held(&ovs_mutex);
  116. else
  117. return 1;
  118. }
  119. EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
  120. #endif
  121. static struct vport *new_vport(const struct vport_parms *);
  122. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  123. const struct sw_flow_key *,
  124. const struct dp_upcall_info *);
  125. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  126. const struct sw_flow_key *,
  127. const struct dp_upcall_info *);
  128. /* Must be called with rcu_read_lock. */
  129. static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
  130. {
  131. struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
  132. if (dev) {
  133. struct vport *vport = ovs_internal_dev_get_vport(dev);
  134. if (vport)
  135. return vport->dp;
  136. }
  137. return NULL;
  138. }
  139. /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
  140. * returned dp pointer valid.
  141. */
  142. static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
  143. {
  144. struct datapath *dp;
  145. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  146. rcu_read_lock();
  147. dp = get_dp_rcu(net, dp_ifindex);
  148. rcu_read_unlock();
  149. return dp;
  150. }
  151. /* Must be called with rcu_read_lock or ovs_mutex. */
  152. const char *ovs_dp_name(const struct datapath *dp)
  153. {
  154. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  155. return vport->ops->get_name(vport);
  156. }
  157. static int get_dpifindex(const struct datapath *dp)
  158. {
  159. struct vport *local;
  160. int ifindex;
  161. rcu_read_lock();
  162. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  163. if (local)
  164. ifindex = netdev_vport_priv(local)->dev->ifindex;
  165. else
  166. ifindex = 0;
  167. rcu_read_unlock();
  168. return ifindex;
  169. }
  170. static void destroy_dp_rcu(struct rcu_head *rcu)
  171. {
  172. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  173. ovs_flow_tbl_destroy(&dp->table);
  174. free_percpu(dp->stats_percpu);
  175. kfree(dp->ports);
  176. kfree(dp);
  177. }
  178. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  179. u16 port_no)
  180. {
  181. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  182. }
  183. /* Called with ovs_mutex or RCU read lock. */
  184. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  185. {
  186. struct vport *vport;
  187. struct hlist_head *head;
  188. head = vport_hash_bucket(dp, port_no);
  189. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  190. if (vport->port_no == port_no)
  191. return vport;
  192. }
  193. return NULL;
  194. }
  195. /* Called with ovs_mutex. */
  196. static struct vport *new_vport(const struct vport_parms *parms)
  197. {
  198. struct vport *vport;
  199. vport = ovs_vport_add(parms);
  200. if (!IS_ERR(vport)) {
  201. struct datapath *dp = parms->dp;
  202. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  203. hlist_add_head_rcu(&vport->dp_hash_node, head);
  204. }
  205. return vport;
  206. }
  207. void ovs_dp_detach_port(struct vport *p)
  208. {
  209. ASSERT_OVSL();
  210. /* First drop references to device. */
  211. hlist_del_rcu(&p->dp_hash_node);
  212. /* Then destroy it. */
  213. ovs_vport_del(p);
  214. }
  215. /* Must be called with rcu_read_lock. */
  216. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  217. {
  218. const struct vport *p = OVS_CB(skb)->input_vport;
  219. struct datapath *dp = p->dp;
  220. struct sw_flow *flow;
  221. struct sw_flow_actions *sf_acts;
  222. struct dp_stats_percpu *stats;
  223. u64 *stats_counter;
  224. u32 n_mask_hit;
  225. stats = this_cpu_ptr(dp->stats_percpu);
  226. /* Look up flow. */
  227. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  228. if (unlikely(!flow)) {
  229. struct dp_upcall_info upcall;
  230. int error;
  231. upcall.cmd = OVS_PACKET_CMD_MISS;
  232. upcall.userdata = NULL;
  233. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  234. upcall.egress_tun_info = NULL;
  235. error = ovs_dp_upcall(dp, skb, key, &upcall);
  236. if (unlikely(error))
  237. kfree_skb(skb);
  238. else
  239. consume_skb(skb);
  240. stats_counter = &stats->n_missed;
  241. goto out;
  242. }
  243. ovs_flow_stats_update(flow, key->tp.flags, skb);
  244. sf_acts = rcu_dereference(flow->sf_acts);
  245. ovs_execute_actions(dp, skb, sf_acts, key);
  246. stats_counter = &stats->n_hit;
  247. out:
  248. /* Update datapath statistics. */
  249. u64_stats_update_begin(&stats->syncp);
  250. (*stats_counter)++;
  251. stats->n_mask_hit += n_mask_hit;
  252. u64_stats_update_end(&stats->syncp);
  253. }
  254. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  255. const struct sw_flow_key *key,
  256. const struct dp_upcall_info *upcall_info)
  257. {
  258. struct dp_stats_percpu *stats;
  259. int err;
  260. if (upcall_info->portid == 0) {
  261. err = -ENOTCONN;
  262. goto err;
  263. }
  264. if (!skb_is_gso(skb))
  265. err = queue_userspace_packet(dp, skb, key, upcall_info);
  266. else
  267. err = queue_gso_packets(dp, skb, key, upcall_info);
  268. if (err)
  269. goto err;
  270. return 0;
  271. err:
  272. stats = this_cpu_ptr(dp->stats_percpu);
  273. u64_stats_update_begin(&stats->syncp);
  274. stats->n_lost++;
  275. u64_stats_update_end(&stats->syncp);
  276. return err;
  277. }
  278. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  279. const struct sw_flow_key *key,
  280. const struct dp_upcall_info *upcall_info)
  281. {
  282. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  283. struct sw_flow_key later_key;
  284. struct sk_buff *segs, *nskb;
  285. struct ovs_skb_cb ovs_cb;
  286. int err;
  287. ovs_cb = *OVS_CB(skb);
  288. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  289. *OVS_CB(skb) = ovs_cb;
  290. if (IS_ERR(segs))
  291. return PTR_ERR(segs);
  292. if (segs == NULL)
  293. return -EINVAL;
  294. if (gso_type & SKB_GSO_UDP) {
  295. /* The initial flow key extracted by ovs_flow_key_extract()
  296. * in this case is for a first fragment, so we need to
  297. * properly mark later fragments.
  298. */
  299. later_key = *key;
  300. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  301. }
  302. /* Queue all of the segments. */
  303. skb = segs;
  304. do {
  305. *OVS_CB(skb) = ovs_cb;
  306. if (gso_type & SKB_GSO_UDP && skb != segs)
  307. key = &later_key;
  308. err = queue_userspace_packet(dp, skb, key, upcall_info);
  309. if (err)
  310. break;
  311. } while ((skb = skb->next));
  312. /* Free all of the segments. */
  313. skb = segs;
  314. do {
  315. nskb = skb->next;
  316. if (err)
  317. kfree_skb(skb);
  318. else
  319. consume_skb(skb);
  320. } while ((skb = nskb));
  321. return err;
  322. }
  323. static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
  324. unsigned int hdrlen)
  325. {
  326. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  327. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  328. + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  329. /* OVS_PACKET_ATTR_USERDATA */
  330. if (upcall_info->userdata)
  331. size += NLA_ALIGN(upcall_info->userdata->nla_len);
  332. /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
  333. if (upcall_info->egress_tun_info)
  334. size += nla_total_size(ovs_tun_key_attr_size());
  335. return size;
  336. }
  337. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  338. const struct sw_flow_key *key,
  339. const struct dp_upcall_info *upcall_info)
  340. {
  341. struct ovs_header *upcall;
  342. struct sk_buff *nskb = NULL;
  343. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  344. struct nlattr *nla;
  345. struct genl_info info = {
  346. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  347. .snd_portid = upcall_info->portid,
  348. };
  349. size_t len;
  350. unsigned int hlen;
  351. int err, dp_ifindex;
  352. dp_ifindex = get_dpifindex(dp);
  353. if (!dp_ifindex)
  354. return -ENODEV;
  355. if (skb_vlan_tag_present(skb)) {
  356. nskb = skb_clone(skb, GFP_ATOMIC);
  357. if (!nskb)
  358. return -ENOMEM;
  359. nskb = __vlan_hwaccel_push_inside(nskb);
  360. if (!nskb)
  361. return -ENOMEM;
  362. skb = nskb;
  363. }
  364. if (nla_attr_size(skb->len) > USHRT_MAX) {
  365. err = -EFBIG;
  366. goto out;
  367. }
  368. /* Complete checksum if needed */
  369. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  370. (err = skb_checksum_help(skb)))
  371. goto out;
  372. /* Older versions of OVS user space enforce alignment of the last
  373. * Netlink attribute to NLA_ALIGNTO which would require extensive
  374. * padding logic. Only perform zerocopy if padding is not required.
  375. */
  376. if (dp->user_features & OVS_DP_F_UNALIGNED)
  377. hlen = skb_zerocopy_headlen(skb);
  378. else
  379. hlen = skb->len;
  380. len = upcall_msg_size(upcall_info, hlen);
  381. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  382. if (!user_skb) {
  383. err = -ENOMEM;
  384. goto out;
  385. }
  386. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  387. 0, upcall_info->cmd);
  388. upcall->dp_ifindex = dp_ifindex;
  389. err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
  390. BUG_ON(err);
  391. if (upcall_info->userdata)
  392. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  393. nla_len(upcall_info->userdata),
  394. nla_data(upcall_info->userdata));
  395. if (upcall_info->egress_tun_info) {
  396. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
  397. err = ovs_nla_put_egress_tunnel_key(user_skb,
  398. upcall_info->egress_tun_info);
  399. BUG_ON(err);
  400. nla_nest_end(user_skb, nla);
  401. }
  402. /* Only reserve room for attribute header, packet data is added
  403. * in skb_zerocopy() */
  404. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  405. err = -ENOBUFS;
  406. goto out;
  407. }
  408. nla->nla_len = nla_attr_size(skb->len);
  409. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  410. if (err)
  411. goto out;
  412. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  413. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  414. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  415. if (plen > 0)
  416. memset(skb_put(user_skb, plen), 0, plen);
  417. }
  418. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  419. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  420. user_skb = NULL;
  421. out:
  422. if (err)
  423. skb_tx_error(skb);
  424. kfree_skb(user_skb);
  425. kfree_skb(nskb);
  426. return err;
  427. }
  428. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  429. {
  430. struct ovs_header *ovs_header = info->userhdr;
  431. struct nlattr **a = info->attrs;
  432. struct sw_flow_actions *acts;
  433. struct sk_buff *packet;
  434. struct sw_flow *flow;
  435. struct sw_flow_actions *sf_acts;
  436. struct datapath *dp;
  437. struct ethhdr *eth;
  438. struct vport *input_vport;
  439. int len;
  440. int err;
  441. bool log = !a[OVS_PACKET_ATTR_PROBE];
  442. err = -EINVAL;
  443. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  444. !a[OVS_PACKET_ATTR_ACTIONS])
  445. goto err;
  446. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  447. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  448. err = -ENOMEM;
  449. if (!packet)
  450. goto err;
  451. skb_reserve(packet, NET_IP_ALIGN);
  452. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  453. skb_reset_mac_header(packet);
  454. eth = eth_hdr(packet);
  455. /* Normally, setting the skb 'protocol' field would be handled by a
  456. * call to eth_type_trans(), but it assumes there's a sending
  457. * device, which we may not have. */
  458. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  459. packet->protocol = eth->h_proto;
  460. else
  461. packet->protocol = htons(ETH_P_802_2);
  462. /* Build an sw_flow for sending this packet. */
  463. flow = ovs_flow_alloc();
  464. err = PTR_ERR(flow);
  465. if (IS_ERR(flow))
  466. goto err_kfree_skb;
  467. err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
  468. &flow->key, log);
  469. if (err)
  470. goto err_flow_free;
  471. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  472. &flow->key, &acts, log);
  473. if (err)
  474. goto err_flow_free;
  475. rcu_assign_pointer(flow->sf_acts, acts);
  476. OVS_CB(packet)->egress_tun_info = NULL;
  477. packet->priority = flow->key.phy.priority;
  478. packet->mark = flow->key.phy.skb_mark;
  479. rcu_read_lock();
  480. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  481. err = -ENODEV;
  482. if (!dp)
  483. goto err_unlock;
  484. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  485. if (!input_vport)
  486. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  487. if (!input_vport)
  488. goto err_unlock;
  489. OVS_CB(packet)->input_vport = input_vport;
  490. sf_acts = rcu_dereference(flow->sf_acts);
  491. local_bh_disable();
  492. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  493. local_bh_enable();
  494. rcu_read_unlock();
  495. ovs_flow_free(flow, false);
  496. return err;
  497. err_unlock:
  498. rcu_read_unlock();
  499. err_flow_free:
  500. ovs_flow_free(flow, false);
  501. err_kfree_skb:
  502. kfree_skb(packet);
  503. err:
  504. return err;
  505. }
  506. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  507. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  508. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  509. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  510. [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
  511. };
  512. static const struct genl_ops dp_packet_genl_ops[] = {
  513. { .cmd = OVS_PACKET_CMD_EXECUTE,
  514. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  515. .policy = packet_policy,
  516. .doit = ovs_packet_cmd_execute
  517. }
  518. };
  519. static struct genl_family dp_packet_genl_family = {
  520. .id = GENL_ID_GENERATE,
  521. .hdrsize = sizeof(struct ovs_header),
  522. .name = OVS_PACKET_FAMILY,
  523. .version = OVS_PACKET_VERSION,
  524. .maxattr = OVS_PACKET_ATTR_MAX,
  525. .netnsok = true,
  526. .parallel_ops = true,
  527. .ops = dp_packet_genl_ops,
  528. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  529. };
  530. static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
  531. struct ovs_dp_megaflow_stats *mega_stats)
  532. {
  533. int i;
  534. memset(mega_stats, 0, sizeof(*mega_stats));
  535. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  536. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  537. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  538. for_each_possible_cpu(i) {
  539. const struct dp_stats_percpu *percpu_stats;
  540. struct dp_stats_percpu local_stats;
  541. unsigned int start;
  542. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  543. do {
  544. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  545. local_stats = *percpu_stats;
  546. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  547. stats->n_hit += local_stats.n_hit;
  548. stats->n_missed += local_stats.n_missed;
  549. stats->n_lost += local_stats.n_lost;
  550. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  551. }
  552. }
  553. static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
  554. {
  555. return ovs_identifier_is_ufid(sfid) &&
  556. !(ufid_flags & OVS_UFID_F_OMIT_KEY);
  557. }
  558. static bool should_fill_mask(uint32_t ufid_flags)
  559. {
  560. return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
  561. }
  562. static bool should_fill_actions(uint32_t ufid_flags)
  563. {
  564. return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
  565. }
  566. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
  567. const struct sw_flow_id *sfid,
  568. uint32_t ufid_flags)
  569. {
  570. size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
  571. /* OVS_FLOW_ATTR_UFID */
  572. if (sfid && ovs_identifier_is_ufid(sfid))
  573. len += nla_total_size(sfid->ufid_len);
  574. /* OVS_FLOW_ATTR_KEY */
  575. if (!sfid || should_fill_key(sfid, ufid_flags))
  576. len += nla_total_size(ovs_key_attr_size());
  577. /* OVS_FLOW_ATTR_MASK */
  578. if (should_fill_mask(ufid_flags))
  579. len += nla_total_size(ovs_key_attr_size());
  580. /* OVS_FLOW_ATTR_ACTIONS */
  581. if (should_fill_actions(ufid_flags))
  582. len += nla_total_size(acts->actions_len);
  583. return len
  584. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  585. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  586. + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
  587. }
  588. /* Called with ovs_mutex or RCU read lock. */
  589. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  590. struct sk_buff *skb)
  591. {
  592. struct ovs_flow_stats stats;
  593. __be16 tcp_flags;
  594. unsigned long used;
  595. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  596. if (used &&
  597. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  598. return -EMSGSIZE;
  599. if (stats.n_packets &&
  600. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  601. return -EMSGSIZE;
  602. if ((u8)ntohs(tcp_flags) &&
  603. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  604. return -EMSGSIZE;
  605. return 0;
  606. }
  607. /* Called with ovs_mutex or RCU read lock. */
  608. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  609. struct sk_buff *skb, int skb_orig_len)
  610. {
  611. struct nlattr *start;
  612. int err;
  613. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  614. * this is the first flow to be dumped into 'skb'. This is unusual for
  615. * Netlink but individual action lists can be longer than
  616. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  617. * The userspace caller can always fetch the actions separately if it
  618. * really wants them. (Most userspace callers in fact don't care.)
  619. *
  620. * This can only fail for dump operations because the skb is always
  621. * properly sized for single flows.
  622. */
  623. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  624. if (start) {
  625. const struct sw_flow_actions *sf_acts;
  626. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  627. err = ovs_nla_put_actions(sf_acts->actions,
  628. sf_acts->actions_len, skb);
  629. if (!err)
  630. nla_nest_end(skb, start);
  631. else {
  632. if (skb_orig_len)
  633. return err;
  634. nla_nest_cancel(skb, start);
  635. }
  636. } else if (skb_orig_len) {
  637. return -EMSGSIZE;
  638. }
  639. return 0;
  640. }
  641. /* Called with ovs_mutex or RCU read lock. */
  642. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  643. struct sk_buff *skb, u32 portid,
  644. u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
  645. {
  646. const int skb_orig_len = skb->len;
  647. struct ovs_header *ovs_header;
  648. int err;
  649. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  650. flags, cmd);
  651. if (!ovs_header)
  652. return -EMSGSIZE;
  653. ovs_header->dp_ifindex = dp_ifindex;
  654. err = ovs_nla_put_identifier(flow, skb);
  655. if (err)
  656. goto error;
  657. if (should_fill_key(&flow->id, ufid_flags)) {
  658. err = ovs_nla_put_masked_key(flow, skb);
  659. if (err)
  660. goto error;
  661. }
  662. if (should_fill_mask(ufid_flags)) {
  663. err = ovs_nla_put_mask(flow, skb);
  664. if (err)
  665. goto error;
  666. }
  667. err = ovs_flow_cmd_fill_stats(flow, skb);
  668. if (err)
  669. goto error;
  670. if (should_fill_actions(ufid_flags)) {
  671. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  672. if (err)
  673. goto error;
  674. }
  675. genlmsg_end(skb, ovs_header);
  676. return 0;
  677. error:
  678. genlmsg_cancel(skb, ovs_header);
  679. return err;
  680. }
  681. /* May not be called with RCU read lock. */
  682. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  683. const struct sw_flow_id *sfid,
  684. struct genl_info *info,
  685. bool always,
  686. uint32_t ufid_flags)
  687. {
  688. struct sk_buff *skb;
  689. size_t len;
  690. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  691. return NULL;
  692. len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
  693. skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
  694. if (!skb)
  695. return ERR_PTR(-ENOMEM);
  696. return skb;
  697. }
  698. /* Called with ovs_mutex. */
  699. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  700. int dp_ifindex,
  701. struct genl_info *info, u8 cmd,
  702. bool always, u32 ufid_flags)
  703. {
  704. struct sk_buff *skb;
  705. int retval;
  706. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
  707. &flow->id, info, always, ufid_flags);
  708. if (IS_ERR_OR_NULL(skb))
  709. return skb;
  710. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  711. info->snd_portid, info->snd_seq, 0,
  712. cmd, ufid_flags);
  713. BUG_ON(retval < 0);
  714. return skb;
  715. }
  716. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  717. {
  718. struct nlattr **a = info->attrs;
  719. struct ovs_header *ovs_header = info->userhdr;
  720. struct sw_flow *flow = NULL, *new_flow;
  721. struct sw_flow_mask mask;
  722. struct sk_buff *reply;
  723. struct datapath *dp;
  724. struct sw_flow_key key;
  725. struct sw_flow_actions *acts;
  726. struct sw_flow_match match;
  727. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  728. int error;
  729. bool log = !a[OVS_FLOW_ATTR_PROBE];
  730. /* Must have key and actions. */
  731. error = -EINVAL;
  732. if (!a[OVS_FLOW_ATTR_KEY]) {
  733. OVS_NLERR(log, "Flow key attr not present in new flow.");
  734. goto error;
  735. }
  736. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  737. OVS_NLERR(log, "Flow actions attr not present in new flow.");
  738. goto error;
  739. }
  740. /* Most of the time we need to allocate a new flow, do it before
  741. * locking.
  742. */
  743. new_flow = ovs_flow_alloc();
  744. if (IS_ERR(new_flow)) {
  745. error = PTR_ERR(new_flow);
  746. goto error;
  747. }
  748. /* Extract key. */
  749. ovs_match_init(&match, &key, &mask);
  750. error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
  751. a[OVS_FLOW_ATTR_MASK], log);
  752. if (error)
  753. goto err_kfree_flow;
  754. ovs_flow_mask_key(&new_flow->key, &key, &mask);
  755. /* Extract flow identifier. */
  756. error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
  757. &key, log);
  758. if (error)
  759. goto err_kfree_flow;
  760. /* Validate actions. */
  761. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  762. &acts, log);
  763. if (error) {
  764. OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
  765. goto err_kfree_flow;
  766. }
  767. reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
  768. ufid_flags);
  769. if (IS_ERR(reply)) {
  770. error = PTR_ERR(reply);
  771. goto err_kfree_acts;
  772. }
  773. ovs_lock();
  774. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  775. if (unlikely(!dp)) {
  776. error = -ENODEV;
  777. goto err_unlock_ovs;
  778. }
  779. /* Check if this is a duplicate flow */
  780. if (ovs_identifier_is_ufid(&new_flow->id))
  781. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
  782. if (!flow)
  783. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  784. if (likely(!flow)) {
  785. rcu_assign_pointer(new_flow->sf_acts, acts);
  786. /* Put flow in bucket. */
  787. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  788. if (unlikely(error)) {
  789. acts = NULL;
  790. goto err_unlock_ovs;
  791. }
  792. if (unlikely(reply)) {
  793. error = ovs_flow_cmd_fill_info(new_flow,
  794. ovs_header->dp_ifindex,
  795. reply, info->snd_portid,
  796. info->snd_seq, 0,
  797. OVS_FLOW_CMD_NEW,
  798. ufid_flags);
  799. BUG_ON(error < 0);
  800. }
  801. ovs_unlock();
  802. } else {
  803. struct sw_flow_actions *old_acts;
  804. /* Bail out if we're not allowed to modify an existing flow.
  805. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  806. * because Generic Netlink treats the latter as a dump
  807. * request. We also accept NLM_F_EXCL in case that bug ever
  808. * gets fixed.
  809. */
  810. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  811. | NLM_F_EXCL))) {
  812. error = -EEXIST;
  813. goto err_unlock_ovs;
  814. }
  815. /* The flow identifier has to be the same for flow updates.
  816. * Look for any overlapping flow.
  817. */
  818. if (unlikely(!ovs_flow_cmp(flow, &match))) {
  819. if (ovs_identifier_is_key(&flow->id))
  820. flow = ovs_flow_tbl_lookup_exact(&dp->table,
  821. &match);
  822. else /* UFID matches but key is different */
  823. flow = NULL;
  824. if (!flow) {
  825. error = -ENOENT;
  826. goto err_unlock_ovs;
  827. }
  828. }
  829. /* Update actions. */
  830. old_acts = ovsl_dereference(flow->sf_acts);
  831. rcu_assign_pointer(flow->sf_acts, acts);
  832. if (unlikely(reply)) {
  833. error = ovs_flow_cmd_fill_info(flow,
  834. ovs_header->dp_ifindex,
  835. reply, info->snd_portid,
  836. info->snd_seq, 0,
  837. OVS_FLOW_CMD_NEW,
  838. ufid_flags);
  839. BUG_ON(error < 0);
  840. }
  841. ovs_unlock();
  842. ovs_nla_free_flow_actions(old_acts);
  843. ovs_flow_free(new_flow, false);
  844. }
  845. if (reply)
  846. ovs_notify(&dp_flow_genl_family, reply, info);
  847. return 0;
  848. err_unlock_ovs:
  849. ovs_unlock();
  850. kfree_skb(reply);
  851. err_kfree_acts:
  852. kfree(acts);
  853. err_kfree_flow:
  854. ovs_flow_free(new_flow, false);
  855. error:
  856. return error;
  857. }
  858. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  859. static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
  860. const struct sw_flow_key *key,
  861. const struct sw_flow_mask *mask,
  862. bool log)
  863. {
  864. struct sw_flow_actions *acts;
  865. struct sw_flow_key masked_key;
  866. int error;
  867. ovs_flow_mask_key(&masked_key, key, mask);
  868. error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
  869. if (error) {
  870. OVS_NLERR(log,
  871. "Actions may not be safe on all matching packets");
  872. return ERR_PTR(error);
  873. }
  874. return acts;
  875. }
  876. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  877. {
  878. struct nlattr **a = info->attrs;
  879. struct ovs_header *ovs_header = info->userhdr;
  880. struct sw_flow_key key;
  881. struct sw_flow *flow;
  882. struct sw_flow_mask mask;
  883. struct sk_buff *reply = NULL;
  884. struct datapath *dp;
  885. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  886. struct sw_flow_match match;
  887. struct sw_flow_id sfid;
  888. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  889. int error;
  890. bool log = !a[OVS_FLOW_ATTR_PROBE];
  891. bool ufid_present;
  892. /* Extract key. */
  893. error = -EINVAL;
  894. if (!a[OVS_FLOW_ATTR_KEY]) {
  895. OVS_NLERR(log, "Flow key attribute not present in set flow.");
  896. goto error;
  897. }
  898. ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
  899. ovs_match_init(&match, &key, &mask);
  900. error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
  901. a[OVS_FLOW_ATTR_MASK], log);
  902. if (error)
  903. goto error;
  904. /* Validate actions. */
  905. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  906. acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
  907. log);
  908. if (IS_ERR(acts)) {
  909. error = PTR_ERR(acts);
  910. goto error;
  911. }
  912. /* Can allocate before locking if have acts. */
  913. reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
  914. ufid_flags);
  915. if (IS_ERR(reply)) {
  916. error = PTR_ERR(reply);
  917. goto err_kfree_acts;
  918. }
  919. }
  920. ovs_lock();
  921. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  922. if (unlikely(!dp)) {
  923. error = -ENODEV;
  924. goto err_unlock_ovs;
  925. }
  926. /* Check that the flow exists. */
  927. if (ufid_present)
  928. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
  929. else
  930. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  931. if (unlikely(!flow)) {
  932. error = -ENOENT;
  933. goto err_unlock_ovs;
  934. }
  935. /* Update actions, if present. */
  936. if (likely(acts)) {
  937. old_acts = ovsl_dereference(flow->sf_acts);
  938. rcu_assign_pointer(flow->sf_acts, acts);
  939. if (unlikely(reply)) {
  940. error = ovs_flow_cmd_fill_info(flow,
  941. ovs_header->dp_ifindex,
  942. reply, info->snd_portid,
  943. info->snd_seq, 0,
  944. OVS_FLOW_CMD_NEW,
  945. ufid_flags);
  946. BUG_ON(error < 0);
  947. }
  948. } else {
  949. /* Could not alloc without acts before locking. */
  950. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  951. info, OVS_FLOW_CMD_NEW, false,
  952. ufid_flags);
  953. if (unlikely(IS_ERR(reply))) {
  954. error = PTR_ERR(reply);
  955. goto err_unlock_ovs;
  956. }
  957. }
  958. /* Clear stats. */
  959. if (a[OVS_FLOW_ATTR_CLEAR])
  960. ovs_flow_stats_clear(flow);
  961. ovs_unlock();
  962. if (reply)
  963. ovs_notify(&dp_flow_genl_family, reply, info);
  964. if (old_acts)
  965. ovs_nla_free_flow_actions(old_acts);
  966. return 0;
  967. err_unlock_ovs:
  968. ovs_unlock();
  969. kfree_skb(reply);
  970. err_kfree_acts:
  971. kfree(acts);
  972. error:
  973. return error;
  974. }
  975. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  976. {
  977. struct nlattr **a = info->attrs;
  978. struct ovs_header *ovs_header = info->userhdr;
  979. struct sw_flow_key key;
  980. struct sk_buff *reply;
  981. struct sw_flow *flow;
  982. struct datapath *dp;
  983. struct sw_flow_match match;
  984. struct sw_flow_id ufid;
  985. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  986. int err = 0;
  987. bool log = !a[OVS_FLOW_ATTR_PROBE];
  988. bool ufid_present;
  989. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  990. if (a[OVS_FLOW_ATTR_KEY]) {
  991. ovs_match_init(&match, &key, NULL);
  992. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
  993. log);
  994. } else if (!ufid_present) {
  995. OVS_NLERR(log,
  996. "Flow get message rejected, Key attribute missing.");
  997. err = -EINVAL;
  998. }
  999. if (err)
  1000. return err;
  1001. ovs_lock();
  1002. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1003. if (!dp) {
  1004. err = -ENODEV;
  1005. goto unlock;
  1006. }
  1007. if (ufid_present)
  1008. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1009. else
  1010. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1011. if (!flow) {
  1012. err = -ENOENT;
  1013. goto unlock;
  1014. }
  1015. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  1016. OVS_FLOW_CMD_NEW, true, ufid_flags);
  1017. if (IS_ERR(reply)) {
  1018. err = PTR_ERR(reply);
  1019. goto unlock;
  1020. }
  1021. ovs_unlock();
  1022. return genlmsg_reply(reply, info);
  1023. unlock:
  1024. ovs_unlock();
  1025. return err;
  1026. }
  1027. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1028. {
  1029. struct nlattr **a = info->attrs;
  1030. struct ovs_header *ovs_header = info->userhdr;
  1031. struct sw_flow_key key;
  1032. struct sk_buff *reply;
  1033. struct sw_flow *flow = NULL;
  1034. struct datapath *dp;
  1035. struct sw_flow_match match;
  1036. struct sw_flow_id ufid;
  1037. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1038. int err;
  1039. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1040. bool ufid_present;
  1041. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1042. if (a[OVS_FLOW_ATTR_KEY]) {
  1043. ovs_match_init(&match, &key, NULL);
  1044. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
  1045. log);
  1046. if (unlikely(err))
  1047. return err;
  1048. }
  1049. ovs_lock();
  1050. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1051. if (unlikely(!dp)) {
  1052. err = -ENODEV;
  1053. goto unlock;
  1054. }
  1055. if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
  1056. err = ovs_flow_tbl_flush(&dp->table);
  1057. goto unlock;
  1058. }
  1059. if (ufid_present)
  1060. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1061. else
  1062. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1063. if (unlikely(!flow)) {
  1064. err = -ENOENT;
  1065. goto unlock;
  1066. }
  1067. ovs_flow_tbl_remove(&dp->table, flow);
  1068. ovs_unlock();
  1069. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  1070. &flow->id, info, false, ufid_flags);
  1071. if (likely(reply)) {
  1072. if (likely(!IS_ERR(reply))) {
  1073. rcu_read_lock(); /*To keep RCU checker happy. */
  1074. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  1075. reply, info->snd_portid,
  1076. info->snd_seq, 0,
  1077. OVS_FLOW_CMD_DEL,
  1078. ufid_flags);
  1079. rcu_read_unlock();
  1080. BUG_ON(err < 0);
  1081. ovs_notify(&dp_flow_genl_family, reply, info);
  1082. } else {
  1083. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  1084. }
  1085. }
  1086. ovs_flow_free(flow, true);
  1087. return 0;
  1088. unlock:
  1089. ovs_unlock();
  1090. return err;
  1091. }
  1092. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1093. {
  1094. struct nlattr *a[__OVS_FLOW_ATTR_MAX];
  1095. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1096. struct table_instance *ti;
  1097. struct datapath *dp;
  1098. u32 ufid_flags;
  1099. int err;
  1100. err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
  1101. OVS_FLOW_ATTR_MAX, flow_policy);
  1102. if (err)
  1103. return err;
  1104. ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1105. rcu_read_lock();
  1106. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1107. if (!dp) {
  1108. rcu_read_unlock();
  1109. return -ENODEV;
  1110. }
  1111. ti = rcu_dereference(dp->table.ti);
  1112. for (;;) {
  1113. struct sw_flow *flow;
  1114. u32 bucket, obj;
  1115. bucket = cb->args[0];
  1116. obj = cb->args[1];
  1117. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1118. if (!flow)
  1119. break;
  1120. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1121. NETLINK_CB(cb->skb).portid,
  1122. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1123. OVS_FLOW_CMD_NEW, ufid_flags) < 0)
  1124. break;
  1125. cb->args[0] = bucket;
  1126. cb->args[1] = obj;
  1127. }
  1128. rcu_read_unlock();
  1129. return skb->len;
  1130. }
  1131. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1132. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1133. [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
  1134. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1135. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1136. [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
  1137. [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
  1138. [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
  1139. };
  1140. static const struct genl_ops dp_flow_genl_ops[] = {
  1141. { .cmd = OVS_FLOW_CMD_NEW,
  1142. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1143. .policy = flow_policy,
  1144. .doit = ovs_flow_cmd_new
  1145. },
  1146. { .cmd = OVS_FLOW_CMD_DEL,
  1147. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1148. .policy = flow_policy,
  1149. .doit = ovs_flow_cmd_del
  1150. },
  1151. { .cmd = OVS_FLOW_CMD_GET,
  1152. .flags = 0, /* OK for unprivileged users. */
  1153. .policy = flow_policy,
  1154. .doit = ovs_flow_cmd_get,
  1155. .dumpit = ovs_flow_cmd_dump
  1156. },
  1157. { .cmd = OVS_FLOW_CMD_SET,
  1158. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1159. .policy = flow_policy,
  1160. .doit = ovs_flow_cmd_set,
  1161. },
  1162. };
  1163. static struct genl_family dp_flow_genl_family = {
  1164. .id = GENL_ID_GENERATE,
  1165. .hdrsize = sizeof(struct ovs_header),
  1166. .name = OVS_FLOW_FAMILY,
  1167. .version = OVS_FLOW_VERSION,
  1168. .maxattr = OVS_FLOW_ATTR_MAX,
  1169. .netnsok = true,
  1170. .parallel_ops = true,
  1171. .ops = dp_flow_genl_ops,
  1172. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1173. .mcgrps = &ovs_dp_flow_multicast_group,
  1174. .n_mcgrps = 1,
  1175. };
  1176. static size_t ovs_dp_cmd_msg_size(void)
  1177. {
  1178. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1179. msgsize += nla_total_size(IFNAMSIZ);
  1180. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1181. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1182. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1183. return msgsize;
  1184. }
  1185. /* Called with ovs_mutex. */
  1186. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1187. u32 portid, u32 seq, u32 flags, u8 cmd)
  1188. {
  1189. struct ovs_header *ovs_header;
  1190. struct ovs_dp_stats dp_stats;
  1191. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1192. int err;
  1193. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1194. flags, cmd);
  1195. if (!ovs_header)
  1196. goto error;
  1197. ovs_header->dp_ifindex = get_dpifindex(dp);
  1198. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1199. if (err)
  1200. goto nla_put_failure;
  1201. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1202. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1203. &dp_stats))
  1204. goto nla_put_failure;
  1205. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1206. sizeof(struct ovs_dp_megaflow_stats),
  1207. &dp_megaflow_stats))
  1208. goto nla_put_failure;
  1209. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1210. goto nla_put_failure;
  1211. genlmsg_end(skb, ovs_header);
  1212. return 0;
  1213. nla_put_failure:
  1214. genlmsg_cancel(skb, ovs_header);
  1215. error:
  1216. return -EMSGSIZE;
  1217. }
  1218. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1219. {
  1220. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1221. }
  1222. /* Called with rcu_read_lock or ovs_mutex. */
  1223. static struct datapath *lookup_datapath(struct net *net,
  1224. const struct ovs_header *ovs_header,
  1225. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1226. {
  1227. struct datapath *dp;
  1228. if (!a[OVS_DP_ATTR_NAME])
  1229. dp = get_dp(net, ovs_header->dp_ifindex);
  1230. else {
  1231. struct vport *vport;
  1232. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1233. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1234. }
  1235. return dp ? dp : ERR_PTR(-ENODEV);
  1236. }
  1237. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1238. {
  1239. struct datapath *dp;
  1240. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1241. if (IS_ERR(dp))
  1242. return;
  1243. WARN(dp->user_features, "Dropping previously announced user features\n");
  1244. dp->user_features = 0;
  1245. }
  1246. static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
  1247. {
  1248. if (a[OVS_DP_ATTR_USER_FEATURES])
  1249. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1250. }
  1251. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1252. {
  1253. struct nlattr **a = info->attrs;
  1254. struct vport_parms parms;
  1255. struct sk_buff *reply;
  1256. struct datapath *dp;
  1257. struct vport *vport;
  1258. struct ovs_net *ovs_net;
  1259. int err, i;
  1260. err = -EINVAL;
  1261. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1262. goto err;
  1263. reply = ovs_dp_cmd_alloc_info(info);
  1264. if (!reply)
  1265. return -ENOMEM;
  1266. err = -ENOMEM;
  1267. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1268. if (dp == NULL)
  1269. goto err_free_reply;
  1270. ovs_dp_set_net(dp, sock_net(skb->sk));
  1271. /* Allocate table. */
  1272. err = ovs_flow_tbl_init(&dp->table);
  1273. if (err)
  1274. goto err_free_dp;
  1275. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1276. if (!dp->stats_percpu) {
  1277. err = -ENOMEM;
  1278. goto err_destroy_table;
  1279. }
  1280. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1281. GFP_KERNEL);
  1282. if (!dp->ports) {
  1283. err = -ENOMEM;
  1284. goto err_destroy_percpu;
  1285. }
  1286. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1287. INIT_HLIST_HEAD(&dp->ports[i]);
  1288. /* Set up our datapath device. */
  1289. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1290. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1291. parms.options = NULL;
  1292. parms.dp = dp;
  1293. parms.port_no = OVSP_LOCAL;
  1294. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1295. ovs_dp_change(dp, a);
  1296. /* So far only local changes have been made, now need the lock. */
  1297. ovs_lock();
  1298. vport = new_vport(&parms);
  1299. if (IS_ERR(vport)) {
  1300. err = PTR_ERR(vport);
  1301. if (err == -EBUSY)
  1302. err = -EEXIST;
  1303. if (err == -EEXIST) {
  1304. /* An outdated user space instance that does not understand
  1305. * the concept of user_features has attempted to create a new
  1306. * datapath and is likely to reuse it. Drop all user features.
  1307. */
  1308. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1309. ovs_dp_reset_user_features(skb, info);
  1310. }
  1311. goto err_destroy_ports_array;
  1312. }
  1313. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1314. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1315. BUG_ON(err < 0);
  1316. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1317. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1318. ovs_unlock();
  1319. ovs_notify(&dp_datapath_genl_family, reply, info);
  1320. return 0;
  1321. err_destroy_ports_array:
  1322. ovs_unlock();
  1323. kfree(dp->ports);
  1324. err_destroy_percpu:
  1325. free_percpu(dp->stats_percpu);
  1326. err_destroy_table:
  1327. ovs_flow_tbl_destroy(&dp->table);
  1328. err_free_dp:
  1329. kfree(dp);
  1330. err_free_reply:
  1331. kfree_skb(reply);
  1332. err:
  1333. return err;
  1334. }
  1335. /* Called with ovs_mutex. */
  1336. static void __dp_destroy(struct datapath *dp)
  1337. {
  1338. int i;
  1339. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1340. struct vport *vport;
  1341. struct hlist_node *n;
  1342. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1343. if (vport->port_no != OVSP_LOCAL)
  1344. ovs_dp_detach_port(vport);
  1345. }
  1346. list_del_rcu(&dp->list_node);
  1347. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1348. * all ports in datapath are destroyed first before freeing datapath.
  1349. */
  1350. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1351. /* RCU destroy the flow table */
  1352. call_rcu(&dp->rcu, destroy_dp_rcu);
  1353. }
  1354. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1355. {
  1356. struct sk_buff *reply;
  1357. struct datapath *dp;
  1358. int err;
  1359. reply = ovs_dp_cmd_alloc_info(info);
  1360. if (!reply)
  1361. return -ENOMEM;
  1362. ovs_lock();
  1363. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1364. err = PTR_ERR(dp);
  1365. if (IS_ERR(dp))
  1366. goto err_unlock_free;
  1367. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1368. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1369. BUG_ON(err < 0);
  1370. __dp_destroy(dp);
  1371. ovs_unlock();
  1372. ovs_notify(&dp_datapath_genl_family, reply, info);
  1373. return 0;
  1374. err_unlock_free:
  1375. ovs_unlock();
  1376. kfree_skb(reply);
  1377. return err;
  1378. }
  1379. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1380. {
  1381. struct sk_buff *reply;
  1382. struct datapath *dp;
  1383. int err;
  1384. reply = ovs_dp_cmd_alloc_info(info);
  1385. if (!reply)
  1386. return -ENOMEM;
  1387. ovs_lock();
  1388. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1389. err = PTR_ERR(dp);
  1390. if (IS_ERR(dp))
  1391. goto err_unlock_free;
  1392. ovs_dp_change(dp, info->attrs);
  1393. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1394. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1395. BUG_ON(err < 0);
  1396. ovs_unlock();
  1397. ovs_notify(&dp_datapath_genl_family, reply, info);
  1398. return 0;
  1399. err_unlock_free:
  1400. ovs_unlock();
  1401. kfree_skb(reply);
  1402. return err;
  1403. }
  1404. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1405. {
  1406. struct sk_buff *reply;
  1407. struct datapath *dp;
  1408. int err;
  1409. reply = ovs_dp_cmd_alloc_info(info);
  1410. if (!reply)
  1411. return -ENOMEM;
  1412. ovs_lock();
  1413. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1414. if (IS_ERR(dp)) {
  1415. err = PTR_ERR(dp);
  1416. goto err_unlock_free;
  1417. }
  1418. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1419. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1420. BUG_ON(err < 0);
  1421. ovs_unlock();
  1422. return genlmsg_reply(reply, info);
  1423. err_unlock_free:
  1424. ovs_unlock();
  1425. kfree_skb(reply);
  1426. return err;
  1427. }
  1428. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1429. {
  1430. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1431. struct datapath *dp;
  1432. int skip = cb->args[0];
  1433. int i = 0;
  1434. ovs_lock();
  1435. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1436. if (i >= skip &&
  1437. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1438. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1439. OVS_DP_CMD_NEW) < 0)
  1440. break;
  1441. i++;
  1442. }
  1443. ovs_unlock();
  1444. cb->args[0] = i;
  1445. return skb->len;
  1446. }
  1447. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1448. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1449. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1450. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1451. };
  1452. static const struct genl_ops dp_datapath_genl_ops[] = {
  1453. { .cmd = OVS_DP_CMD_NEW,
  1454. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1455. .policy = datapath_policy,
  1456. .doit = ovs_dp_cmd_new
  1457. },
  1458. { .cmd = OVS_DP_CMD_DEL,
  1459. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1460. .policy = datapath_policy,
  1461. .doit = ovs_dp_cmd_del
  1462. },
  1463. { .cmd = OVS_DP_CMD_GET,
  1464. .flags = 0, /* OK for unprivileged users. */
  1465. .policy = datapath_policy,
  1466. .doit = ovs_dp_cmd_get,
  1467. .dumpit = ovs_dp_cmd_dump
  1468. },
  1469. { .cmd = OVS_DP_CMD_SET,
  1470. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1471. .policy = datapath_policy,
  1472. .doit = ovs_dp_cmd_set,
  1473. },
  1474. };
  1475. static struct genl_family dp_datapath_genl_family = {
  1476. .id = GENL_ID_GENERATE,
  1477. .hdrsize = sizeof(struct ovs_header),
  1478. .name = OVS_DATAPATH_FAMILY,
  1479. .version = OVS_DATAPATH_VERSION,
  1480. .maxattr = OVS_DP_ATTR_MAX,
  1481. .netnsok = true,
  1482. .parallel_ops = true,
  1483. .ops = dp_datapath_genl_ops,
  1484. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1485. .mcgrps = &ovs_dp_datapath_multicast_group,
  1486. .n_mcgrps = 1,
  1487. };
  1488. /* Called with ovs_mutex or RCU read lock. */
  1489. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1490. u32 portid, u32 seq, u32 flags, u8 cmd)
  1491. {
  1492. struct ovs_header *ovs_header;
  1493. struct ovs_vport_stats vport_stats;
  1494. int err;
  1495. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1496. flags, cmd);
  1497. if (!ovs_header)
  1498. return -EMSGSIZE;
  1499. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1500. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1501. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1502. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1503. vport->ops->get_name(vport)))
  1504. goto nla_put_failure;
  1505. ovs_vport_get_stats(vport, &vport_stats);
  1506. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1507. &vport_stats))
  1508. goto nla_put_failure;
  1509. if (ovs_vport_get_upcall_portids(vport, skb))
  1510. goto nla_put_failure;
  1511. err = ovs_vport_get_options(vport, skb);
  1512. if (err == -EMSGSIZE)
  1513. goto error;
  1514. genlmsg_end(skb, ovs_header);
  1515. return 0;
  1516. nla_put_failure:
  1517. err = -EMSGSIZE;
  1518. error:
  1519. genlmsg_cancel(skb, ovs_header);
  1520. return err;
  1521. }
  1522. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1523. {
  1524. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1525. }
  1526. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1527. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1528. u32 seq, u8 cmd)
  1529. {
  1530. struct sk_buff *skb;
  1531. int retval;
  1532. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1533. if (!skb)
  1534. return ERR_PTR(-ENOMEM);
  1535. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1536. BUG_ON(retval < 0);
  1537. return skb;
  1538. }
  1539. /* Called with ovs_mutex or RCU read lock. */
  1540. static struct vport *lookup_vport(struct net *net,
  1541. const struct ovs_header *ovs_header,
  1542. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1543. {
  1544. struct datapath *dp;
  1545. struct vport *vport;
  1546. if (a[OVS_VPORT_ATTR_NAME]) {
  1547. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1548. if (!vport)
  1549. return ERR_PTR(-ENODEV);
  1550. if (ovs_header->dp_ifindex &&
  1551. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1552. return ERR_PTR(-ENODEV);
  1553. return vport;
  1554. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1555. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1556. if (port_no >= DP_MAX_PORTS)
  1557. return ERR_PTR(-EFBIG);
  1558. dp = get_dp(net, ovs_header->dp_ifindex);
  1559. if (!dp)
  1560. return ERR_PTR(-ENODEV);
  1561. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1562. if (!vport)
  1563. return ERR_PTR(-ENODEV);
  1564. return vport;
  1565. } else
  1566. return ERR_PTR(-EINVAL);
  1567. }
  1568. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1569. {
  1570. struct nlattr **a = info->attrs;
  1571. struct ovs_header *ovs_header = info->userhdr;
  1572. struct vport_parms parms;
  1573. struct sk_buff *reply;
  1574. struct vport *vport;
  1575. struct datapath *dp;
  1576. u32 port_no;
  1577. int err;
  1578. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1579. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1580. return -EINVAL;
  1581. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1582. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1583. if (port_no >= DP_MAX_PORTS)
  1584. return -EFBIG;
  1585. reply = ovs_vport_cmd_alloc_info();
  1586. if (!reply)
  1587. return -ENOMEM;
  1588. ovs_lock();
  1589. restart:
  1590. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1591. err = -ENODEV;
  1592. if (!dp)
  1593. goto exit_unlock_free;
  1594. if (port_no) {
  1595. vport = ovs_vport_ovsl(dp, port_no);
  1596. err = -EBUSY;
  1597. if (vport)
  1598. goto exit_unlock_free;
  1599. } else {
  1600. for (port_no = 1; ; port_no++) {
  1601. if (port_no >= DP_MAX_PORTS) {
  1602. err = -EFBIG;
  1603. goto exit_unlock_free;
  1604. }
  1605. vport = ovs_vport_ovsl(dp, port_no);
  1606. if (!vport)
  1607. break;
  1608. }
  1609. }
  1610. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1611. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1612. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1613. parms.dp = dp;
  1614. parms.port_no = port_no;
  1615. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1616. vport = new_vport(&parms);
  1617. err = PTR_ERR(vport);
  1618. if (IS_ERR(vport)) {
  1619. if (err == -EAGAIN)
  1620. goto restart;
  1621. goto exit_unlock_free;
  1622. }
  1623. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1624. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1625. BUG_ON(err < 0);
  1626. ovs_unlock();
  1627. ovs_notify(&dp_vport_genl_family, reply, info);
  1628. return 0;
  1629. exit_unlock_free:
  1630. ovs_unlock();
  1631. kfree_skb(reply);
  1632. return err;
  1633. }
  1634. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1635. {
  1636. struct nlattr **a = info->attrs;
  1637. struct sk_buff *reply;
  1638. struct vport *vport;
  1639. int err;
  1640. reply = ovs_vport_cmd_alloc_info();
  1641. if (!reply)
  1642. return -ENOMEM;
  1643. ovs_lock();
  1644. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1645. err = PTR_ERR(vport);
  1646. if (IS_ERR(vport))
  1647. goto exit_unlock_free;
  1648. if (a[OVS_VPORT_ATTR_TYPE] &&
  1649. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1650. err = -EINVAL;
  1651. goto exit_unlock_free;
  1652. }
  1653. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1654. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1655. if (err)
  1656. goto exit_unlock_free;
  1657. }
  1658. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1659. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1660. err = ovs_vport_set_upcall_portids(vport, ids);
  1661. if (err)
  1662. goto exit_unlock_free;
  1663. }
  1664. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1665. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1666. BUG_ON(err < 0);
  1667. ovs_unlock();
  1668. ovs_notify(&dp_vport_genl_family, reply, info);
  1669. return 0;
  1670. exit_unlock_free:
  1671. ovs_unlock();
  1672. kfree_skb(reply);
  1673. return err;
  1674. }
  1675. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1676. {
  1677. struct nlattr **a = info->attrs;
  1678. struct sk_buff *reply;
  1679. struct vport *vport;
  1680. int err;
  1681. reply = ovs_vport_cmd_alloc_info();
  1682. if (!reply)
  1683. return -ENOMEM;
  1684. ovs_lock();
  1685. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1686. err = PTR_ERR(vport);
  1687. if (IS_ERR(vport))
  1688. goto exit_unlock_free;
  1689. if (vport->port_no == OVSP_LOCAL) {
  1690. err = -EINVAL;
  1691. goto exit_unlock_free;
  1692. }
  1693. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1694. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1695. BUG_ON(err < 0);
  1696. ovs_dp_detach_port(vport);
  1697. ovs_unlock();
  1698. ovs_notify(&dp_vport_genl_family, reply, info);
  1699. return 0;
  1700. exit_unlock_free:
  1701. ovs_unlock();
  1702. kfree_skb(reply);
  1703. return err;
  1704. }
  1705. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1706. {
  1707. struct nlattr **a = info->attrs;
  1708. struct ovs_header *ovs_header = info->userhdr;
  1709. struct sk_buff *reply;
  1710. struct vport *vport;
  1711. int err;
  1712. reply = ovs_vport_cmd_alloc_info();
  1713. if (!reply)
  1714. return -ENOMEM;
  1715. rcu_read_lock();
  1716. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1717. err = PTR_ERR(vport);
  1718. if (IS_ERR(vport))
  1719. goto exit_unlock_free;
  1720. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1721. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1722. BUG_ON(err < 0);
  1723. rcu_read_unlock();
  1724. return genlmsg_reply(reply, info);
  1725. exit_unlock_free:
  1726. rcu_read_unlock();
  1727. kfree_skb(reply);
  1728. return err;
  1729. }
  1730. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1731. {
  1732. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1733. struct datapath *dp;
  1734. int bucket = cb->args[0], skip = cb->args[1];
  1735. int i, j = 0;
  1736. rcu_read_lock();
  1737. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1738. if (!dp) {
  1739. rcu_read_unlock();
  1740. return -ENODEV;
  1741. }
  1742. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1743. struct vport *vport;
  1744. j = 0;
  1745. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1746. if (j >= skip &&
  1747. ovs_vport_cmd_fill_info(vport, skb,
  1748. NETLINK_CB(cb->skb).portid,
  1749. cb->nlh->nlmsg_seq,
  1750. NLM_F_MULTI,
  1751. OVS_VPORT_CMD_NEW) < 0)
  1752. goto out;
  1753. j++;
  1754. }
  1755. skip = 0;
  1756. }
  1757. out:
  1758. rcu_read_unlock();
  1759. cb->args[0] = i;
  1760. cb->args[1] = j;
  1761. return skb->len;
  1762. }
  1763. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1764. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1765. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1766. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1767. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1768. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1769. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1770. };
  1771. static const struct genl_ops dp_vport_genl_ops[] = {
  1772. { .cmd = OVS_VPORT_CMD_NEW,
  1773. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1774. .policy = vport_policy,
  1775. .doit = ovs_vport_cmd_new
  1776. },
  1777. { .cmd = OVS_VPORT_CMD_DEL,
  1778. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1779. .policy = vport_policy,
  1780. .doit = ovs_vport_cmd_del
  1781. },
  1782. { .cmd = OVS_VPORT_CMD_GET,
  1783. .flags = 0, /* OK for unprivileged users. */
  1784. .policy = vport_policy,
  1785. .doit = ovs_vport_cmd_get,
  1786. .dumpit = ovs_vport_cmd_dump
  1787. },
  1788. { .cmd = OVS_VPORT_CMD_SET,
  1789. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1790. .policy = vport_policy,
  1791. .doit = ovs_vport_cmd_set,
  1792. },
  1793. };
  1794. struct genl_family dp_vport_genl_family = {
  1795. .id = GENL_ID_GENERATE,
  1796. .hdrsize = sizeof(struct ovs_header),
  1797. .name = OVS_VPORT_FAMILY,
  1798. .version = OVS_VPORT_VERSION,
  1799. .maxattr = OVS_VPORT_ATTR_MAX,
  1800. .netnsok = true,
  1801. .parallel_ops = true,
  1802. .ops = dp_vport_genl_ops,
  1803. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1804. .mcgrps = &ovs_dp_vport_multicast_group,
  1805. .n_mcgrps = 1,
  1806. };
  1807. static struct genl_family * const dp_genl_families[] = {
  1808. &dp_datapath_genl_family,
  1809. &dp_vport_genl_family,
  1810. &dp_flow_genl_family,
  1811. &dp_packet_genl_family,
  1812. };
  1813. static void dp_unregister_genl(int n_families)
  1814. {
  1815. int i;
  1816. for (i = 0; i < n_families; i++)
  1817. genl_unregister_family(dp_genl_families[i]);
  1818. }
  1819. static int dp_register_genl(void)
  1820. {
  1821. int err;
  1822. int i;
  1823. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1824. err = genl_register_family(dp_genl_families[i]);
  1825. if (err)
  1826. goto error;
  1827. }
  1828. return 0;
  1829. error:
  1830. dp_unregister_genl(i);
  1831. return err;
  1832. }
  1833. static int __net_init ovs_init_net(struct net *net)
  1834. {
  1835. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1836. INIT_LIST_HEAD(&ovs_net->dps);
  1837. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1838. return 0;
  1839. }
  1840. static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
  1841. struct list_head *head)
  1842. {
  1843. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1844. struct datapath *dp;
  1845. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1846. int i;
  1847. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1848. struct vport *vport;
  1849. hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
  1850. struct netdev_vport *netdev_vport;
  1851. if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
  1852. continue;
  1853. netdev_vport = netdev_vport_priv(vport);
  1854. if (dev_net(netdev_vport->dev) == dnet)
  1855. list_add(&vport->detach_list, head);
  1856. }
  1857. }
  1858. }
  1859. }
  1860. static void __net_exit ovs_exit_net(struct net *dnet)
  1861. {
  1862. struct datapath *dp, *dp_next;
  1863. struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
  1864. struct vport *vport, *vport_next;
  1865. struct net *net;
  1866. LIST_HEAD(head);
  1867. ovs_lock();
  1868. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1869. __dp_destroy(dp);
  1870. rtnl_lock();
  1871. for_each_net(net)
  1872. list_vports_from_net(net, dnet, &head);
  1873. rtnl_unlock();
  1874. /* Detach all vports from given namespace. */
  1875. list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
  1876. list_del(&vport->detach_list);
  1877. ovs_dp_detach_port(vport);
  1878. }
  1879. ovs_unlock();
  1880. cancel_work_sync(&ovs_net->dp_notify_work);
  1881. }
  1882. static struct pernet_operations ovs_net_ops = {
  1883. .init = ovs_init_net,
  1884. .exit = ovs_exit_net,
  1885. .id = &ovs_net_id,
  1886. .size = sizeof(struct ovs_net),
  1887. };
  1888. static int __init dp_init(void)
  1889. {
  1890. int err;
  1891. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1892. pr_info("Open vSwitch switching datapath\n");
  1893. err = action_fifos_init();
  1894. if (err)
  1895. goto error;
  1896. err = ovs_internal_dev_rtnl_link_register();
  1897. if (err)
  1898. goto error_action_fifos_exit;
  1899. err = ovs_flow_init();
  1900. if (err)
  1901. goto error_unreg_rtnl_link;
  1902. err = ovs_vport_init();
  1903. if (err)
  1904. goto error_flow_exit;
  1905. err = register_pernet_device(&ovs_net_ops);
  1906. if (err)
  1907. goto error_vport_exit;
  1908. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1909. if (err)
  1910. goto error_netns_exit;
  1911. err = ovs_netdev_init();
  1912. if (err)
  1913. goto error_unreg_notifier;
  1914. err = dp_register_genl();
  1915. if (err < 0)
  1916. goto error_unreg_netdev;
  1917. return 0;
  1918. error_unreg_netdev:
  1919. ovs_netdev_exit();
  1920. error_unreg_notifier:
  1921. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1922. error_netns_exit:
  1923. unregister_pernet_device(&ovs_net_ops);
  1924. error_vport_exit:
  1925. ovs_vport_exit();
  1926. error_flow_exit:
  1927. ovs_flow_exit();
  1928. error_unreg_rtnl_link:
  1929. ovs_internal_dev_rtnl_link_unregister();
  1930. error_action_fifos_exit:
  1931. action_fifos_exit();
  1932. error:
  1933. return err;
  1934. }
  1935. static void dp_cleanup(void)
  1936. {
  1937. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1938. ovs_netdev_exit();
  1939. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1940. unregister_pernet_device(&ovs_net_ops);
  1941. rcu_barrier();
  1942. ovs_vport_exit();
  1943. ovs_flow_exit();
  1944. ovs_internal_dev_rtnl_link_unregister();
  1945. action_fifos_exit();
  1946. }
  1947. module_init(dp_init);
  1948. module_exit(dp_cleanup);
  1949. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1950. MODULE_LICENSE("GPL");