datapath.c 53 KB

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